【问题标题】:How to use Poedit with smarty templates?如何将 Poedit 与 smarty 模板一起使用?
【发布时间】:2014-08-26 10:23:37
【问题描述】:

我在 smarty 模板(phtml 和 tpl 文件)上使用 PoEdit,并为 PHP 解析器提供以下 Poedit 配置:

扩展:

*.php;*.phtml;*.tpl

解析器命令:

xgettext --language=PHP --force-po -o %o %C %K %F

如果我在 PHP 标记内使用 gettext <?php _('test'); ?> Poedit 会按预期检测到该翻译。但是我使用 Smarty {_('test')} 来显示翻译,Poedit 没有检测到它。

有没有办法让 Smarty 将 { 和 } 字符检测为 PHP 标签?

【问题讨论】:

    标签: php smarty gettext poedit


    【解决方案1】:

    最近我开始使用与 Smarty 和 gettext 一起使用的 Yii。但是poedit不支持smarty的语法。

    https://github.com/Garefild/PoeditSmarty

    PoeditSmarty 的使用让 Poedit 能够识别 Smarty 的语法。

    {t}Text to be localized{/t}
    {"Text to be localized"|_} 
    {_("Text to be localized")}
    

    使用非常简单,PoeditSmarty 创建了一个 Poedit 知道可以读取的“pot”文件。

    例如,我有一个文件“Header.tpl”

            <form class="login" method="post" id="login">
            <h2>{__('login')}</h2>
            <div class="wrap">
                <input type="text" autofocus="" placeholder="{__('Username')}" class="form-control" name="username">
                <br>
                <input type="password" placeholder="{__('Password')}" class="form-control" name="password">
                <div class="captcha">
                    <img alt="" src="" id="codeImage">
                    <label class="checkbox">
                        <span class="pull-right">
                            <a href="#" id="code" class="forgot">{__('Get a new code')}</a>
                        </span>
                    </label>
                    <input type="text" placeholder="{__('verify Code')}" class="form-control" name="verifyCode">
                </div>
                <button type="submit" class="btn login-btn" name="submitLogin">
                    <i class="fa fa-lock"></i>
                    {__('Log me in')}
                </button>
                <hr>
                <div class="password">
                    <p>{__('Do you not remember your password')} ?</p>
                    <a data-toggle="modal" class="forgot">{__('Password Recovery')}</a>
                </div>
                <hr>
                <h3 class="logo">{__('siteName')}</h3>
            </div>
        </form> 
    

    我将通过 cmd 运行 PoeditSmarty

    java -jar "C:\Program Files\Poedit\GettextTools\bin\PoeditSmarty.jar" -o C:\Users\Mike\Desktop\xxx\tempPot.pot -c UTF-8 -k __ -f header.tpl
    

    pot文件的结果

        # Garefild2@gmail.com.
        # Garefild <Garefild2@gmail.com>, 2015-2020.
        #
    
        msgid ""
        msgstr ""
        "Project-Id-Version: PACKAGE VERSION\n"
        "Report-Msgid-Bugs-To: \n"
        "POT-Creation-Date: 2015-12-29 16:47\n"
        "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
        "Last-Translator: FULL NAME <Garefild2@gmail.com>\n"
        "Language-Team: LANGUAGE <Garefild2@gmail.com>\n"
        "Language: \n"
        "MIME-Version: 1.0\n"
        "Content-Type: text/plain; charset=CHARSET\n"
        "Content-Transfer-Encoding: 8bit\n"
    
        #: header.tpl:48
        msgid "Password Recovery"
        msgstr ""
    
        #: header.tpl:36
        msgid "Get a new code"
        msgstr ""
    
        #: header.tpl:29
        msgid "Username"
        msgstr ""
    
        #: header.tpl:39
        msgid "verify Code"
        msgstr ""
    
        #: header.tpl:51
        msgid "siteName"
        msgstr ""
    
        #: header.tpl:27
        msgid "login"
        msgstr ""
    
        #: header.tpl:47
        msgid "Do you not remember your password"
        msgstr ""
    
        #: header.tpl:31
        msgid "Password"
        msgstr ""
    
        #: header.tpl:43
        msgid "Log me in"
        msgstr ""
    

    通过 Poedit

    enter image description here

    【讨论】:

    • 这和 PHP 有什么关系?
    • 它在 ph 级别什么都不做,它允许 Poedit 知道 Smarty 的语法。因此可以与 smarty php 和 Poedit 结合使用。
    • 这确实是有效的答案,谢谢分享。我不明白为什么它被否决了......
    • 很高兴我能帮上忙 :) 你用过吗?它运作良好吗?还有一个java版本|| exe?
    【解决方案2】:

    据我所知,这是不可能的。您可以更改 Smarty 分隔符,但不能强制 Smarty 将默认 Smarty 分隔符视为 PHP 代码。

    【讨论】:

      【解决方案3】:

      我想到的解决方案是使用templates_c文件夹中的Smarty编译文件作为PoEdit的源。我刚刚制作了这个小脚本,以便在更新 Po 文件之前编译我的所有视图:

      // ... smarty config here ...
      
      $i = 0;
      if ($handle = opendir('./views/')) {
          while (false !== ($file = readdir($handle))) {
              if ($file != "." && $file != ".." && is_file('./views/'.$file) {
                  $smarty->fetch('./views/'.$file);
                  $i++;
              }
          }
      }
      die('<h2>' . $i . ' templates compiled</h2>');
      

      【讨论】:

      • 或者你可以使用$smarty-&gt;compileAllTemplates()
      猜你喜欢
      • 2021-06-19
      • 2012-06-19
      • 2012-09-17
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 2016-06-02
      相关资源
      最近更新 更多