PHP and gettext for I18n

It looks complicated on first sight, because GNU gettext is very powerful. But it can be done in 10 minutes to setup a simple example which scales to an arbitrary complex application.

index.php

<?php

session_start();

// I18N support information here
$language = 'en';

if ($_REQUEST['lang']) {
    $language = $_REQUEST['lang'];
}

putenv("LANG=$language");
setlocale(LC_ALL, $language);

// Set the text domain as 'messages'
$domain = 'messages';
bindtextdomain($domain, "locale");
textdomain($domain);

echo(gettext("This is text number one."));
echo(gettext("This is a second text."));

echo('<br/>');

echo('<a href="' . $_SERVER['PHP_SELF'] . '?lang=en' . '">English</a>');
echo('<br>');
echo('<a href="' . $_SERVER['PHP_SELF'] . '?lang=de' . '">German</a>');

?>

Simple, eh?

See:


http://www.onlamp.com/pub/a/php/2002/06/13/php.html


http://www.php.net/manual/en/ref.gettext.php

Leave a Reply


uberdose 2.0

L-l-look at you, hacker.