À l’heure de la mondialisation, l’internationalisation s’impose comme un enjeu essentiel. Elle implique la gestion de nombreux détails susceptibles de devenir de vrais casse-tête : jeux de caractères, changement de langue, gestion des monnaies, synchronisation des horloges, etc. Prévenir plutôt que guérir, voilà pourquoi il est essentiel de penser en amont votre internationalisation.
Exercice 1
1) Créez le fichier /www/htdocs/sitezend_avance/application/config/traduction.tmx qui contiendra les traductions suivantes au format tmx :
Bonjour : Bonjour (fr), Buenos dias (es), Hello (en)
Exemple de traduction : Traduction example (en)
2) Dans la méthode indexAction() du contrôleur index, affichez le texte Bonjour traduit en anglais.
Correction de l'exercice 1
$ nano /www/htdocs/sitezend_avance/application/config/traduction.tmx
<?xml version="1.0" ?> <!DOCTYPE tmx SYSTEM "tmx14.dtd"> <tmx version="1.4"> <header creationtoolversion="1.0.0" datatype="winres" segtype="sentence" adminlang="fr-fr" srclang="fr-fr" o-tmf="abc" creationtool="XYZTool" /> <body> <tu tuid='Bonjour'> <tuv xml:lang="fr">Bonjour <tuv xml:lang="es">Buenos dias <tuv xml:lang="en">Hello </tu> <tu tuid='Exemple de traduction'> <tuv xml:lang="en">Traduction example </tu> </body> </tmx>
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEit1HWWIpUCQp0j9FFRrwsdv-zdD4KwS07-rEw4okPpLwpy3kBfpsiStb2tvhxxhyzKY1MwaqtwetLe3lPJQlbo7spXMN2pOd_KHwqvSavGx0WWrTZQxr5b484YI1jey5bbIP3bJFIUbVk/s320/090.jpg)
On déclare notre fichier de traduction dans notre bootstrap en créant la fonction _initTrad() :
$ nano /www/htdocs/sitezend_avance/application/Bootstrap.php
protected function _initTrad () { // Déclaration de l’objet translate $translate = new Zend_Translate('tmx', APPLICATION_PATH.'/config/traduction.tmx', 'fr'); // Enregistrement explicite de la langue courante $translate->setLocale('fr'); Zend_Registry::set('trad', $translate); }
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQILo8xXyBpCm7vk0O4V2GDyXy8taYhj3uajH2jOwzjp7F0a1988Y1y5EXwwSfh4jv0K-l7XSgAxKmWWAD1Z2gYHjvOtKtrKNUySn1ehIFQNGjRB_oea9zrtrKvYpBg9kfDGHttBhKjfs/s320/091.jpg)
Retournons dans notre contrôleur index :
$ nano /www/htdocs/sitezend_avance/application/controllers/IndexController.php
Dans la méthode init() on récupère l'objet trad créé dans le bootstrap :
public function init() { $this->translate = Zend_Registry::get('trad'); }
Et dans notre méthode indexAction() on affiche la traduction
public function indexAction() { echo $this->translate->_('Bonjour'); }
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLoIX5eiX1Uk3Fff3a_k1YsNKapfC4flYtqty_WvkdqREZ_0Aq1Aci5wTyN4ZvhuI1BX-EbXJKrlvGRdWZ2hmhRE6NlAgoUGhT9GmRlbye3HD5oYxYijI7Cuj1JA5Q3cdnWesDZMgZtYc/s320/092.jpg)
On affiche notre site et on devrait obtenir le résultat suivant :
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1IRF2tqYNnGmQ0sY_yvlyPMVaqjF0DfgUBZwBv2p7bNTx8kIklqKGrcbhf7J2PybEJ0gfrl6vO1FM3YizEe2TpIflJ9W5cuyZDJ79kxF_jxFw3gUP2in8btdJdXlZQUPgFEHSYh0fO24/s320/093.jpg)
Il est vrai que cet exemple n'est pas le plus parlant, revenons à notre bootstrap pour la méthode _initTrad() que nous avons créé au dessus.
Et on change :
$translate->setLocale('fr');
pour :
$translate->setLocale('en');
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTTr1WRqh_lmL5F0-UdbH27En-EZUHcN8YpnPCi3vmHGIuYb7f36AQbS0jxXNWPFFaJ8zcl7pRg9ud56BzN3b9UjkiLdQqjnx_f3lxvOWAc7V7zJuEkfBp4JyGMFz9Q4vcRZIvniBk8AQ/s320/095.jpg)
Et on recharge notre page :
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsrPFjDA2KFWLjY6SfrPHyBi5g2Ez2kuYPOp63p-kH2mpIxtIcYTiYgd5CAGxHUtuwWXeXMTyVySZSmynEXXL3q8QVeAKJ4wkbPnDj3A3NDiapLl-QnGWeyJ3ZELlTdOgxFl4MV_QjQvc/s320/094.jpg)
Notre chaîne est correctement traduite en anglais.
Aucun commentaire:
Enregistrer un commentaire