Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig date time_diff translation

Tags:

php

twig

symfony

i am using Twig Date extension to get working time_diff.

{{ photo.getCreationDate|time_diff }}

I want to make it multilanguage. I have read the docs, it says

To get a translatable output, give a Symfony\Component\Translation\TranslatorInterface as constructor argument. The returned string is formatted as diff.ago.XXX or diff.in.XXX where XXX can be any valid unit: second, minute, hour, day, month, year.

Im not sure how to do it, but seems it doesnt work for me.

Thats the way i tried in my controller.

$twig = new Twig_Environment(new TranslatorInterface()); $twig->addExtension(new Twig_Extensions_Extension_Date());

I am getting next error

Error: Cannot instantiate interface Symfony\Component\Translation\TranslatorInterface

Twig_Environment constructor is waiting for Twig_LoaderInterface object, not TranslatorInterface.

How it should be done to allow me to translate the time_diff output?

Thanks

like image 544
Tigran Avatar asked Jan 02 '16 17:01

Tigran


2 Answers

For anyone using Symfony4, you need to uncomment a line in the config/packages/twig_extensions.yaml file:

services:
    _defaults:
        public: false
        autowire: true
        autoconfigure: true

    #Twig\Extensions\ArrayExtension: ~
    Twig\Extensions\DateExtension: ~
    #Twig\Extensions\IntlExtension: ~
    #Twig\Extensions\TextExtension: ~

Then you'll need to add translations as @Mexcanoon mentioned in his answer. Or just use the KnpTimeBundle instead. Make sure you clear your cache to load new translations.

like image 69
Yep_It's_Me Avatar answered Sep 19 '22 11:09

Yep_It's_Me


What you read is the Twig documentation, not the Symfony2's, even if both are made by SensioLabs.

If you use the Symfony full-stack framework, the Symfony\Component\Translation\TranslatorInterface is already defined as a constructor argument of your Twig extension.

To make sure, have a look at the file located at \vendor\twig\extensions\lib\Twig\Extensions\Extension\date.php

You should see something like this :

*\vendor\twig\extensions\lib\Twig\Extensions\Extension\date.php*

<?php

/**
 * This file is part of Twig.
 *
 * (c) 2014 Fabien Potencier
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
use Symfony\Component\Translation\TranslatorInterface;

/**
 * @author Robin van der Vleuten <[email protected]>
 */
class Twig_Extensions_Extension_Date extends Twig_Extension
{
    public static $units = array(
        'y' => 'year',
        'm' => 'month',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );

    /**
     * @var TranslatorInterface
     */
    private $translator;

    /**
     * Constructor.
     *
     * @param TranslatorInterface $translator A TranslatorInterface instance.
     */
    public function __construct(TranslatorInterface $translator = null)
    {
        $this->translator = $translator;
    }

// etc.

If this is is what you have on your side, then what you have to do now is the translation itself.

Symfony2 doesn't come with a ready-made-translated-file-depending-on-your-locale for this, you have to make it (or find someone that has already done this and is OK to share it with you).

First, add this argument to the Twig Extension in your services.yml :

*services.yml*

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

Then, create a "date.fr.xliff" in the location \app\Resources\translations. Adapt the location if you're working in a Bundle of your own, i.e. different of AppBundle. Of course, depending on the language you are looking for, adapt the "fr" (like "de", "es"...). Why ".xliff" extension ? Of course, you could create an ".yml" file, for example. But using ".xliff" allow you to take advantage of what I suggest to you next.

Then, if I keep going with the french translation example, open your "date.fr.wliff" file and copy/paste the translation provided by the KnpTimeBundle.

Don't forget to clear the dev cache if you have to.

If you're looking for the translation to take place in a Twig template, just use the filter without changing anything (don't try to add a second filter "|trans") :

{{ user.createdAt|time_diff }}

Of course, replace the "user.createdAt" by what you need there.

If you want to, just implement the KnpTimeBundle in your application to do all of this for you. Otherwise, I guess it's OK for you to copy/paste the translation files provided by the KnpTimeBundle for a huge amount of different languages, just take care of replacing their filename "time.fr.xliff" by "date.fr.xliff", which is needed by the Twig Extension available in the Symfony2 pack.

like image 30
Mexicanoon Avatar answered Sep 18 '22 11:09

Mexicanoon