4

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.

2
  • check this one out: codeigniter.com/wiki/… Commented Jan 29, 2012 at 22:40
  • It doesnt fix my problem - it is for non variable strings. Commented Jan 29, 2012 at 22:51

2 Answers 2

10

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()

Sign up to request clarification or add additional context in comments.

3 Comments

Isn't it an open door for XSS?
@SteeveDroz Only if you are brave enough to feed it with untrusted user input.
@f4der Great, now I feel dumb... and I don't even remember having commented that.
3

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.

1 Comment

Okay. Seems that CI doesn't have a built in solution for that. So i will build my own function to parse that with help of sprintf. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.