Twig:工作国际化(i18n)示例 [英] Twig: Working internationalization (i18n) example
问题描述
我正在寻找Twig(模板引擎)的有效i18n示例.
I am looking for a working i18n example for Twig (the template engine).
语言文件的文档很少.他们应该去哪里,看起来如何,应该命名什么?我一直在尝试使用.po/.mo文件,但是不走运.
Documentation is a bit sparse when it comes to language files. Where should they go, how should they look and what should they be named? I have been trying with .po/.mo files, no luck.
如果有人可以指出我正确的方向...
If someone could point me in the right direction...
请参阅: i18n扩展示例并没有真正告诉我有关语言文件本身的更多信息.
See: the i18n extension example which does not really tell me much about the language files themselves.
注意:我想单独使用Twig,而不是作为Symfony的一部分.
Note: I want to use Twig on its own, not as part of Symfony.
这是我的php文件:
<?php
header('Content-Type: text/html; charset=utf-8');
$vars = array();
foreach($_POST as $key => $value) {
$vars[$key] = json_decode(utf8_encode(urldecode($value)));
}
/* Prepare Twig template enginge */
require_once './lib/Twig/Autoloader.php';
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('./templates');
$twig = new Twig_Environment($loader, array(
//'cache' => './cache',
'cache' => false
));
/* i18n */
$twig->addExtension(new Twig_Extensions_Extension_I18n());
$availableLanguages = array(
'en' => 'en_EN',
'de' => 'de_DE',
'default' => 'de_DE'
);
// Set language
$locale = array_key_exists($_GET['lang'], $availableLanguages) ? $availableLanguages[$_GET['lang']] : $availableLanguages['default'];
putenv('LC_ALL='.$locale);
setlocale(LC_ALL, $locale);
// Specify the location of the translation tables
bindtextdomain('kalkulator', 'includes/locale');
bind_textdomain_codeset('kalkulator', 'UTF-8');
// Choose domain
textdomain('kalkulator');
$template = $twig->loadTemplate('print.tpl');
$html = $template->render($vars);
switch($_GET['action']) {
case 'mail':
break;
default:
echo $html;
break;
}
?>
在include/locale内,我有以下文件:
And inside includes/locale I have the following files:
-rw-r--r-- 1 user group 670 Jul 28 10:17 kalkulator-de_DE.mo
-rw-r--r-- 1 user group 982 Jul 28 10:22 kalkulator-de_DE.po
-rw-r--r-- 1 user group 688 Jul 28 10:38 kalkulator-en_EN.mo
-rw-r--r-- 1 user group 1004 Jul 28 10:38 kalkulator-en_EN.po
在print.tpl文件中,我使用标签指定要翻译的部分:
And inside the print.tpl file I am using tags to specify which parts are to be translated:
{% trans %}
Text to be translated
{% endtrans %}
推荐答案
twig i18n扩展名基于gettext.因此,首先要考虑与gettext相关的所有内容.您可以在以下文档中找到该文档: Gettext Docs .
The twig i18n extension is based on gettext. So first of all everything related to gettext applies. You find that documented here: Gettext Docs.
因此,现在对您的问题的更具体部分进行讨论,但是请确保您至少已了解gettext的基本原理:
So now to the more concrete parts of you question, but please ensure you've at least understood the basic principles with gettext:
[语言文件]应该放在哪里
Where should they [the language files] go
进入已为文本域注册的目录.
,它们的外观和名称应如何?
, how should they look and what should they be named?
语言文件应命名为:
文本域 -
语言环境 .po/mo
名为myAppPhp
和fr_FR
语言环境的文本域的示例:
Example for a text domain called myAppPhp
and the fr_FR
locale:
myAppPhp-fr_FR.po
myAppPhp-fr_FR.mo
我一直在尝试使用
.po/.mo
文件,没有运气.
I have been trying with
.po/.mo
files, no luck.
.po/.mo
对我来说听起来不错,也许您只是错过了移动它们的路径,或者忘记了在前面添加文本域.
.po/.mo
sounds good to me, maybe you have just missed the path where to move them or you have forgotten to add the text-domain in front.
这篇关于Twig:工作国际化(i18n)示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!