I use gettext to translate my web site.
It would be nice to have link in text translated but I don't want to write html tags in gettext files because these translations may be used elsewhere.
I could create text for web site only (with links) and text for all purpose.
I'll have to maintain 2 versions. :-(I could also write a custom parser to insert links in text开发者_如何转开发 but it seems overkill and I'm affraid of The Danger of Naïveté
For those who had the same problem, how did you handle that ?
I used to put links as parameters. Most of the time the whole <a>
-tag is a parameter. Like Please see %s this link %s for more information.
The first %s is passed the link tag, and the second one is passed the closing </a>
tag.
Now I use only whole phrases as links when a link text must be translated. So we have just text like 'Please see this link for more information', get it translated and put the link tags around it. It's much easier to manage the translations, and you don't have a confused translator, or need to spend time explaining what you're trying to do.
I honestly wouldn't be too concerned about using some basic HTML (or use bbCode, textile, markdown, etc.) directly into your translation files. URL's might appear to be the only problem right now, but what about other text markup?
Not to mention that the URL's themselves could potentially be different depending on the language.
I do agree that translation files do have their issues though.
To complement the selected answer, here is a practical example:
printf( __( 'Please see %s this link %s for more information.', 'text-domain' ), '<a href="http://yolo.io" title="yolo">', '</a>' );
Another solution here: https://zargony.com/2008/01/24/links-in-gettext-translated-strings.
It suggests using {curly brackets}
for links. As for example:
_("Please click {here} or {here}")
And then write your own function linkify
that replaces things in curly brackets with the parameters given.
linkify(
_("Please click {here} or {here}"),
"</a>",
"<a href='www.example1.com'>",
"<a href='www.example2.com'>"
)
Definition in PHP:
function linkify($string, $closingTag) {
$arguments = func_get_args();
return preg_replace_callback(
'/{(.*?)}/', // Ungreedy (*?)
function($matches) use ($arguments, $closingTag) {
static $i = 1;
$i++;
return $arguments[$i] . $matches[1] . $closingTag;
},
$string
);
}
PS: You can also easily substitute the {} for [], as I've noticed that in POEdit these curly brackets give warnings to the translators and recommend them not to be translated.
精彩评论