Skip to content

Instantly share code, notes, and snippets.

@bosskovic
Last active August 16, 2017 05:01
Show Gist options
  • Save bosskovic/5900148 to your computer and use it in GitHub Desktop.
Save bosskovic/5900148 to your computer and use it in GitHub Desktop.
Example of localization using json
<?php
// the original of this code can be found on:
// http://www.mind-it.info/2010/02/22/a-simple-approach-to-localization-in-php/
function localize($phrase) {
/* Static keyword is used to ensure the file is loaded only once */
static $translations = NULL;
if (is_null($translations)) {
$lang_file = '/lang/' . $_SESSION["locale"] . '.txt';
/* If no instance of $translations has occured load the language file */
if (!file_exists($lang_file)) {
$lang_file = '/lang/' . 'en.txt';
}
$lang_file_content = file_get_contents($lang_file);
/* Load the language file as a JSON object
and transform it into an associative array */
$translations = json_decode($lang_file_content, true);
}
return $translations[$phrase];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment