views:

85

answers:

3

Hello,

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?

+1  A: 

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.

xtofl
A: 

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, e.g. 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.

Gordon
Taking this to the edge, you may have translators take your complete source code and pick out the string literals... I don't like that, really. Translators should be unknowing of technical details, in order to complete the job efficiently. (Who knows if the `name` attribute in a html form's input should be translated into `nom`, `name`, `gnorpx`?)
xtofl
@xtofl For the OP's purpose, the above is a valid approach. We do not know whether the OP is working with translators. If the OP does, the translators can likely tell him, if they can handle inline HTML. Undelete your own answer, if you think it is a better solution.
Gordon
+2  A: 

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.

Walter
I understood that "Title-Attribute" will have to be translated too.
Gordon
In that case xtofl's answer looks like the better one. Upvoted it. Same idea, though. :)
Walter