Twig:工作国际化 (i18n) 示例

发布于 2024-11-26 23:02:39 字数 2025 浏览 1 评论 0原文

我正在寻找 Twig(模板引擎)的工作 i18n 示例。

当涉及到语言文件时,文档有点稀疏。他们应该去哪里,他们应该是什么样子,他们应该叫什么名字?我一直在尝试使用 .po/.mo 文件,但没有成功。

如果有人能给我指出正确的方向...

请参阅:i18n 扩展示例< /a> 这并没有真正告诉我很多关于语言文件本身的信息。

注意:我想单独使用 Twig,而不是作为 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;
}


?>

在includes/locale 中我有以下文件:

-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 文件中我使用标签来指定要翻译的部分:

{% trans %}
Text to be translated
{% endtrans %}

I am looking for a working i18n example for Twig (the template engine).

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

See: the i18n extension example which does not really tell me much about the language files themselves.

Note: I want to use Twig on its own, not as part of Symfony.

Here is my php-file:

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


?>

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

And inside the print.tpl file I am using tags to specify which parts are to be translated:

{% trans %}
Text to be translated
{% endtrans %}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流星番茄 2024-12-03 23:02:39

twig i18n 扩展基于 gettext。因此,首先与 gettext 相关的所有内容都适用。您可以在此处找到该文档:Gettext Docs

现在到您问题的更具体部分,但请确保您至少了解 gettext 的基本原理:

他们[语言文件]应该去哪里

已经为文本域注册的目录。

,它们应该是什么样子,应该命名什么?

语言文件应命名为以下内​​容:

text-domain-locale.po/mo

文本域示例名为 myAppPhpfr_FR 语言环境:

myAppPhp-fr_FR.po
myAppPhp-fr_FR.mo

我一直在尝试使用 .po/.mo 文件,但没有成功。

.po/.mo 对我来说听起来不错,也许您刚刚错过了移动它们的路径,或者您忘记在前面添加文本域。

The twig i18n extension is based on gettext. So first of all everything related to gettext applies. You find that documented here: Gettext Docs.

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

Into the directory that has been registered for the text-domain.

, how should they look and what should they be named?

Language files should be named the following:

text-domain-locale.po/mo

Example for a text domain called myAppPhp and the fr_FR locale:

myAppPhp-fr_FR.po
myAppPhp-fr_FR.mo

I have been trying with .po/.mo files, no luck.

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

海螺姑娘 2024-12-03 23:02:39

我已经在 slim 3 中尝试了所有可能的方法,但没有任何效果,只有 slim 2,所以我创建了自己的方式。基本上系统会从浏览器检测语言并加载正确的翻译文件,更多详细信息请参见图片:

我的解决方法

I've tried everything possible in slim 3 but nothing was working, only slim 2, so I've created my own way. Basically the system detects the language from the browser and load the correct translation file, more details in the picture:

my way to solve

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文