Jump to content

How to Translate Text containing HTML Tags?


bytesource
 Share

Recommended Posts

Hi all, 

I was wondering about the best practices for translating text that contains HTML tags such as span.

Example:

<p>
  We love to <span class='highlight'>highlight</span>so that it <span class='highlight'>stands out</span>.
</p>
 

The following does not work as the tags won't be interpreted but output as is:

<p>
  <?php echo __("We love to <span class='highlight'>highlight</span>so that it <span class='highlight'>stands out</span>."); ?>
</p> 

Cheers, 

Stefan

Link to comment
Share on other sites

https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/core/LanguageFunctions.php#L39

I think you can only split your text so that it doesn't contain HTML-tags.

Also it isn't possible to write a wrapper function that does the splitting + translation + concatenation for you, because the translation-texts must be available in static form in code, :(

  • Like 1
Link to comment
Share on other sites

Hi Horst, 

Thanks for pointing me to the code handling the translation. 

Splitting the text at every tag is a bit cumbersome and might make sentences more difficult to translate, when words get out of context, but this is only a small inconvenience compared to the advantage of having built-in multi-language support.

Cheers,

Stefan 

Link to comment
Share on other sites

Why not use a simple language file with php vars or a page with language text fields? The translation in PW isn't really suited for such forrmatted text in front end. Theres too many restrictions and the default text is like a key means once you change a typo or word you have to reenter all translations again.

  • Like 3
Link to comment
Share on other sites

Hi Soma, 

That is a very interesting option. It's just that I haven't dealt with translations before, and therefore am not quite sure how to proceed. Could you post a link to an introduction of such translation system?

Cheers, 

Stefan

  • Like 1
Link to comment
Share on other sites

@WillyC

I did not know about html_entity_decode(), and in fact is solves my problem for now:

<p>
  <?php  $t = "We love to <span class='highlight'>highlight</span> so that it <span class='highlight'> stands out</span>.";
  $t = __($t);
  echo html_entity_decode($t, ENT_QUOTES, 'UTF-8');
  ?></p>
 

main.css

.highlight {
    font-weight: 600;
    color: red;
}
 
 
The string will be output with the words 'highlight' and 'stands out' shown in bold red.
 
Thanks a lot for this tip!
 
At everyone:
Soma mentioned that when using __("text"), everytime I change the text, the translations needed to be updated, too. I haven't started doing any translations, but I know that I often tweak text here and there, even if I intially thought it was 'final'. I certainly don't want the translaters have to redo their translation every time I do such a tweak.
 
Is there a way to deal with edits to the main language text without effecting its translations?
 
Cheers, 
 
Stefan
 
 
 
  • Like 3
Link to comment
Share on other sites

Is there a way to deal with edits to the main language text without effecting its translations?

It's best to have your translators do their job of translating static text once you've finished your development work. Either that, or like Antti said, use multi-language fields for stuff that you think may change regularly. 

  • Like 1
Link to comment
Share on other sites

Hi Ryan, 

Multi-language fields are indeed very pleasant to work with, and I try to manage most of the site's content using these fields. My question therefore only refered to static text in templates.

You are right, though, eternal tweaking of such content is certainly not the best way to go  :rolleyes:

Cheers, 

Stefan

Link to comment
Share on other sites

  • 2 years later...

As soma said in #4, it is good to use an external php file for multilanguage string translation, and include in some where  instead of do it by pw.

$translation_strings['default'] = array(
  'MESSAGE-1' => "this is english message",
  'MESSAGE-2' => "this is english message 2",
  'MESSAGE-3' => "this is english message 3",
  'MESSAGE-4' => "this is english message 4 ",
  'MESSAGE-5' => "this is english message 5",
)

translation_strings['japan'] = array(
  'MESSAGE-1' => "これは、日本のマッサージ",
  'MESSAGE-2' => "これは、日本のマッサージ2",
  'MESSAGE-3' => "これは、日本のマッサージ3",
  'MESSAGE-4' => "これは、日本のマッサージ4",
  'MESSAGE-5' => "これは、日本のマッサージ5",
)

Then include in _init.php

in a page template, something like

$text = $translation_strings[$user->language->name];

//do something

$content .=  $text['MESSAGE-4'];

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...