开发者

Making Smarty supporting multiple languages

开发者 https://www.devze.com 2023-03-15 13:44 出处:网络
first of all, let me tell ya, that I\'m from Germany. So my English will not be very well. Please forgive me. =P

first of all, let me tell ya, that I'm from Germany. So my English will not be very well. Please forgive me. =P

I'm about to develop a multilingual website with PHP5. In order to seperate the presentation layer from the business logic, I'm using the SmartyTemplateEngine (v3.0.8). To make it multilingual I had to edit this SmartyPlugin sometimes. But finally it is working for me. I'm using it that way:

{lang}language.string{/lang}.

In the language file I have:

language.string = <![CDATA[Hello world!]]> (So it says Hello world!)

Works fine. But I want to expend the script a bit further. I want to pass a variable to the language string. Something like this:

{lang s=$userName}language.string{/lang}

In the language file I want to have:

language.string = <![CDATA[Hello %s!]]> (S开发者_如何转开发o it says Hello username!)

I tried to find my solution with Google, but I didn't find something good. As far as I'm not a professional in PHP, I'm not capable of editing it myself. I hope somebody can help me with that. It is bothering me quite a while...

Greets, Basti


Well, I'm not using this plugin, but had to do the same thing. I figuered out that for my needs the following was the best solution:

In an XML file I define my strings (this example is xml/en/content.xml):

<translations>
    <translation id="hello_world"><![CDATA[Hello ##username##!]]></translation>
    <translation id="how_are_you"><![CDATA[How are you?]]></translation>
</translations>

In my Localizer class I initialize these translations and save them in an array. The translate function gets the ID string from smarty, searches for the id in its translations and for any ##string## text. These ##...## will be replaced with variables already assigned to smarty.

class Localizer {

    private static $translations = array();

    public static function init($language) {

        $temp_content = simplexml_load_file('xml/' . $language . '/content.xml');
        foreach ($temp_content as $key => $value){
            self::$translations[(string)$value['id']] = (string)$value;
        }

    }

    public static function translate($params, $name, $smarty) {

         $translation = '';
         if( ! is_null($name) && array_key_exists($name, self::$translations)) {

            // get variables in translation text
            $translation = self::$translations[$name];
            preg_match_all('/##([^#]+)##/i', $translation, $vars, PREG_SET_ORDER);

            // replace with assigned smarty values
            foreach($vars as $var) {
                $translation = str_replace($var[0], $smarty->getTemplateVars($var[1]), $translation);
            }

        }

        return $translation;

    }

}

Now you have to tell smarty which function it should use. This could be your index.php:

include('Localizer.class.php');
Localizer::init('en');
$smarty->registerPlugin('block', 'translate', array('Localizer', 'translate'), true);

To use the translations, first, I assign the username:

$smarty->assign('username', $username);

In the template file:

{translate}hello_world{/translate}

Hope this helps, greetings from austria :)

0

精彩评论

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

关注公众号