我像这样扩展了 Code CI_Lang 类..
class MY_Lang extends CI_Lang {
function line($line = '', $swap = null) {
$loaded_line = parent::line($line);
// If swap if not given, just return the line from the language file (default codeigniter functionality.)
if(!$swap) return $loaded_line;
// If an array is given
if (is_array($swap)) {
// Explode on '%s'
$exploded_line = explode('%s', $loaded_line);
// Loop through each exploded line
foreach ($exploded_line as $key => $value) {
// Check if the $swap is set
if(isset($swap[$key])) {
// Append the swap variables
$exploded_line[$key] .= $swap[$key];
}
}
// Return the implode of $exploded_line with appended swap variables
return implode('', $exploded_line);
}
// A string is given, just do a simple str_replace on the loaded line
else {
return str_replace('%s', $swap, $loaded_line);
}
}
}
IE。在您的语言文件中:
$lang['foo'] = 'Thanks, %s. Your %s has been changed.'
以及您想在哪里使用它(控制器/视图等)
echo $this->lang->line('foo', array('Charlie', 'password'));
会产生
Thanks, Charlie. Your password has been changed.
这可以处理单个“交换”以及多个
它也不会破坏任何现有的对$this->lang->line
.