I particularly like I like this zito system , it's simple and you can do several things with it!
You can define the language by $_GET
, ie it is by url
You can have as many languages as you need, and it's easy to add translations!
Here's an example:
//Definir linguagens a ser usadas:
define('LANG_ENGLISH', 'en');
define('LANG_FRENCH', 'fr');
define('LANG_SPANISH', 'es');
// Definir linguagem principal
$language = isset($_GET['l']) ? $_GET['l'] : LANG_ENGLISH;
// Mini-função que faz o seu trabalho:
function t($string, $args = array(), $langcode = NULL) {
global $language, $translation;
// Set language code.
$langcode = isset($langcode) ? $langcode : $language;
// Search for a translated string.
if ( isset($translation[$langcode][$string]) ) {
$string = $translation[$langcode][$string];
}
// Replace arguments if present.
if ( empty($args) ) {
return $string;
} else {
foreach ( $args as $key => $value ) {
switch ( $key[0] ) {
case '!':
case '@':
case '%':
default: $args[$key] = $value; break;
}
}
return strtr($string, $args);
}
}
To add translations:
(in another separate file)
//index.php
// incluir a mini-função acima se tiver noutro ficheiro
include('inc.language.php');
// adicionar traduções a uma certa linguagem exemplo:
$translation[LANG_SPANISH] = array(
'Email' => 'email',
'Name' => 'nombre',
'Organization' => 'Organización',
'Phone Number' => 'Número de teléfono',
'Hello %name' => 'Hola %name',
);
In its use it would look something like:
//www.site.com/index.php?l=es
<text id="org"><?= t('Organization') ?></text>
// output: Organización