Gettext: Translation of strings with HTML inside?
My current implementation, which is array based stores keys and values in a dictionary, example:
$arr = array(
'message' => 'Paste a <a href="http://开发者_如何转开发flickr.com/">flickr</a> URL below.',
);
I realize that it was probably a bad idea storing html inside of a string such as this, but if I'm using gettext then in my .mo/.po files how should I handle storing a similar string? Should I just store words, such as 'Paste a' and 'URL below' and 'flickr' separately?
You should store something like
"Paste a %1 URL below"
and replace all 'vars' using something simple like str_replace('%1', $link, $message);
$link can also be translatable
"<a href="http://flickr.com/">%1</a>"
although that might be overkill (does flickr translate between languages?)
rationale behind this is that different languages have different grammatical structures and the ordering of the words wont always be the same.
Update:
as @alex and @chelmertz mention in the comments, try using the sprintf
function, which is built for this very thing.
I'd go for this:
$arr = array(
'message' => _('Paste a %s URL below.'),
);
Having all translations as string literals within gettext function calls allows to use standard tools to update *.po catalogues.
精彩评论