簡體   English   中英

如何將 Poedit 與 smarty 模板一起使用?

[英]How to use Poedit with smarty templates?

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

擴展:

*.php;*.phtml;*.tpl

解析器命令:

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

如果我在 PHP 標簽中使用 gettext <?php _('test'); ?> <?php _('test'); ?> Poedit 按預期檢測該翻譯。 但是我使用 Smarty {_('test')}來顯示翻譯,Poedit 沒有檢測到它。

有沒有辦法讓 Smarty 將 { 和 } 字符檢測為 PHP 標簽?

據我所知,這是不可能的。 您可以更改 Smarty 分隔符,但不能強制 Smarty 將默認 Smarty 分隔符視為 PHP 代碼。

我來的解決方案是使用模板_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 和 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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM