RE: [SMARTY] using different languages in my Templates
| From: | Yuri Weseman | Date: | Fri, 20 Dec 2002 15:10:24 +0000 |
| Subject: | RE: [SMARTY] using different languages in my Templates | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-6916@lists.php.net to get a copy of this message | ||
Hi again!
Seems like we're the only ones on this list! :-)
Personally, I wouldn't connect translations to a single template. In a
larger site you'd probably use the same words/lines/paragraphs over and over
again. The way you suggest the data to be stored would result in a lot of
duplicate information.
Nevertheless, if you really want to do that, I'd probably store it like
this:
translations table:
key|translation|language
template_translation table:
template_name|key
This way you can do:
SELECT translation FROM translations LEFT JOIN template_translation ON
translations.key=template_translation.key WHERE
template_name="current_template_name" AND language="current_language"
Now you don't have the data duplication, but you can still select all
translations for a specific script.
However, I still don't like the fact that you have to specify which
translations are used in a specific template like that. It's actually a
double specification, because you first specify in your template which keys
you need, and then you need to store all that again in that
template_translation table and fetch it when smarty does it work.
That's why I wanted to extract this information from the template. Of course
2 regExes aren't the speediest way to do this. Perhapse there's a faster
way?
By the way: what's so bad about SELECT * FROM wordlist WHERE key in
(commaSeparatedKeys)? It's probably a lot faster than 80 separate queries...
I did some digging in the archive of this mailing list and here's what I
came up with:
"A variation on this theme that has proved very useful for me is
the following:
foo.tpl
{ config_load file=_foo.conf section=$lang }
{#copyright#}
_foo.conf
copyright = untranslated
[english]
copyright = Copyright 2002 All rights reserved
[spanish]
copyright = Copyright 2002 Todos los derechos reservados
[catala]
copyright = Copyright 2002 Todos los derechos sin traducir
foo.php
$smarty->assign("lang","spanish");
$smarty->display("foo.tpl");
Mucha suerte
Ubaldo"
This is a possible option when you want to use config files. But this has
the disadvantage of the data not being stored in a database (at least for me
this is a disadvantage... it's way more easy to create a webinterface to
edit translations when they're stored in a database).
In the past there was also a suggestion of using {translate lang=$LANG
en='en string' fr='fr string'}. But this has the disadvantage that all your
translations are even in your template file = massive duplicate data!
Another suggestion in the past:
"I'm using a couple of plugins developed by Alejandro Sarco
(ale@sarco.com.ar) 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."
That's all I could find on this subject... Hope it's helpfull to you.
Yuri
-----Original Message-----
From: akorthaus@web.de [mailto:akorthaus@web.de]
Sent: Friday, December 20, 2002 3:52 PM
To: Yuri Weseman
Cc: smarty-general@lists.php.net
Subject: Re: [SMARTY] using different languages in my Templates
Hi Yuri!
Thanks for your idea, but 2 things I don't like very much:
1. SELECT * FROM wordlist WHERE key in (commaSeparatedKeys)
2. parse the template twice by regEx.
What about my new idea (a mixture of my old an yours):
If the plugin translate gets the first call like that:
{translate key="key_to_translate}
It will call all data for database, the data is stored like this:
key|script_name|EN|NL|DE...
So I get a query like SELECT * FROM translate_table WHERE script_name =
'$script_name'
After that the script should generate an array with all the data in it.
So if the next {translate key="key_to_translate} comes, the data could
be taken from array.
Is it possible like that? What if I change something in my database, but
not in my template, could I somehow create a new template_c?
regards,
Andreas
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php