Skip to content

Instantly share code, notes, and snippets.

@bosskovic
Last active December 19, 2015 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bosskovic/5900015 to your computer and use it in GitHub Desktop.
Save bosskovic/5900015 to your computer and use it in GitHub Desktop.
Example of localizing strings with string array
<?php
$GLOBALS["messages"] = array (
'en'=> array(
'Monday' => 'Monday',
'Tuesday' => 'Tuesday',
'Wednesday' => 'Wednesday',
'Thursday' => 'Thursday',
'Friday' => 'Friday',
'Saturday' => 'Saturday',
'Sunday' => 'Sunday'
),
'de'=> array(
'Monday' => 'Montag',
'Tuesday' => 'Dienstag',
'Wednesday' => 'Mittwoch',
'Thursday' => 'Donnerstag',
'Friday' => 'Frietag',
'Saturday' => 'Samstag',
'Sunday' => 'Sontag'
)
);
function msg($s) {
locale = $_SESSION["locale"];
if (isset($GLOBALS["messages"][$locale][$s])) {
return $GLOBALS["messages"][$locale][$s];
} else {
error_log("l10n error: locale: "."$locale, message:'$s'");
}
}
?>
<?php
// example of usage
session_start();
// if _SESSION["locale"] is set to "de", it will output "Sonntag"
print msg('Sunday');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment