Using variables in Language files |
[eluser]Philo01[/eluser]
Hi there! I was wondering if it's possible to display variables in the language files. For example: Code: <?php Can anyone point me in the right direction? Would appreciate it! Kind regards, Philo
[eluser]Cro_Crx[/eluser]
You can use placeholders like this: Code: $lang['unread_messages'] = "You have %s unread messages"; Then when calling it, use: Code: $number = $this->my_model->unread_messages();
[eluser]nlogachev[/eluser]
cro_crx, you can certainly do that but differences in language grammar might result in the variable positions changing. What I did was overload the line() function in the Language class to allow number-based arguments: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); It works the same way as the original function. Simply put MY_Language.php into your application/libraries folder and you can do stuff like this: Code: //in english Hope this helps.
[eluser]Buso[/eluser]
Interesting, thanks for sharing. I had this problem once and had to change my redaction =s
[eluser]juddmuir[/eluser]
Regarding internationalisation, the quicker solution is to use php's ability argument swapping functionality in the sprintf function (see http://php.net/manual/en/function.sprintf.php) //in english $lang['unread_messages'] = "You have %1$s unread messages, %2$s"; //in another language $lang['unread_messages'] = "Hi %2$s, You have %1$s unread messages"; $message = sprintf($this->lang->line('unread_messages'), $number, $name);
[eluser]nlogachev[/eluser]
Yes, but then you would be writing sprintf($this->lang->line(.....)) every single time. Overload once, use as normal. Easier.
[eluser]Jelenik[/eluser]
Will this work for Codeigniter Reactor 2.0 version too? |
Welcome Guest, Not a member yet? Register Sign In |