twig time ago 输出 'diff.ago.day'

twig time ago outputs 'diff.ago.day'

使用Symfony2.8 date 是 DateTime 对象

树枝

{{ event.createDate|time_diff }}

services.yml

twig.extension.date:
    class: Twig_Extensions_Extension_Date
    arguments: ["@translator"]
    tags:
        - { name: twig.extension }

输出不像 'ago',而只是 'diff.ago.day'。在树枝转储中也显示了这个字符串。怎么了?

我想你忘了在你的应用程序中实际进行翻译。

This is a good resource to reference.

其中说:

(if the translations folder doesn't exist, then create it). For example, the following file (app/Resources/translations/date.de.xliff) provides the translation for our dates in German:

并且他们就该翻译的内容提供了一个很好的例子。

<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
    <file source-language="en" datatype="plaintext" original="file.ext">
        <body>
            <trans-unit id="diff.ago.year">
                <source>diff.ago.year</source>
                <target>vor einem Jahr|vor %count% Jahren</target>
            </trans-unit>
            <trans-unit id="diff.ago.month">
                <source>diff.ago.month</source>
                <target>vor einem Monat|vor %count% Monaten</target>
            </trans-unit>
            <trans-unit id="diff.ago.day">
                <source>diff.ago.day</source>
                <target>vor %count% Tag|vor %count% Tagen</target>
            </trans-unit>
            <trans-unit id="diff.ago.hour">
                <source>diff.ago.hour</source>
                <target>vor einer Stunde|vor %count% Stunden</target>
            </trans-unit>
            <trans-unit id="diff.ago.minute">
                <source>diff.ago.minute</source>
                <target>vor einer Minute|vor %count% Minuten</target>
            </trans-unit>
            <trans-unit id="diff.ago.second">
                <source>diff.ago.second</source>
                <target>vor einer Sekunde|vor %count% Sekunden</target>
            </trans-unit>
            <trans-unit id="diff.empty">
                <source>diff.empty</source>
                <target>jetzt</target>
            </trans-unit>
            <trans-unit id="diff.in.second">
                <source>diff.in.second</source>
                <target>in einer Sekunde|in %count% Sekunden</target>
            </trans-unit>
            <trans-unit id="diff.in.hour">
                <source>diff.in.hour</source>
                <target>in einer Stunde|in %count% Stunden</target>
            </trans-unit>
            <trans-unit id="diff.in.minute">
                <source>diff.in.minute</source>
                <target>in einer Minute|in %count% Minuten</target>
            </trans-unit>
            <trans-unit id="diff.in.day">
                <source>diff.in.day</source>
                <target>in einem Tag|in %count% Tagen</target>
            </trans-unit>
            <trans-unit id="diff.in.month">
                <source>diff.in.month</source>
                <target>in einem Monat|in %count% Monaten</target>
            </trans-unit>
            <trans-unit id="diff.in.year">
                <source>diff.in.year</source>
                <target>in einem Jahr|in %count% Jahren</target>
            </trans-unit>
        </body>
    </file>
</xliff>

当然,如果您想要英文文件,那么文件应该是 app/Resources/translations/date.en.xliff,当然您可以将上面的德语替换为您自己的英文文件 taste/choice。

干杯