Jump to content

Recommended Posts

Posted

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

Posted

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
Posted

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.

Posted

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
Posted

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!!

Posted

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.

  • Like 2

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
  • Recently Browsing   0 members

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