Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use strings with variables in codeigniter's language files?

for a localized website I want to create different language files. But my main problem before starting the localiuation is, that i probably have strings with variables.

My theory is that i can use placeholders within my language files like:

$lang['somekey'] = "Hello Mr. %s, how are you?";

Is there a clean and nice way to parse those variables or do i have to develop a function for that?

Thanks.

like image 678
pila Avatar asked Dec 16 '22 04:12

pila


2 Answers

I have the same problem and do it simply by using,

echo sprintf($this->lang->line('somekey'),'XYZ');//load language library before use

Read sprintf()

like image 98
Rohan Kumar Avatar answered Jan 26 '23 00:01

Rohan Kumar


you can use codeigniter i18n with PHP .sprintf() to achieve what you want. load up the codeigniter non-variable strings (with those format stuff), then pass it on to .sprintf() for formatting and assignment of values. it should replace the %s part.

it's similar to this question. .sprintf() works like .printf(), only that it returns the string rather than printing it.

like image 38
Joseph Avatar answered Jan 26 '23 01:01

Joseph