Home / Blog / Internationalization "how to" for the 5 most popular PHP frameworks

Internationalization "how to" for the 5 most popular PHP frameworks

Developer resources
Helmut Juskewycz
CEO & Founder of LingoHub

Last updated

7/23/2013

Read time

12 min

Best for

Developers

Internationalization with 5 popular PHP frameworks

As discussed in the previous articles on PHP internationalization, PHP provides native support for string translation using gettext and PHP arrays. These can be used in any PHP project. Additionally, some popular PHP frameworks offer their own way of string translation.

In this article, I want to provide a brief summary of the internationalization process with five of the most popular PHP frameworks currently out there. In this How-To you will read about using CodeIgniter, CakePHP, Zend, Yii and Symfony. If you would like us to focus on any of these (or some other PHP framework of your choice) in a future article of its own, please leave a comment.

CodeIgniter framework

code igniter

CodeIgniter Language Class provides functions to retrieve language files and lines of text for purpose of internationalization. The language files are located within the language directory. CodeIgniter automatically looks for the language directory in two locations, first within the “languages” subdirectory of the application, and then, if not found, within the “languages” subdirectory of the CodeIgniter system directory. Each language is stored in its own subdirectory. For example, English language files can be stored either in

 system/language/english

or

 application/language/english

Language resource file names must end with _lang.php. Within the file, a line of text is assigned to a $lang array in each line this way:

$lang[‘language_key’] = “The actual message to be shown”;

Recommended practice is to prefix all the keys with the name of the file in which they are located, in order to avoid collision with similar keys in different files. For example, the error message that informs the user about the missing email address that is located in the error_lang.php could have this key: “error_missing_email”.

Before fetching lines from any file, it has to be loaded first:

$this->lang->load('filename', 'language');

Filename is the name of the file without the extension. So for the previous example with error_lang.php, the filename to supply to the load function would simply be “error”. The language specifies the set that contains the file. If the second parameter is missing, the default language set in the application/config/config.php is used instead.

After the language file is loaded, a line of text can be fetched and echoed like this:

echo $this->lang->line('language_key');

Language files that are used globally can be auto-loaded by CodeIgniter. Auto loading can be set up in the autoload array in application/config/autoload.php.

Further reading:

CakePHP framework

cake-logo-100x100

CakePHP uses its own implementation of Gettext. In the previous blog article, I described how Gettext (.po) files can be created.

Functions used in CakePHP for application internationalization include:

  • __($string_id[, $formatArgs])
    Returns the translated string from the current domain of the loaded language for the supplied ID. Translation strings are treated as format strings for sprintf(), and additional arguments can be provided to replace placeholders within the string.

  • __d($domain, $msg, $args = null)
    Allows overriding the current domain for a single message lookup.

  • __n($singular, $plural, $count, $args = null)
    Returns the correct plural form of a message identified by $singular and $plural based on the value of $count.

  • __dn($domain, $singular, $plural, $count, $args = null)
    Overrides the current domain for a single plural message lookup and returns the appropriate plural form from the specified domain.

A complete list of global functions, including those used for localization, can be found in the CakePHP documentation.

Further Reading

  • Previous blog article on PHP internationalization using Gettext

  • Internationalization & Localization with CakePHP

Zend Framework

zend-framework logo1

The Zend framework offers its own complete solution for localization and internationalization. It includes support for both gettext as well as some other resource file formats.

Available adapters for Zend_translate (a library used within the framework to handle translation) are:

  • Array (use >PHP arrays)

  • Csv (use comma separated (*.csv/*.txt) files)

  • Gettext (use binary gettext (*.mo) files)

  • Ini (use simple INI (*.ini) files)

  • Tbx (use termbase exchange (*.tbx/*.xml) files)

  • Tmx (use tmx (*.tmx/*.xml) files)

  • Qt (use qt linguist (*.ts) files)

  • Xliff (use xliff (*.xliff/*.xml) files)

  • XmlTm (use xmltm (*.xml) files)

Further reading:

Symfony framework

symphony

In Symfony, translation of text is done through the translator service. To translate a message, the trans() method is used, following this process:

  • The locale is determined (from the request or a session variable).

  • A catalog of translated messages is loaded from the translation resource files defined for that locale.

  • The requested translation is returned.

The Symfony framework provides support for these loaders by default:

  • xliff: XLIFF file

  • php: PHP file

  • yml: YAML file

However, custom loaders can be created by implementing the LoaderInterface interface.

Further reading:

Yii Framework

yii

Yii Framework

With the Yii framework, message translation is performed using the Yii::t() method.

When translating a message, its category must be specified because the same message may require different translations depending on the context. For example, the category yii is reserved for messages used by the Yii framework core.

Parameter placeholders within messages are replaced with actual values when calling Yii::t(). For example:

Yii::t(
    'app',
    'Path alias "{alias}" is redefined.',
    array('{alias}' => $alias)
);

In this example, the {alias} placeholder is replaced with the value of $alias.

Message Sources

Translated messages are stored in repositories known as message sources.

A message source is represented by an instance of CMessageSource or one of its subclasses. When Yii::t() is called, Yii searches the configured message source and returns the translated version of the message if one exists.

Yii includes several built-in message source implementations:

CPhpMessageSource

Translations are stored as key-value pairs in PHP arrays.

  • The original message serves as the key.

  • The translated message serves as the value.

  • Each file contains translations for a specific message category.

  • Files are organized by locale within directories named after the locale identifier.

  • These locale directories are located under the path specified by basePath.

CGettextMessageSource

Translations are stored using GNU Gettext files (.po and .mo).

For more information on Gettext, see our previous tutorial on PHP internationalization with Gettext.

CDbMessageSource

Translations are stored in database tables.

Custom Message Sources

You can create your own message source implementation by extending the CMessageSource class.

Further Reading

  • Yii Framework Documentation – Internationalization

Conclusion

These popular PHP frameworks are only some of the many approaches to internationalization.

Which frameworks and localization solutions do you use? What has your experience been like?

In this series, we’ve explored several approaches to preparing applications for localization, and future tutorials will cover additional programming languages, frameworks, and resource file formats.

Many of these recommendations are based on customer experiences and lessons learned while developing LingoHub. We look forward to hearing your thoughts and experiences in the comments.

Related articles