Skip to content

Instantly share code, notes, and snippets.

@mpezzi
Created March 29, 2010 20:39
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpezzi/348379 to your computer and use it in GitHub Desktop.
Save mpezzi/348379 to your computer and use it in GitHub Desktop.
PHP Simple Translation Engine
<?php
define('LANG_ENGLISH', 'en');
define('LANG_FRENCH', 'fr');
define('LANG_SPANISH', 'es');
// Look for l in query string and set as language.
$language = isset($_GET['l']) ? $_GET['l'] : LANG_ENGLISH;
/**
* Translate a string
* Use the $translate variable to define your set of language translations.
* Note: Based off of Drupal's t() function.
*
* @param $string
* The string that is to be translated.
* @param $args
* An array of placeholders that will populate the translated string.
* @param $langcode
* The language you wish to translate the string to.
*/
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);
}
}
/*
Example: "http://example.com/index.php?l=fr"
--------------------------------------------
include('inc.language.php');
// Set our translations.
$translation['fr'] = array(
'Email' => 'Courriel',
'Name' => 'Nom',
'Organization' => 'Entreprise',
'Phone Number' => 'Num&eacute;ro de t&eacute;l&eacute;phone',
'Hello %name' => 'Bonjour %name',
);
print t('Name');
print t('Email');
print t('Organization');
print t('Phone Number');
print t('Hello %name', array('%name' => 'Mr. Anderson'));
Outputs:
--------------------------------------------
Nom
Courriel
Entreprise
Numéro de téléphone
Bonjour Mr. Anderson
*/
@Thegamethor
Copy link

dssdfsdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment