1

The Stackoverflow user Casimir et Hippolyte gave me this awesome function to replace strings like [[ Something ]] with localized strings being returned with $this->_() function.

$that = $this;
$view = preg_replace_callback('~\[\[\K(?>[^]]++|](?!]))*~', function ($m) use ($that) {
   return $that->__($m[0]); }, $view);

It works except that it doesn't replace the brackets. So, if I have "Yes|Ja" my I18n file, and in my view file I write [[ Yes ]] it comes up with [[ Ja ]].

I've spent a few hours trying and googling but unfortunately - nothing. Any ideas?

2 Answers 2

1

Sorry, you can replace your pattern by:

$that = $this;
$view = preg_replace_callback(
        '~\[\[((?:[^]]+|](?!]))*+)]]~', function ($m) use ($that) {
        return $that->__($m[1]); }, $view);
Sign up to request clarification or add additional context in comments.

Comments

1

This will capture the two square brackets \K\[\[(?>[^]]++|](?!]))]]*

Input Text

like [[ yes|ja ]] with 

Matches

[0] => [[ yes|ja ]]
[1] => yes|ja 

I"m not a python programmer, but I think you'll want to modify your script to be like this:

$that = $this;
$view = preg_replace_callback('~\K\[\[(?>[^]]++|](?!]))]]*~', function ($m) use ($that) {
   return $that->__($m[1]); }, $view);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.