Jump to content

How to output multilanguage string inside a multiline variable?


Juergen
 Share

Recommended Posts

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

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

  • Like 2
Link to comment
Share on other sites

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

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. 

  • Like 1
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...