I implemented a multilanguage feature for my web application. I get the values by this
echo $lang['the key here'];
and i keep the values in a separate fiels like this
$lang['confirm'] = 'COnfirm the message';
$lang['deny'] = 'Deny the invitation';
so i want if somebody calls a undefined key like $lang[‘sdefscfef’] , insted of printing white space, I want to print the key name i.e ‘sdefscfef’
I want to make it as a function
function translate($string) {
if(! isset($string)) {
echo THE KEY;
}
else {
echo $string;
}
}
translate($lang['asdadad']);
and to print the key
Instead of printing the array directly I would create a function (
_()is common) and use it like so:And the
_()function would then look in the $language array:Something like that.
If you want to avoid using a global variable you can wrap all of this in a class like this:
And then, to avoid having to type
Lang::translate()everywhere you can do this:Here’s an example of a little more advanced Language class: http://code.google.com/p/sleek-php/source/browse/trunk/Core/Lang.php