Chapter 12. Internationalization

One of the holy grails of web development is internationalization (usually shortened to i18n), especially as the industry matures and needs to cover several different markets and countries. Often you would like to have the same website translated into different languages, and that’s the subject of this chapter.

So while this is a pretty simple objective, we do face some challenges on how to properly design the architecture of your web application while using Smarty. We have two different types of requirements for this:

  • Translation of an entire set of templates into different languages, while keeping the templates the same. This is a perfect example for a web application, since you want to deliver the same features to all of your users.
  • Translation of a complete set of templates, and changing a few of the templates for some of the different languages. That usually happens for websites that require some changes to a few pages, like special promotions that are only supposed to happen on a specific country, and so on.

We will go over the first requirement over the next few sections. You should be able to extend it pretty easily to be able to override any of the default translated templates with a customized one later on.

Translation Infrastructure: Gettext

The gold standard for the i18n community, as far as UNIX (and consequently PHP) is concerned, is the use of the Gettext project. This is a set of GNU tools designed to help translate text strings on the fly, from one language to another.

Gettext works by extracting text strings from source code files, and storing them into message catalogs. These catalog files are stored into a directory structure that is standardized, and the translated string is looked up at run time. If the translated text string is not found, or doesn’t have a translation available yet, the original one will be used instead.

So here’s an overview of the workflow:

  • Extract the text strings from source code into a message catalog file, named messages.po by default.
  • Edit the messages.po file to translate the text strings into another language.
  • When done, convert the modified messages.po into a binary file, called messages.mo by default, that the Gettext extension will understand and use.
  • Set up the PHP code to use the appropriate translation file, by using the Gettext functions.

We will go over the details of using Gettext with PHP over the next few sections.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.88.249