Skip to content
Advertisement

How to use Poedit with smarty templates?

I’m using PoEdit on smarty templates (phtml and tpl files) with the following Poedit configuration for PHP parser :

Extensions:

*.php;*.phtml;*.tpl

Parser command :

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

If i’m using gettext inside PHP tags <?php _('test'); ?> Poedit detect that translation as expected. But i’m using Smarty {_('test')} for displaying the translation, Poedit don’t detect it.

Is there a way to make Smarty detect { and } characters as PHP tags ?

Advertisement

Answer

Recently I got to work with Yii used with Smarty and gettext. but poedit not support the syntax of smarty.

https://github.com/Garefild/PoeditSmarty

And here comes the use of PoeditSmarty allowed Poedit recognize the syntax of Smarty.

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

Very simple to use, PoeditSmarty creates a “pot” file which Poedit know to read.

For example, I have a file “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> 

I’ll run the PoeditSmarty via cmd

java -jar "C:Program FilesPoeditGettextToolsbinPoeditSmarty.jar" -o C:UsersMikeDesktopxxxtempPot.pot -c UTF-8 -k __ -f header.tpl

The result of pot file

    # Garefild2@gmail.com.
    # Garefild <Garefild2@gmail.com>, 2015-2020.
    #

    msgid ""
    msgstr ""
    "Project-Id-Version: PACKAGE VERSIONn"
    "Report-Msgid-Bugs-To: n"
    "POT-Creation-Date: 2015-12-29 16:47n"
    "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONEn"
    "Last-Translator: FULL NAME <Garefild2@gmail.com>n"
    "Language-Team: LANGUAGE <Garefild2@gmail.com>n"
    "Language: n"
    "MIME-Version: 1.0n"
    "Content-Type: text/plain; charset=CHARSETn"
    "Content-Transfer-Encoding: 8bitn"

    #: 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 ""

via Poedit

enter image description here

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement