我是php的新手,我使用PHP + JS编写了我的第一个网页,没有任何框架(可能以后)。
我有下一个问题。
如何在Twig中实现多语言而不需要Gettext或其他插件?
类似的东西(用纯PHP -没有问题.但如何在树枝上使用它)
$lang=array(
"about_site" => "о нас",
"project" => "проект",
"team" => "команда");
事实上,我的项目没有任何限制。如果Gettext是我最好的解决方案-我会用它。我想我每页只需要翻译5-10个单词。为此目的使用Gettext有点奇怪。
发布于 2015-01-24 13:25:00
您可以很容易地使用像您这样的php数组在小树枝中进行翻译。只需将数组传递给小枝模板:
/* $twig is a is an instance of Twig_Environment */
$template = $twig->loadTemplate('yourtemplate.html');
$templateVars['trans'] = array(
"about_site" => "о нас",
"project" => "проект",
"team" => "команда"
);
echo $template->render($templateVars);
在模板中:
<!-- a lot of html stuff -->
<ul>
<li><a href="#">{{ trans.about_site }}</a></li>
<li><a href="#">{{ trans.project }}</a></li>
<li><a href="#">{{ trans.team }}</a></li>
</ul>
但是请注意,如果您有带有单数/复数表达式、变量、日期表达式等的复杂翻译,我强烈建议您使用基于gettext:http://twig.sensiolabs.org/doc/extensions/i18n.html的小枝扩展。
https://stackoverflow.com/questions/27883530
复制相似问题