Twig date time_diff 翻译

2022-01-22 00:00:00 php symfony twig

我正在使用 Twig Date 扩展来获取工作时间差异.

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

要获得可翻译的输出,请给出SymfonyComponentTranslationTranslatorInterface 作为构造函数争论.返回的字符串格式为 diff.ago.XXX 或diff.in.XXX 其中 XXX 可以是任何有效单位:秒、分钟、小时、日、月、年.

To get a translatable output, give a SymfonyComponentTranslationTranslatorInterface 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());

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

我收到下一个错误

错误:无法实例化接口SymfonyComponentTranslationTranslatorInterface

Error: Cannot instantiate interface SymfonyComponentTranslationTranslatorInterface

Twig_Environment 构造函数正在等待 Twig_LoaderInterface 对象,而不是 TranslatorInterface.

Twig_Environment constructor is waiting for Twig_LoaderInterface object, not TranslatorInterface.

应该怎么做才能让我翻译 time_diff 输出?

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

谢谢

推荐答案

您阅读的是 Twig 文档,而不是 Symfony2,即使两者都是由 SensioLabs 制作的.

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

如果您使用 Symfony 全栈框架,SymfonyComponentTranslationTranslatorInterface已经定义为 Twig 扩展的构造函数参数.

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

为确保,请查看位于 vendor wigextensionslibTwigExtensionsExtensiondate.php 的文件

你应该看到这样的东西:

You should see something like this :

*vendor	wigextensionslibTwigExtensionsExtensiondate.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 SymfonyComponentTranslationTranslatorInterface;

/**
 * @author Robin van der Vleuten <robinvdvleuten@gmail.com>
 */
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 没有为此提供现成的翻译文件依赖于您的语言环境,您必须自己制作(或找到已经完成此操作并且可以与您分享的人)).

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).

首先,将此参数添加到您的 services.yml 中的 Twig 扩展:

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 }

然后,在 appResources ranslations 位置创建一个date.fr.xliff".如果您在自己的 Bundle 中工作,即与 AppBundle 不同,请调整位置.当然,根据您要查找的语言,调整fr";(如de"、es"……).为什么是.xliff"?延期 ?当然,您可以创建一个.yml"文件.文件,例如.但是使用.xliff";让您可以利用我接下来向您提出的建议.

Then, create a "date.fr.xliff" in the location appResources ranslations. 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.

然后,如果我继续使用法语翻译示例,请打开您的date.fr.wliff"文件并复制/粘贴KnpTimeBundle 提供的翻译.

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.

如果您正在寻找在 Twig 模板中进行的翻译,只需使用过滤器而不更改任何内容(不要尝试添加第二个过滤器|trans"):

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

当然,替换user.createdAt";根据您的需要.

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

如果您愿意,只需在您的应用程序中实现 KnpTimeBundle 即可为您完成所有这些工作.否则,我想您可以复制/粘贴 KnpTimeBundle 为大量不同语言提供的翻译文件,只需注意替换它们的文件名time.fr.xliff".通过date.fr.xliff",这是 Symfony2 包中可用的 Twig 扩展所需要的.

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.

相关文章