0

This is our concluding article on PHP internationalization frameworks and i18n prep tutorials. Our previous articles have covered the following aspects:

The last article in this series on PHP internalization will cover two more PHP frameworks that are frequently used: Laravel and FuelPHP. Thanks also to previous commenters that have expressed interest in covering Laravel in more detail.

PHP internationalization frameworks: The Laravel framework

laravel

Internationalization in the Laravel framework can be done by the use of the PHP array files, stored within app/lang directory. Language files for each language should be stored in their own subdirectory:

/app /lang /en days.php months.php /de days.php months.php /fr days.php months.php
The language files should return an array of keyed strings:

http://gist.github.com/bosskovic/6113848

The keys should be chosen carefully, because if the language line for the active language does not exist, the key will be returned. Laravel does not allow specifying a fall-back language.

The default language can be set in the app/config/app.php, and the active language can be changed using the App::setLocale method like this:

App::setLocale('de');

The lines can be retrieved from the language files with the get method of the Lang class. The argument passed to the get method consists of two segments, first one being the name of the language file, and the second one the key of the line that should be retrieved.

echo Lang::get('days.monday');

The presence of the translation for the specific key and active language can be checked with the has method:

if (Lang::has('days.monday')){ // }

Place-holders can be defined in the language files, and substituted with the dynamic values during the run time:

//messages.php  'Welcome, :username' ); ?>

The substitute values are passed to the get method in an array as a second argument:

echo Lang::get('messages.welcome', array('username' => $username));

A singular form can be separated from a plural form in the translations by the use of the "pipe" character:

 'There is one apple|There are many apples' ); ?>

The Lang::choice method can be used to retrieve the line:

echo Lang::choice('messages.apples', 10);

Laravel translator is powered by the Symfony Translation component, so more explicit pluralization rules can easily be created:

 '{0} There are none|\[1,19\] There are some|\[20,Inf\] There are many' ); ?>

Further reading:

PHP internationalization frameworks: The FuelPHP framework

fullphp

The Lang class of FuelPHP framework allows setting up language variables using language files. The default language (en) is set in app/config/config.php. The Config:set method can be used to change that value:

Config::set('language', 'de');
FuelPHP also allows defining of fall-back language in  the configuration, which is either a language code or an array of language codes.

An example of app/config/config.php :

https://gist.github.com/bosskovic/6116266\\

Several resource file formats are supported by FuelPHP:

PHP array files are the default type. They have a similar key-value structure as previously described for the Laravel framework.

INI files. We have already covered this resource file type in an article of its own.

YAML files. We have covered these as well, you can find the article here.

JSON files. You can read about resjson files, json resource files used in the Windows applications developed for Windows 8 in this article.

The language resource files are loaded with the load method of the Lang class. If the resource file type is not specified, Lang::load will default to "php array" type.

Specific lines are retrieved from the language file with the Lang::get method. The one required parameter of the get method is the key of the phrase that should be returned. Optional parameters are: $parameters - an array of parameters used to replace the place-holders, $default - value to return if the key can not be found and $language - language code for which the line should be retrieved; if not given, the current active language is used.

For other methods of the Lang class, you can check the official FuelPHP documentation.

Example:

[gist]https://gist.github.com/bosskovic/6116466\[/gist\]

Further reading:

This concludes our series of articles on the PHP i18n. It should give you an idea how website internationalization is implemented across the PHP world, and help you make the necessary steps so that your websites can be more accessible to wider audience. Once that is clear, get ready for localization. Talk to us if we can help you prepare to roll out your applications in many languages.

Try lingohub 14 days for free. No credit card. No catch. Cancel anytime