doushichun9409 2015-01-11 02:55
浏览 91
已采纳

没有gettext的php twig多语言

I am novice in php and I write my first web page using PHP + JS without any frameworks (maybe later).

I have the next question.

How to implement multi language in Twig without Gettext or other plugins?

Something like that (in pure PHP - no questions... but how use it in twig)

$lang=array(
"about_site" => "о нас",
"project" => "проект",
"team" => "команда");

In fact my project has no restrictions. If Gettext is the best solution for me - I'll use it. I thought I'll have to translate only 5-10 words per page. Using Gettext for this purpose is a bit strange.

  • 写回答

1条回答 默认 最新

  • douguizhuang8276 2015-01-24 13:25
    关注

    You can easily use a php array like yours for translation in twig. Just pass the array to the twig template:

    /* $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);
    

    And in the template:

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

    But mind, if you have complex translations with singular/plural expressions, variables, date expressions etc. I would strongly recommend you to use the twig i18n extension that is built upon gettext: http://twig.sensiolabs.org/doc/extensions/i18n.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?