-
Notifications
You must be signed in to change notification settings - Fork 0
Placeholders substitution in language strings
From time to time is useful to have an ability to modify language string from language file dynamically. I'm using the following functions:
[size=1] [code] <? $lang['user_logged_out'] = "User %s has been logged out";
#this functions takes variable number of parameters, calls second function #which replaces %s placeholders with these parameters in appropriate order. function lang($msg) { $args = func_get_args();
if(is_array($args)) array_shift($args);
if(is_array($args) && count($args))
{
foreach($args as $arg)
{
$msg = str_replace_first('%s', $arg, $msg);
}
}
return $msg;
} function str_replace_first($search_for, $replace_with, $in) { $pos = strpos($in, $search_for); if($pos === false) { return $in; } else { return substr($in, 0, $pos) . $replace_with . substr($in, $pos + strlen($search_for), strlen($in)); } } ?> [/code] [/size]
Example: [code] [size=1] <?=lang($lang['user_logged_out'], $username);?> [/size] [/code]
Messages than sound more naturally. I believe this helps someone and it would by nice if something like this could be part of language class in standard CI distro.