简体   繁体   中英

Localization with HTML-Tags

I wanted to localizate some texts in my Zend Framework applicaiton. There are some texts like:

Hello, I'm <a href="test.php" title="Title-Attribute" rel="test">a sample text</a>, greetings to all of you! :)

If there's no html in it, it's simple to localizate, but with HTML in it, how should I do it best?

Some localisation tools like Virtaal handles XML (HTML) tags in a way that makes it easy for the translator to work with. The other option is to simplify the string presented to your localisers by replacing the tag with a string formatting variable and making the format string localisable in stead of the result of the formatting. For example (I'm mostly guessing for the sake of the example and don't know PHP very well):

printf(_("Hello, I'm %sa sample%s, greetings to all of you!"), '<a href="test.php" title="Title-Attribute" rel="test">', "</a>");

In the above statement the string that the localiser will see is only the one containing the %s formatting variables (because it's wrapped in the _() gettext function). This, of course, has the drawback of being quite confusing to a localiser if (s)he is unaware of what the variables will be replaced with, so do include a proper localisation comment.

You should localize any string that is in the UI. In this situation, you have a combination of UI/control elements. That means that the title attribute's content has to be localized separately.

In this case, this means:

$guilink = "<a href='test.php' "
           ."title='".gettext("Title-Attribute")."'"
           ." rel='test'>"
           .gettext( "a sample text" )
           ."</a>";

$guistring = sprintf( 
      gettext( "Hello, I'm %s, greetings to all of you! :)" ),  $guilink );

But, as Gordon says: don't overcomplicate.

For the translation adapters it should not make a difference whether the translation contains HTML or not. If you got short strings with inline HTML, just put the translation along with the HTML into your language source file as needed. You just have to make sure the output is not escaped when there is HTML inside the string.


EDIT Like xtofl pointed out in the comments, having HTML inside the language file is not an optimal solution, because translators might be unfamiliar with handling HTML. While there is some truth to that, I find it neglectable. HTML is not rocket science and for the few cases where inline HTML elements may appear, you can provide them with a cheat sheet. Professional translators should be able to handle that. Another concern would be them inserting unwanted content, eg links (if you are working with community translators instead of professionals). But since you didn't mention any translators anyway, I assume you are doing the translation yourself, in which case all of the above is yagni . There is no need to overcomplicate things .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM