|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
andreas Smarty Pro
Joined: 22 Apr 2003 Posts: 106
|
|
Back to top |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Wed Apr 23, 2003 1:59 am Post subject: |
|
|
How I did it was I wrote up a function to load strings from an XML file much like how Gettext does with .po files. And I made a block called {translate}{/translate} so everything between the block is searched for in the XML file and if found it will be replaced with the translated string. If you want I can post up the code I used to do this. _________________ "Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane |
|
Back to top |
|
andreas Smarty Pro
Joined: 22 Apr 2003 Posts: 106
|
|
Back to top |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Wed Apr 23, 2003 9:13 am Post subject: |
|
|
Well what I did was you only parse the XML one per page request that way you cut down on overhead alot, and parsing XML with PHP is fairly fast.[php:1:b5ee9eb4ee]<?php
$xml = array();
function smarty_t($params, $string, &$smarty) {
foreach($params as $key => $value) {
$params["%$key"] = $value;
unset($params[$key]);
}
print(t($string, $params));
}
function t($string, $args = array()) {
global $xml;
if(empty($xml)) {
if(file_exists("locale/{$_GET['lang']}.xml")) {
$xml = getXmlTree("locale/{$_GET['lang']}.xml");
} else {
return strtr($string, $args);
}
}
foreach($xml[0]['children'] as $tag) {
if($tag['tag'] == "MESSAGE") {
if($tag['children'][0]['value'] == $string) {
if($tag['children'][1]['value'] != "") {
return strtr($tag['children'][1]['value'], $args);
}
}
}
}
return strtr($string, $args);
}
function getChildren($vals, &$i) {
$children = array();
if(!isset($vals[$i]['attributes'])) {
$vals[$i]['attributes'] = "";
}
while(++$i < count($vals)) {
if(!isset($vals[$i]['attributes'])) {
$vals[$i]['attributes'] = "";
}
if(!isset($vals[$i]['value'])) {
$vals[$i]['value'] = "";
}
switch ($vals[$i]['type']) {
case 'complete':
array_push($children, array('tag' => $vals[$i]['tag'], 'attributes' => $vals[$i]['attributes'], 'value' => $vals[$i]['value']));
break;
case 'open':
array_push($children, array('tag' => $vals[$i]['tag'], 'attributes' => $vals[$i]['attributes'], 'children' => getChildren($vals, $i)));
break;
case 'close':
return $children;
break;
}
}
return $children;
}
function getXmlTree($file) {
$data = implode("", file($file));
$xml = xml_parser_create();
xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($xml, $data, &$vals, &$index);
xml_parser_free($xml);
$tree = array();
$i = 0;
array_push($tree, array('tag' => $vals[$i]['tag'], 'attributes' => $vals[$i]['attributes'], 'children' => getChildren($vals, $i)));
return $tree;
}
?>[/php:1:b5ee9eb4ee]thats the code to parse through a XML file with the function to use for the smarty block. Below is a XML file to use. Code: | <?xml version="1.0" encoding="utf-8" ?>
<locale lang="de">
<header>
<project>Project Name</project>
<create>2003-01-12 17:24-0500</create>
<revise>2003-01-15 19:15-0500</revise>
<trans>Your Name - Your Email</trans>
<lang>Deutsch</lang>
</header>
<message>
<id>Hello</id>
<string>Guten Tag</string>
</message>
<message>
<id>%u: How are you?</id>
<string>%u: Wie gates es Ihnen?</string>
</message>
</locale> |
To use the above in just PHP code with that XML fil, which you put in a dir called locale and name de.xml, then pass lang=de to your script or however you want to specify it.[php:1:b5ee9eb4ee]<?php print(t("Hello)); ?>[/php:1:b5ee9eb4ee]if your locale is set to english or the string doesnt exist in the file that isn't replaced, but if that string is in your locale then its replaces so germans would see Guten Tag. Also if say you need to insert small things like a username or something into the string but dont want to break it up (because god knows not every language puts verbs in the second position like english), then you pass an array with replace values that are passed to strtr(). So[php:1:b5ee9eb4ee]<?php print(t("%u: How are you?", array("%u" => $username))); ?>[/php:1:b5ee9eb4ee]its a bad example of how useful it is but I found it extreamly useful when my translators started sending back the files saying they couldn't translate certin sentences. Now on to using it in Smarty what this is all about [php:1:b5ee9eb4ee]<?php
$smarty->register_block("translate", "smarty_t", false);
$smarty->register_block("trans", "smarty_t", false);
?>[/php:1:b5ee9eb4ee]you could use the above with caching but a much better idea would probably be to use the locale as part of the cache id in which case remove the third parameter or just change it to true
that will register the translate and trans blocks so in case you feel lazy that day (I did). Then use it like below in your templates Code: |
{* Smarty *}
<html>
<head>
<title>My Page</title>
<body>
<b>{translate}Hello{/translate}</b><br />
<i>{translate u=$username}%u: How are you?{/translate}</i>
</body>
</html> | I picked XML files because I know they can effectively store information in UTF-8 so multiple character sets wouldn't be a huge problem. I know this method has its down sides, like having to copy over every english phrase in your template to a XML file to use for translating, but its the method I found most effective... and at one time I wrote a parser to pull all the strings but I have but lost it. <selfad>BTW all this code I wrote for my Sage project </selfad> Hope this helps
PS My german is a bit rusty so please excuse it[i] _________________ "Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane
Last edited by AZTEK on Wed Mar 17, 2004 11:40 am; edited 9 times in total |
|
Back to top |
|
joscha Smarty Rookie
Joined: 18 Apr 2003 Posts: 8 Location: Metzingen, Germany
|
Posted: Wed Apr 23, 2003 9:58 am Post subject: |
|
|
I have another architecture:
Code: | MySQL with language table <-> language data table
|
Smarty template with {lang MESSAGE_WHATEVER} tags in it
|
PHP with language architecture, which gets text belogning to the current site in the correct language
|
prefilter.lang {lang MESSAGE_WHATEVER} is replaced by my language string for MESSAGE_WHATEVER out of the database)
|
smarty->display with correct compile and caching flags (language code)
|
this way it is possible to store placeholders in the database, e.g.:
"Hello {$name} what a nice day!"
because the prefilter is executed before interpretation of the template.
greets,
Joscha |
|
Back to top |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Wed Apr 23, 2003 10:34 am Post subject: |
|
|
Thats how I was going to do it but I had problems in the past with mysql not liking UTF-8 _________________ "Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane
Last edited by AZTEK on Wed Apr 23, 2003 11:19 am; edited 1 time in total |
|
Back to top |
|
andreas Smarty Pro
Joined: 22 Apr 2003 Posts: 106
|
|
Back to top |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Wed Apr 23, 2003 11:19 am Post subject: |
|
|
Pear has the problem of being rather slow and also not being installed always. I wrote mine with idea its to be portable because its part of my CMS. Also I wrote mine in the begining when I didn't use Smarty at all so it was origionaly coded for regular output using print. The Smarty block thing was just a convience. I send the translators the english versions of the XML files with no text in the string tag and have them translate it into there language and tehn save it in UTF-8 format so that the characters are the same across all charsets. And yes I know my german is horible . I do like that multilanguage class but to write a template for each language and use it as a theme system like I did without knowing exactly what your saying would be impracticle. I wanted people to be able to submit themes (mainly a Smarty template and a class) in english without cryptic replace filters and have it work from the already made language files. _________________ "Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane |
|
Back to top |
|
andreas Smarty Pro
Joined: 22 Apr 2003 Posts: 106
|
|
Back to top |
|
victoryoalli Smarty n00b
Joined: 22 Apr 2003 Posts: 4 Location: Mexico
|
Posted: Wed Apr 23, 2003 4:38 pm Post subject: GetText |
|
|
I was asking myself the same question about multilingual. I decided to use gettext.
It is really easy to use it and to mantain, once you understand it.
And the most important thing about it. It is that if I decide to use the same translations in another programming language. I will not have a problem. Well I guess it is the same case with xml.
I found this documentation very usefull.
http://www.onlamp.com/pub/a/php/2002/06/13/php.html
http://laurent.bedubourg.free.fr/bagpack/GetText/doc/index.html
Here you have a little more of something to think about. I hope it helps.[/b] |
|
Back to top |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Wed Apr 23, 2003 7:18 pm Post subject: |
|
|
The reason I made my own is because I couldnt get gettext to work and even then it would not work on all platforms _________________ "Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane |
|
Back to top |
|
katana Smarty Rookie
Joined: 17 Apr 2003 Posts: 26 Location: France
|
Posted: Thu Apr 24, 2003 7:47 am Post subject: |
|
|
And what's your method for multilingual dynamic data? In the database I mean. Do you create one field per language or do you use another method ? |
|
Back to top |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Thu Apr 24, 2003 8:52 am Post subject: |
|
|
One file per language named by letter code and then the layout is alot like an xml version of gettext _________________ "Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane
Last edited by AZTEK on Thu Apr 24, 2003 11:12 am; edited 1 time in total |
|
Back to top |
|
Sri_Lumpa Smarty Rookie
Joined: 24 Apr 2003 Posts: 8 Location: Paris, FRANCE
|
Posted: Thu Apr 24, 2003 10:16 am Post subject: |
|
|
Hi,
An easy way to make a multi-language interface with Smarty is to use a config file (in the configs directory) called for example "localization.conf" and containing :
[FR]
title = "Mon titre en français"
[EN]
title = "My english title"
...
At the top of your template, you just have to add this line :
{config_load file="localization.conf" section=$currentLanguage}
In your PHP code, you have somewhere a assign like this :
$_SESSION["MySmarty"]->assign_by_ref("currentLanguage", $_SESSION["language"]);
So, you can change your session language value by using a menu or something else :
$_SESSION["language"] = "FR" or $_SESSION["language"] = "EN"
When the page is displayed, use a cache ID like $ID.$_SESSION["language"] where $ID is like you want (for example, the name of the template) to make a difference between template of different language. |
|
Back to top |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
Posted: Thu Apr 24, 2003 12:01 pm Post subject: |
|
|
So I am using http://smarty.incutio.com/?page=SmartyMultilanguageSupport as meantioned above. Ehh... I'm using it because I wrote the script
It works quite well for me and it's fast. If somebody needs some help just post your questions in the forum.
bye,
Andre |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|