bytesource Posted July 20, 2013 Share Posted July 20, 2013 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 More sharing options...
horst Posted July 20, 2013 Share Posted July 20, 2013 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, 1 Link to comment Share on other sites More sharing options...
bytesource Posted July 20, 2013 Author Share Posted July 20, 2013 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 More sharing options...
Soma Posted July 20, 2013 Share Posted July 20, 2013 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. 3 Link to comment Share on other sites More sharing options...
bytesource Posted July 20, 2013 Author Share Posted July 20, 2013 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 1 Link to comment Share on other sites More sharing options...
WillyC Posted July 20, 2013 Share Posted July 20, 2013 echo html_entity_decode(__('you.text w <blink>htmls</blink>')); 8 Link to comment Share on other sites More sharing options...
bytesource Posted July 21, 2013 Author Share Posted July 21, 2013 @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 3 Link to comment Share on other sites More sharing options...
apeisa Posted July 21, 2013 Share Posted July 21, 2013 For content you should not use this method, but multilang fields instead. On front end tokens are fine for some mostly static stuff like button labels ie. "search" etc. 2 Link to comment Share on other sites More sharing options...
ryan Posted July 23, 2013 Share Posted July 23, 2013 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. 1 Link to comment Share on other sites More sharing options...
bytesource Posted July 23, 2013 Author Share Posted July 23, 2013 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 Cheers, Stefan Link to comment Share on other sites More sharing options...
adrianmak Posted April 24, 2016 Share Posted April 24, 2016 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 More sharing options...
adrianmak Posted April 24, 2016 Share Posted April 24, 2016 I think it is more easily to manage multilanguage strings translation, as I don't need to login at the back-end to do that job Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now