Internationalization in PHP

Best way to internationalize simple PHP website

Although ext/gettext and ext/intl are both related to i18 (internationalization), gettext deals with translation while intl deals with internationalizing things like number and date display, sorting orders and transliteration. So you'd actually need both for a complete i18-solution. Depending on your needs you may come up with an home-brew solution relying on the extensions mentioned above or your use components provided by some framework:

  • Translation
    • Symfony 2 Translation component: https://github.com/symfony/Translation
    • Zend Framework Zend_Translate
  • Internationalization
    • Zend Framework Zend_Locale

If you only need translation and the site is simple enough, perhaps your simple solution (reading a translation configuration file into an PHP array, using a simple function to retrieve a token) might be the easiest.

The most simple solution I can think of is:

$translation = array(
'Hello world!' => array(
'fr' => 'Bonjour tout le monde!',
'de' => 'Hallo Welt!'
)
);

if (!function_exists('gettext')) {
function _($token, $lang = null) {
global $translation;
if ( empty($lang)
|| !array_key_exists($token, $translation)
|| !array_key_exists($lang, $translation[$token])
) {
return $token;
} else {
return $translation[$token][$lang];
}
}
}

echo _('Hello World!');

Internationalization in PHP

In addition to gettext already mentioned, PHP 5.3 has native Internationalization support

If that's not an option, consider using Zend Framework's Zend_Translate, Zend_Locale and related components for that. Zend_Translate supports a number of adapters, including but not limited to simple arrays, gettext, XmlTm and others.

What is the best way of i18n in PHP

I would go for gettext. As it is a de-facto standard and is used in many applications in different languages. Many people use it means bigger community and good support.

If you search for gettext in stackoverflow, you'll get good resources and examples.

Getting started with it in PHP:
http://www.onlamp.com/pub/a/php/2002/06/13/php.html

Are there any library for i18n on plain php (without any framework)?

intl(since php 5.3) and gettext(since forever)

From php intl extension docs page:

Internationalization extension (further is referred as Intl) is a
wrapper for ICU library, enabling PHP programmers to perform
UCA-conformant collation and date/time/number/currency formatting
in their scripts.

From gettext docs page:

This package offers to programmers, translators and even users, a well
integrated set of tools and documentation. Specifically, the GNU
gettext utilities are a set of tools that provides a framework within
which other free packages may produce multi-lingual messages.

How to internationalize a PHP Web Application?

The only thing you have really missed out is utf8_encoding / utf8_decoding over pages.

https://secure.php.net/manual/en/function.utf8-encode.php

https://secure.php.net/manual/en/function.utf8-decode.php

Also may need to use iconv:

https://secure.php.net/manual/en/function.iconv.php

NOTE:

utf8_encode() will only convert ISO-8859-1 to UTF-8 so you will have to use iconv in this instance.



Related Topics



Leave a reply



Submit