Did you know? String::insert is nice for translation!
You probably replace your language strings like so:
e(sprintf( __("Please %s or %s to write a comment", true),
$html->link(__("login", true), array('controller' => 'users', 'action' => 'login')),
$html->link(__("register", true), array('controller' => 'users', 'action' => 'register'))
));
There are two problems with it:
- You may have to explain alot when someone translates the app. %s is meaningless.
- You probably have to reorder the sprintf arguments some day (login,register<>register,login).
String::insert to the rescue!
Let’s rewrite the above:
e(String::insert( __("Please :login or :register to write a comment", true),
array(
'login' => $html->link(__("login", true), array('controller' => 'users', 'action' => 'login')),
'register' => $html->link(__("register", true), array('controller' => 'users', 'action' => 'register'))
)));
Now this looks alot nicer, don’t you agree? Anyone translating your app will know what is inserted where, and it even allows to reorder the link replacements within the translation msgstr if needed. And the best part of it: we don’t care!


You can reorder the replacements with sprintf by doing “%1$s”, but your solution is much better.
..and this should go to book i18n chapter or bakery.
tnx
[...] Did you know? String::insert is nice for translation! [...]
[...] Set::extract – it was like I was 10 years old and discovering Halloween (the movie) all over again. cakealot has a quick post explaining the String::insert that is worth checking [...]
Great! Thank you very much!
I always wanted to write in my blog something like that. Can I take part of your post to my blog?
Of course, I will add backlink?
Sincerely, Timur I.