Re: [SMARTY] Smarty and I18N

From: Date: Thu, 17 Oct 2002 02:12:56 +0000
Subject: Re: [SMARTY] Smarty and I18N
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-6181@lists.php.net to get a copy of this message
Thomas, I'm using a couple of plugins developed by Alejandro Sarco (email address in CC list) and myself to create a multilingual web site. The basic premise is that the template file contains only the HTML with embedded references to translatable text, all of the text is abstracted out into language dependent files. A couple of plugin compiler functions and postfilters then parse the template at compile time and relace all text references with the appropriate text for that language. This means that the compiled template now contains all of the translated text so no extra work is required by smarty for the language processing after the template has been compiled. The templates look something like this: <p>This text isn't translated.</p> <p> {lang STEP2} <select name="type">{lang_options options=TXN_TYPE
    selected=$ctw_type}</select><br />
<input type="image" value="doit" src="{lang TRANSLATE_BTN}" /> </p> The 'lang' smarty tags are processed by Alejandro's compiler.lang.php and postfilter.lang.php plugins, the 'lang_options' smarty tags are processed by my compier.lang_options.php and postfilter.lang_options.php plugins (instead of using the smarty html_options function). In the plugins they include the language files based on the value of the smarty compile_id, which also ensures that the compiled templates are separated out for each language (as discussed in an earlier email on this list). For example, the following line in the plugin includes the appropriate language file for my application.
    require( 'ctweb/text_lang.'.$smarty->compile_id.'.utf8' );
If the language is english, this is the 'ctweb/text_lang_en.utf8' file, if Japanese it is 'ctweb/text_lang_ja.utf8'. The contents of this file are:
$lang['STEP2']              = 'Select the type of translation:';
$lang['TRANSLATE_BTN']      = 'i/translate_en.png';
$lang_options['TXN_TYPE']    = array(
'url' => 'Web Site Translation', 'lu' => 'Dictionary Lookup', 'text' => 'Text Translation' ); The resulting compiled template file would then have the following contents in the english subdirectory. <p>This text isn't translated.</p> <p> Select the type of translation: <select name="type"><?php { $selarray = array( 'url' => array(
    'Web Site Translation','' ), 'lu' => array( 'Dictionary Lookup',
    '' ), 'text' => array( 'Text Translation','' ), ); foreach
    ...rest of the code omitted...
</select><br /> <input type="image" value="doit" src="i/translate_en.png" /> </p> In this case the selected option is determined by a template variable, and so the option list is generated dynamically, if the selected item was instead a static selection, the option list would have been written out into the compiled template as pure html code. I find this is working well for me. If you (or anyone else) are interested in the plugin, then send me an email and I'll email it to you directly (so that the whole list doesn't have to receive it). Regards, Brodie. Thomas Lamy wrote:
Hi, I'm using smarty for 3 weeks now, and have only an idea of how I would create a site with internationalisation. I'd like to have different levels of i18n: words/phrases (like table headings or input labels), and text blocks (about more than 1 sentence worth of text). I have a similar project (single language) up and running for 2 years, and get about 200 SQL queries/sec peak, so adding addition load by using a (relationsl) database is not really an option. I thought of implementing the words/phrases thing with simple PHP arrays, and text blocks using some smart include() function, which either includes the "right" template (say "login_en.tpl"), and, if that file doesn't exist, fall back to include a generic template (eg "login.tpl"). I'm not really sure if my approach is really The Right Thing (tm), and how to do that with smarty (especially auto-including language templates with fallback), by not breaking caching. Any help out there? Thomas


« previous php.smarty.general (#6181) next »