开发者

Smarty and gettext

开发者 https://www.devze.com 2023-04-11 09:18 出处:网络
What\'s the easiest way to use gettext combined with smarty, is there some simalar function as there is available in php: _(\'hello world\');开发者_StackOverflow ?

What's the easiest way to use gettext combined with smarty, is there some simalar function as there is available in php: _('hello world');开发者_StackOverflow ?

Thanks,


I really found the support of Smarty for (n)gettext quite lacking as well. And while the existing plugin(s) seem to do a fair job, I still thought I should give it a go.

I've just released: http://code.google.com/p/smarty-gettext/

Maybe it's of help to anyone. Feedback, etc. more than appreciated.


Looks like there's a smarty-gettext plugin available: http://sourceforge.net/projects/smarty-gettext/, last updated May 2011. http://smarty.incutio.com/?page=SmartyGettext


There are lot of ways to achieve a translation of a page with Smarty.

My way

I create some .conf files containing something like:

en.conf

hello_world = "Hello! World!"
my_name_is = "They call me"

nl.conf

hello_world = "Hallo! Wereld!"
my_name_is = "Ik heet"

fr.conf

hello_world = "Bonjour! Tout le Monde!"
my_name_is = "Ils m'appellent"

Now you have 2 options:

  • You can load the .conf file from the .tpl file:
  • Or you want PHP to handle the right file. (I use this method)

template.tpl (English)

{config_load file="en.conf"}

<html>
    <body>
        <h1>{#hello_world#}</h1>
        <p>
            {#my_name_is#}
        </p>
    </body>
</html>

template.tpl (Dutch)

{config_load file="nl.conf"}

<html>
    <body>
        <h1>{#hello_world#}</h1>
        <p>
            {#my_name_is#}
        </p>
    </body>
</html>

template.php (Using Smarty Class in PHP)

 $configFile = 'fr.conf';

 // Smarty Version 2
 $this->smarty->config_load($configFile);

 //Smarty Version 3
 $this->smarty->configLoad($configFile);

I hope this works out for you as well.


Please correct me if I am wrong. I use Smarty 3.1, added to my project via composer. And it seems to be the case that I am able to execute any php function in my .tpl file. For example:

<p>post_max_size: {ini_get('post_max_size')}</p>

it gives me:

<p>post_max_size: 8M</p>

That behaviour makes me think that I should be able to do simply:

<p>{_("I like the way it works")}</p>

The first test shows no error and outputs:

<p>I like the way it works</p>

I have no translation file yet and i don't know if the call will be recognized by xgettext but i will do some tests now and will tell you if it works.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号