Codeigniter uses a fairly standard way of inserting values into strings within its language files.
You define your string with placeholders, and then use
sprintf to format the string.
//In your language file... $lang['welcome_username'] = 'Welcome %s.'; //then in your view or controller..
echo ( sprintf( lang("welcome_username"), "Bob") ); //Results in "Welcome Bob."
However, for values that only really change per environment / deployment, it would be beneficial to be able to refer to them from within the language file, rather than having to format the string every time you want to use it.
A common example is the name of your application. This probably wont change at run time, but it might change per deployment. If this was dynamically set, you wouldn't need to use
sprtinf every time you want to show that string, its done for you, but is still dynamically generated from one property. It could even reside in another language file.
To load up items from another lang file, you need to ensure that lang file is loaded first.
The best way to do that is to add it to the
language property of
$autoload['language'] = array('common');
Then add the values to your common lang file
$lang['site_name'] = "My Site";
Then, you simply target the CI instance and load the language helper in your other lang file..
//welcome_lang.php //Load up the language helper. CI_Controller::get_instance()->load->helper('language'); $lang["welcome"] = "Welcome to " . lang('site_name') . " I hope you like it"; $lang["info"] = lang('site_name') . " is a web site"; //And then in you view, you can just echo the value.. echo (lang('welcome')); // Which outputs "Welcome to My Site"
You can also target config settings if you really need to.
//general_lang.php //Load up the config.. $config = CI_Controller::get_instance()->config; $lang["welcome"] = "This is a config value " . $config->item('my_setting'); //And then in you view, you can just echo the value.. echo (lang('welcome')); // Which outputs "This is a config value abcde"
sprintf is a better and approach for values that change at runtime, but in some cases, when its a commonly used value, and only changes per deployment, being able to dynamically set the initial language strings can be quite beneficial.