Akurat w tym konkretnym przypadku. Załóżmy że w testowym katalogu projektu mam następujące pliki
i18n/pl.php
<?php
return array(
);
i18n/en.php
<?php
return array(
'Strona domowa' => 'Home',
'O nas' => 'About',
'Kontakt' => 'Contact',
);
i18n/it.php
<?php
return array(
'Strona domowa' => 'Casa',
'O nas' => 'Riguardo a noi',
'Kontakt' => 'Contatto',
);
Klasa helper: I18n.php
<?php
class I18n
{
public static $default = 'pl';
public static function load($lang)
{
return include DOCROOT . 'i18n' . DIRECTORY_SEPARATOR . $lang . '.php';
}
public static function get($string, $lang = NULL)
{
if ($lang === NULL)
{
$lang = I18n::$default;
}
$table = I18n::load($lang);
return isset($table[$string]) ? $table[$string] : $string;
}
}
if (!function_exists('__'))
{
function __($string, array $values = NULL, $lang = 'pl')
{
if ($lang !== I18n::$default)
{
$string = I18n::get($string);
}
return empty($values) ? $string : strtr($string, $values);
}
}
oraz testowy index.php (testowałem na CLI)
<?php
define('DOCROOT', realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR);
spl_autoload_extensions('.php');
spl_autoload_register();
$menu = array('Strona domowa', 'O nas', 'Kontakt');
I18n::$default = 'pl';
echo "Polish translation:\n\n";
foreach ($menu as $item)
{
echo "\t" . __($item) . "\n";
}
I18n::$default = 'en';
echo "\nEnglish translation:\n\n";
foreach ($menu as $item)
{
echo "\t" . __($item) . "\n";
}
I18n::$default = 'it';
echo "\nItalian translation:\n\n";
foreach ($menu as $item)
{
echo "\t" . __($item) . "\n";
}
Chociaż jako punkt odniesienia powinien być język angielski i I18n::$default powinno być ustawione na en a wszystkie klucze powinny być po angielsku a ich wartości to odpowiednie tłumaczenia i całe menu w bazie powinno być też po angielsku.