Hi abiko
Quote:
Originally Posted by abiko
Hello !
I am wondering how to make a multilingual site?
I figured I'd use gettext & Zend_Translate
|
I can't help with the MySQL stuff I'm afraid but I can help with Zend_Translate
Below is an example copy/pasted from one of my sites that uses Zend_Translate (and the rest of the Zend Framework).
I use TMX rather than gettext because you can use any XML or text editor to edit it rather than the hellish tool that is PoEdit, and it is looking likely to become the open standard in translation
-------------
First we load and setup the Zend_Translate class - in my application, this is in the bootstrap but if you're not using the Zend Framework MVC components then put it whether you wish:
PHP Code:
Zend_Loader::loadClass('Zend_Translate');
$translate = new Zend_Translate('tmx', './application/languages/language.tmx', 'en');
Zend_Registry::set('language', $translate);
In this code, I load my TMX file (language.tmx) and tell Zend_Translate that I want to use the English ('en') language.
Here is a snippet from my language.tmx so you can see what the format is:
Code:
<?xml version="1.0" ?>
<!DOCTYPE tmx SYSTEM "tmx14.dtd">
<tmx version="1.4">
<header creationtool="CTmx" creationtoolversion="1.0" datatype="winres" segtype="sentence" adminlang="en-us" srclang="en-us" o-tmf="txt">
</header>
<body>
<tu tuid='pleaseEnterYourEmailAddress'>
<tuv xml:lang="en">
<seg>Please enter your email address</seg>
</tuv>
<tuv xml:lang="de">
<seg>...German Translation Here...</seg>
</tuv>
</tu>
<tu tuid='emailAddress'>
<tuv xml:lang="en">
<seg>Email Address</seg>
</tuv>
<tuv xml:lang="de">
<seg>...German Translation Here...</seg>
<tuv>
</tu>
And it goes on like that. Each <tu> block contains the translations for each string. In mine, it has the English strings first, then the German strings (well, will have

)
The next step is to fetch the 'language' object from Zend_Registry in your controllers (again, if you aren't using the Zend Framework MVC components, adjust as needed

)
In my controllers init() method I have:
PHP Code:
$this->language = Zend_Registry::get('language');
If you are also using Zend_View, you could use something like:
PHP Code:
$this->view->language = Zend_Registry::get('language');
This will make the language available in your views.
So, at this point, you have all of your translated strings from language.tmx, in your selected language (English in this example) available in $this->language.
To go about displaying translated text to your user, you need to use the _() method (yes, the method is just an underscore - it aliases to translate() but _() is shorter to type

)
For example, using our language.tmx snippet above, if I wanted to echo out "Email Address" and "Please enter your email address", I would use:
PHP Code:
echo $this->language->_('emailAddress');
echo $this->language->_('pleaseEnterYourEmailAddress');
If you have another look at the TMX snippet above you'll see that we are using the tuid property to get the language string.
If you wanted to echo the German translations for "emailAddress" and "pleaseEnterYourEmailAddress", you would just change the language specified when you created the Zend_Translate object.
For example, to load our German translation instead of English:
PHP Code:
$translate = new Zend_Translate('tmx', './application/languages/language.tmx', 'de');
Hopefully that wall of text made sense and will get you started in the right direction
Alan