PHP Twig翻译文件

时间:2016-02-25 13:47:59

标签: php internationalization twig translation

我正在使用twig模板引擎。我添加了I18n扩展名进行翻译,但它无法正常工作。它始终显示“密码”。

这是我的代码:

// index.php
putenv('LC_ALL=fr_FR');
setlocale(LC_ALL, 'fr_FR');

bindtextdomain('app', 'locale');
bind_textdomain_codeset('app', 'UTF-8');
textdomain('app');

require_once 'lib/vendor/autoload.php';

$loader = new Twig_Loader_Filesystem('templates');
$twig = new Twig_Environment($loader, array(
    'charset' => 'utf-8',
    'cache' => false,
    'debug' => true
));
$twig->addExtension(new Twig_Extensions_Extension_I18n());


// templates/index.html
{% include 'header.html' %}
    Translate that: {% trans "password" %}
{% include 'footer.html' %}

// locale/fr_FR/app.yml
password: mot de passe

有什么想法吗? 我没有使用symfony框架。

1 个答案:

答案 0 :(得分:0)

1。创建正确的PO文件。或者尝试使用此app.po

msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: \n"
"PO-Revision-Date: \n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=iso-8859-1\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: fr_FR\n"
"X-Generator: Poedit 1.8.7\n"

#: templates/1.php:3
msgid "password"
msgstr "mot de passe"

2。基于app.po生成app.mo。例如,使用po2mo.net工具。

3。将app.mo放置到locale/fr_FR/LC_MESSAGES目录。