Dima Rashevskiy
Dima Rashevskiy

Reputation: 182

twig time ago outputs 'diff.ago.day'

Using Symfony2.8 date is DateTime object

twig

{{ event.createDate|time_diff }}

services.yml

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

output is not like 'ago', but just 'diff.ago.day'. In twig dump shows this string also. What is wrong?

Upvotes: 4

Views: 1538

Answers (1)

Yes Barry
Yes Barry

Reputation: 9876

I think you forgot to actually make the translations in your app.

This is a good resource to reference.

Which says:

(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:

And they give a good example on the contents of that translation.

<?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>

Of course if you want yours in english then the file should be app/Resources/translations/date.en.xliff and of course you'd replace the German above with something in english to your own taste/choice.

Cheers

Upvotes: 3

Related Questions