Skip to content

Instantly share code, notes, and snippets.

@luckyshot
Created October 9, 2013 23:19
Show Gist options
  • Save luckyshot/6910230 to your computer and use it in GitHub Desktop.
Save luckyshot/6910230 to your computer and use it in GitHub Desktop.
Simple PHP i18n (internationalization) (Haven't tested it, written on the fly)
<?php
$lang = array(
'createaccount' => 'Create Account',
'welcome' => 'Welcome %s!',
// ...
);
<?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