Bootstrap Zend_Translate

This entry is part 2 of 4 in the series Work­ing with Zend_Translate and Poedit

A recur­ring prob­lem for site devel­op­ers is imple­ment­ing a solid way to cre­ate and main­tain mul­ti­lin­gual sites, this arti­cle series is my fee­ble attempt to guide you through how to quickly imple­ment the Zend_Translate in an Zend Frame­work 1.9.x site.

The pro­ce­dures and best prac­tices for this is unfor­tu­nately like train­ing a dog, every­one has a dif­fer­ent way of doing it and an opin­ion, so the meth­ods and code I show here are taken out of appli­ca­tions that are run­ning in pro­duc­tion so if you have a bet­ter way of doing it please feel free to comment!.

I usu­ally use poedit a get­text edi­tor which is avail­able for most plat­forms to cre­ate my trans­la­tion files, and after some ini­tial con­fig­u­ra­tion of the cat­a­log paths so it can see your source files please see Part 1 of this arti­cle series.

The boot­strap below looks for the lan­guage spe­cific get­text .mo files in /application/languages/ for exam­ple /application/languages/sv_SE.mo

Bootstrap.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
protected function _initTranslate() {
  // We use the Swedish locale as an example
  $locale = new Zend_Locale('sv_SE');
  Zend_Registry::set('Zend_Locale', $locale);

  // Create Session block and save the locale
  $session = new Zend_Session_Namespace('session');
  $langLocale = isset($session->lang) ? $session->lang : $locale;
       
  // Set up and load the translations (all of them!)
  $translate = new Zend_Translate('gettext', APPLICATION_PATH . DIRECTORY_SEPARATOR .'languages', $langLocale,
     array('disableNotices' => true));

  //$translate->setLocale($langLocale); // Use this if you only want to load the translation matching current locale, experiment.
       
  // Save it for later
  $registry = Zend_Registry::getInstance();
  $registry->set('Zend_Translate', $translate);
}

Now when you use state­ments like

1
<?php $this->translate('Contact Admin'); ?>

in your layout.phtml or view.phtml files it will be picked up by poedit and you will be pre­sented with a string “Con­tact Admin” to trans­late, in my case i’ll just enter “Kon­takta Administratören”.

There is some debate on what to put in the trans­late strings as iden­ti­fiers, I per­son­aly pre­fer the actual term to trans­late in a base lan­guage, in this case Eng­lish instead of some con­vo­luted “IDS0001” type strings.

Poedit will keep track of changes, i.e if I would change the “Con­tact Admin” to “Con­tact us” it will tell you on syn­chro­niza­tion that “Con­tact Admin” dis­ap­peared and a new trans­la­tion is required for “Con­tact us”. It’s quite easy to send those strings to your translators.

Thats it for today.




Series Nav­i­ga­tionCon­fig­ur­ing Poedit for Zend Frame­work ProjectsHow to make POEdit detect source strings in Zend Framework

Tags: , , , , , ,

1 Response to "Bootstrap Zend_Translate"

  • max says:
Leave a Comment

*

Get Adobe Flash player