Juergen Posted April 5, 2016 Share Posted April 5, 2016 Hello @ all, PW offers the opportunity to use multilanguage translateable string. $text = _x('text', 'noun'); With echoing the text variable you can get the different language translations - so far so good. But how can I use such a language string inside a multiline variable? THIS WORKS $name = _x('Your name', 'Form'); $summary = <<<EOT My $name is nobody. This is a text over multiple lines. EOT; echo $summary; THIS DOESN´T WORK $summary = <<<EOT My _x('Your name', 'Form') is nobody. This is a text over multiple lines. EOT; echo $summary; I have also tried it with ${_x('text', 'noun')} and other combinations but I cannot get it to work. Does anyone know how to use translateable strings inside multiline variables? Best regards Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 5, 2016 Share Posted April 5, 2016 The heredoc syntax behaves exactly like double-quote strings in a single line. Both do not support calling a function in it, only class methods or anonymous functions, see here for examples: http://stackoverflow.com/questions/10002652/in-php-how-to-call-function-in-string Edit: What might work is this, as templates while mostly using local variables are still executed in class context. $a = <<<EOT My {$this->_x('text', 'noun')} EOT; Edit2: You're also using nowdoc instead of heredoc, which does not allow for variable substitution at all, like single-quote strings. See here: http://php.net/manual/de/language.types.string.php#language.types.string.syntax.heredoc 2 Link to comment Share on other sites More sharing options...
Juergen Posted April 5, 2016 Author Share Posted April 5, 2016 Unfortunately this leads to error 500 in my case. Instead of the standard syntax for translateable strings I use a function: //Global translation function function _t($text, $context, $textdomain = '/site/templates/_strings.php') { return _x($text, $context, $textdomain); } and then I call the string like this: $namelabel = _t('Your name', 'Form'); Maybe this could be a reason for the error message. Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 5, 2016 Share Posted April 5, 2016 That's exactly the point I meant to convey. You cannot "embed" normal functions in strings. It does only work for class methods or anonymous functions, e.g. everything where a variable (the class or the anonym. func) is involved. 1 Link to comment Share on other sites More sharing options...
Juergen Posted April 5, 2016 Author Share Posted April 5, 2016 OK, I understand. So there is no other way. I have to put the translateable string function into a variable and put this variable into the multiline variable. Thanks for you help!! Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 5, 2016 Share Posted April 5, 2016 Or you create a class for the job. Edit: Also are you sure the first example of your entry post does work? As nowdocs do not substitute variables it shouldn't. If it's not working please correct your entry post. 2 Link to comment Share on other sites More sharing options...
Juergen Posted April 5, 2016 Author Share Posted April 5, 2016 Yep. Example was not the correct syntax. I have corrected it and I have tested it. I got the correct output. 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