drxrgundk062317205 2015-10-28 11:05
浏览 34

Twig DateExtension翻译

I try to add translation for Twig DateExtension in my Silex application. Here is what I did for now.

First I added the requested components in my composer.json

"twig/twig": "^1.22",  
"twig/extension": "^1.3",  
"symfony/twig-bridge": "^2.7",  
"symfony/translation": "^2.7",  
"symfony/config": "^2.7",  
"symfony/yaml": "^2.7",  

Then I register services like this:

<?php
$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => __DIR__.'/../views',
));

$app->register(new Silex\Provider\TranslationServiceProvider(), array(
    'locale_fallbacks' => array('en'),
    'locale' => 'fr_FR'
));
$app['translator'] = $app->share($app->extend('translator', function($translator, $app) {
    $translator->addLoader('yaml', new Symfony\Component\Translation\Loader\YamlFileLoader());
    $translator->addResource('yaml', __DIR__.'/../src/locales/fr.yml', 'fr_FR');

    return $translator;
}));

$app['twig'] = $app->share($app->extend('twig', function(Twig_Environment $twig, $app) {
    $twig->addExtension(new Twig_Extensions_Extension_Text());
    $twig->addExtension(new Twig_Extensions_Extension_Date($app['translator']));
    return $twig;
}));

In my fr.yml I try to put translation like this:

diff.ago.day: Il y a %count% jour|Il y a %count% jours
test: converted

This configuration seems to be partially working because in my template I have (where foo is a DateTime):

{{ foo|time_diff }} - {{ "test"|trans }}

The result should be:

Il y a 1 jour - converted

But it's:

diff.ago.day - converted

So the first part isn't converted while the second one is.

Where is my mistake ?

Thanks a lot.

EDIT:

I finally found my mistake. After digging into Symfony code, I found they use a special domain for translation: 'date'. So I add a forth parameter to my ressource loading in order to tell it.

The new loading line is now:

$translator->addResource('yaml', __DIR__.'/../src/locales/fr.yml', 'fr_FR', 'date');
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
    • ¥15 乘性高斯噪声在深度学习网络中的应用
    • ¥15 运筹学排序问题中的在线排序
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛