Created
October 9, 2013 23:19
-
-
Save luckyshot/6910230 to your computer and use it in GitHub Desktop.
Simple PHP i18n (internationalization)
(Haven't tested it, written on the fly)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$lang = array( | |
'createaccount' => 'Create Account', | |
'welcome' => 'Welcome %s!', | |
// ... | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// You'd put these in your class constructor (remember to change to $this->lang) | |
// Loads the language file based on user agent | |
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); | |
// Load it or default to English | |
if (file_exists('lang/'.$lang.'.php')) { | |
include('lang/'.$lang.'.php'); | |
}else{ | |
include('lang/en.php'); | |
} | |
// Now the function/method | |
function __($string, $custom = array()) { | |
if ($lang[$string]) { | |
$return = $lang[$string]; | |
$i = 0; | |
while (strpos($return, '%s')) { | |
$return = str_replace('%s', $custom[$i], $return, $temp=1); | |
$i++; | |
} | |
return $return; | |
}else{ | |
return 'Unrecognized language string!'; // or false if you want to catch it | |
} | |
} | |
// Usage | |
echo __('createaccount'); | |
echo __('welcome', array('John')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment