Using Translation Adapters

The next step is to use the adapter within your code.

Example #1 Example of single-language PHP code

  1. print "Example\n";
  2. print "=======\n";
  3. print "Here is line one\n";
  4. print "Today is the " . date("d.m.Y") . "\n";
  5. print "\n";
  6. print "Here is line two\n";

The example above shows some output with no support for translation. You probably write your code in your native language. Generally you need to translate not only the output, but also error and log messages.

The next step is to integrate Zend_Translate into your existing code. Of course it is much easier if you had already written your code with translation in mind, than changing your code afterwards.

Example #2 Example of multi-lingual PHP code

  1. $translate = new Zend_Translate(
  2.     array(
  3.         'adapter' => 'gettext',
  4.         'content' => '/my/path/source-de.mo',
  5.         'locale'  => 'de'
  6.     )
  7. );
  8. $translate->addTranslation(
  9.     array(
  10.         'content' => '/path/to/translation/fr-source.mo',
  11.         'locale'  => 'fr'
  12.     )
  13. );
  14.  
  15. print $translate->_("Example") . "\n";
  16. print "=======\n";
  17. print $translate->_("Here is line one") . "\n";
  18. printf($translate->_("Today is the %1\$s") . "\n", date('d.m.Y'));
  19. print "\n";
  20.  
  21. $translate->setLocale('fr');
  22. print $translate->_("Here is line two") . "\n";

Now let's take a deeper look into what has been done and how to integrate Zend_Translate into your own code.

Create a new Zend_Translate object and define the base adapter:

  1. $translate = new Zend_Translate
  2.     array(
  3.         'adapter' => 'gettext',
  4.         'content' => '/path/to/translation/source-de.mo',
  5.         'locale'  => 'de'
  6.     )
  7. );

In this example we chose the Gettext Adapter. We place our file source-de.mo into the directory /path/to/translation. The gettext file will have German translation included, and we also added another language source for French.

The next step is to wrap all strings which are to be translated. The simplest approach is to have only simple strings or sentences like this:

  1. print $translate->_("Example") . "\n";
  2. print "=======\n";
  3. print $translate->_("Here is line one") . "\n";

Some strings do not needed to be translated. The separating line is always a separating line, even in other languages.

Having data values integrated into a translation string is also supported through the use of embedded parameters.

  1. printf($translate->_("Today is the %1\$s") . "\n", date("d.m.Y"));

Instead of print(), use the printf() function and replace all parameters with %1\$s parts. The first is %1\$s, the second is %2\$s, and so on. This way a translation can be done without knowing the exact value. In our example, the date is always the actual day, but the string can be translated without the knowledge of the actual day.

Each string is identified in the translation storage by a message ID. You can use message IDs instead of strings in your code, like this:

  1. print $translate->_(1) . "\n";
  2. print "=======\n";
  3. print $translate->_(2) . "\n";

But doing this has several disadvantages:

You can not see what your code should output just by viewing your code.

Also you will have problems if some strings are not translated. You must always keep in mind how translation works. First Zend_Translate checks whether the specified language has a translation for the given message ID or string. If no translation string has been found it refers to the next lower level language as defined within Zend_Locale. So "de_AT" becomes "de" only. If there is no translation found for "de" either, then the original message is returned. This way you always have an output, even in case the message translation does not exist in your message storage. Zend_Translate never throws an error or exception when translating strings.

Translation Source Structures

Your next step is to create the translation sources for the languages you want to translate. Every adapter is created its own way as described here, but there are common features applicable for all adapters.

You have to decide where to store your translation source files. Using Zend_Translate you are not restricted in any way. The following structures are preferable:

  • Single structured source

    1. /application/
    2. /languages/
    3. /languages/lang.en
    4. /languages/lang.de
    5. /library/

    Positive: all source files for every languages are stored in one directory. No splitting of related files.

  • Language structured source

    1. /application/
    2. /languages/
    3. /languages/en/
    4. /languages/en/first.en
    5. /languages/en/second.en
    6. /languages/de/
    7. /languages/de/first.de
    8. /languages/de/second.de
    9. /library

    Positive: Every language is stored in their own directories. Easy translation, as every language team has to translate only one directory. Also the usage of multiple files is transparent.

  • Application structured source

    1. /application/
    2. /application/languages/
    3. /application/languages/first.en
    4. /application/languages/first.de
    5. /application/languages/second.en
    6. /application/languages/second.de
    7. /library/

    Positive: all source files for every language are stored in one directory. No splitting of related files.

    Negative: having multiple files for the same language can be problematic.

  • Gettext structured source

    1. /application/
    2. /languages/
    3. /languages/de/
    4. /languages/de/LC_MESSAGES/
    5. /languages/de/LC_MESSAGES/first.mo
    6. /languages/de/LC_MESSAGES/second.mo
    7. /languages/en/
    8. /languages/en/LC_MESSAGES/
    9. /languages/en/LC_MESSAGES/first.mo
    10. /languages/en/LC_MESSAGES/second.mo
    11. /library/

    Positive: existing gettext sources can be used without changing structure.

    Negative: having sub-sub directories may be confusing for people who have not used gettext before.

  • File structured source

    1. /application/
    2. /application/models/
    3. /application/models/MyModel.php
    4. /application/models/MyModel.de
    5. /application/models/MyModel.en
    6. /application/controllers/
    7. /application/controllers/MyController.php
    8. /application/controllers/MyController.de
    9. /application/controllers/MyController.en
    10. /library/

    Positive: translation files are localted near their source.

    Negative: too many and also small translation files result in being tedious to translate. Also every file has to be added as translation source.

Single structured and language structured source files are most usable for Zend_Translate.

So now, that we know which structure we want to have, we should create our translation source files.

blog comments powered by Disqus