For multi-language usage of CMS, they translate terms by a function similar to
function __($word) {
include 'fr.php';
if(!empty($lang[$word])) {$translated=$lang[$word];
} else {
$translated = $word;
}
return $translated;
}
-
Since we need to use this function several times in a php page, as all words and phrases will be echoed by __(‘ ‘); does the function need to include the language time every time, or it will be cached for the function after first load?
-
Since the language file contains a complete list of words and phrased used throughout the website (thousands of key/value), pho needs to load this long array into memory every time a page is visited. Is it really the best approach to add multi-language feature to a CMS?
If you can’t use
gettext()for some reason, you’d be better off, with something like this, to put it into an object with the included language strings as a static array, something like:Your message library file (included with the
setMessageLibrary()static function), of which you’ll have one per language, will need a variable in it called$aMsgswhich might look something like:Since it’s all static but within the object you can effectively cache that included language file by setting it at the start of your script.
All three messages will then reference the single language array stored in
Message::$_messagesThere’s no sanitisation, nor sanity checks in there, but that’s the basic principle anyway … if you can’t use gettext() 😉