Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
best way to build a multi-language site with smarty
Goto page 1, 2, 3 ... 13, 14, 15  Next
 
Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
andreas
Smarty Pro


Joined: 22 Apr 2003
Posts: 106

PostPosted: Wed Apr 23, 2003 12:51 am    Post subject: best way to build a multi-language site with smarty Reply with quote

Hi!

I'm not sure which is the best way to build a multi-language-site with smarty. If I want to have my site in english, spanish and german language, how could I do this? I think I have to store the contend-strings in a special language direktory("lang") should I create a language-table for each language? Or one table per language? One table per template or one at all?

For example I have something like this:

variable | english | german
---------------------------------------------------------------
nice_day | what a nice day! | was für ein schöner Tag!

How/where should I parse this? How should I assign this to my template? What is the best way to do this?

Thank you very much!

kind regards,
Andreas


Last edited by andreas on Wed May 05, 2010 10:33 pm; edited 1 time in total
Back to top
View user's profile Send private message
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 236
Location: Purdue University

PostPosted: Wed Apr 23, 2003 1:59 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
andreas
Smarty Pro


Joined: 22 Apr 2003
Posts: 106

PostPosted: Wed Apr 23, 2003 8:03 am    Post subject: Reply with quote

Thank you! Would be nice if you could post the code. What about performance? Is this method fast? What about an output-filter, this should be not so good, is it? Do you save all your language-strings in one global XML-file? How does it look like? Is one XML-file such a good solution? Because it becomes very big, and parsing XML with php is not that fast, or is it?
Or is there a nice plugin to do this?

________
uhwh warehouse


Last edited by andreas on Fri Feb 04, 2011 8:59 am; edited 2 times in total
Back to top
View user's profile Send private message
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 236
Location: Purdue University

PostPosted: Wed Apr 23, 2003 9:13 am    Post subject: Reply with quote

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 Smile[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 Smile</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
View user's profile Send private message Visit poster's website
joscha
Smarty Rookie


Joined: 18 Apr 2003
Posts: 8
Location: Metzingen, Germany

PostPosted: Wed Apr 23, 2003 9:58 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 236
Location: Purdue University

PostPosted: Wed Apr 23, 2003 10:34 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
andreas
Smarty Pro


Joined: 22 Apr 2003
Posts: 106

PostPosted: Wed Apr 23, 2003 11:03 am    Post subject: Reply with quote

Thank you very much for these examples!

@AZTEK: "Wie gates es Ihnen?" is very close, you have to say "Wie geht es Ihnen?" Wink I hope you don't have problems to understand my english Wink

Your XML-version looks nice, perhaps I try it this way. But how do you or your translators work on these XML-files? I think just writing an HTML-Form and a small PHP-Script could do this job.

Another solution I found is that:

http://smarty.incutio.com/?page=SmartyMultilanguageSupport

What do you think about this? What I like here(usage-desc. after the code) is that you don't have to care about it in your scripts, because it is build into the smarty-class(extends...), but I like your XML-version(why did not use PEAR::XML?). Another advantage is that each language-version of a script is compiled seperatly, so it should be quite fast.

What do you think about it? Perhaps I will try to bring the advantages of both solutions together?!
________
volcano vaporizer


Last edited by andreas on Fri Feb 04, 2011 8:59 am; edited 2 times in total
Back to top
View user's profile Send private message
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 236
Location: Purdue University

PostPosted: Wed Apr 23, 2003 11:19 am    Post subject: Reply with quote

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 Smile. 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
View user's profile Send private message Visit poster's website
andreas
Smarty Pro


Joined: 22 Apr 2003
Posts: 106

PostPosted: Wed Apr 23, 2003 4:04 pm    Post subject: Reply with quote

Is PEAR always slow or just the XML-parser? Becaus I use some things like PEAR:Very HappyB, PEAR::mail and PEAR::error!

But my application will only run in my enviroment, so I can use what ever I want. So which way would you suggest? I think about rewriting the class I posted above, so that I can use XML for language-data. And perhaps I will change the tags from ## to something else.

This is just for one application, I don't need things like "themes"..., im looking for the best way to display all strings in different languages.

At the moment I'm not sure if I should use a block-function - as you do - or building a replacement-filter as mentioned above into smarty class. The second way seems to be a better application-design, doesn't it?

What would you suggest me?

________
herbal vaporizers


Last edited by andreas on Fri Feb 04, 2011 8:59 am; edited 2 times in total
Back to top
View user's profile Send private message
victoryoalli
Smarty n00b


Joined: 22 Apr 2003
Posts: 4
Location: Mexico

PostPosted: Wed Apr 23, 2003 4:38 pm    Post subject: GetText Reply with quote

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
View user's profile Send private message
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 236
Location: Purdue University

PostPosted: Wed Apr 23, 2003 7:18 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
katana
Smarty Rookie


Joined: 17 Apr 2003
Posts: 26
Location: France

PostPosted: Thu Apr 24, 2003 7:47 am    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 236
Location: Purdue University

PostPosted: Thu Apr 24, 2003 8:52 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Sri_Lumpa
Smarty Rookie


Joined: 24 Apr 2003
Posts: 8
Location: Paris, FRANCE

PostPosted: Thu Apr 24, 2003 10:16 am    Post subject: Reply with quote

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
View user's profile Send private message
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Thu Apr 24, 2003 12:01 pm    Post subject: Reply with quote

So I am using http://smarty.incutio.com/?page=SmartyMultilanguageSupport as meantioned above. Ehh... I'm using it because I wrote the script Wink

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks All times are GMT
Goto page 1, 2, 3 ... 13, 14, 15  Next
Page 1 of 15

 
Jump to:  
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
Protected by Anti-Spam ACP