I needed to translate strings on another language than the current $locale, so I need to change the locale and restore it later, and I really couldn't repeat all the code for the 30++ strings I had to translate.

So I ended up with this function:

function __2($string, $textdomain, $locale){
  global $l10n;
  if(isset($l10n[$textdomain])) $backup = $l10n[$textdomain];
  load_textdomain($textdomain, get_template_directory() . '/languages/'. $locale . '.mo');
  $translation = __($string,$textdomain);
  if(isset($bkup)) $l10n[$textdomain] = $backup;
  return $translation;

Now, as per this famous article, I really shouldn't have coded that function :


...But it simply works. So my question is: is it really ALWAYS wrong to pass variabled to l10n functions? An eventually why? And why shouldn't I use my function if it just works?

2 Answers 2


No it does not work, people that will need to translate the string will not be able to use the automated tools they usually use to extract the strings that need translation.

And yes, it is always bad.

  • I'm using it to translate hidden fields of custom metaboxes in admin, and it relies on .po/mo files on the theme. It just works great for me. Jul 28, 2017 at 10:44

The answer is: sometimes. As long as you know what you are doing, there are no problems in terms of bugs. There can be situations where this is possible and safe, and other where it can be necessary or extremely useful.

To complete and share the code above, I add the following:

function _e2($string, $textdomain, $locale){
  echo __2($string, $textdomain, $locale);

// example



Some bonus code can be found here:

How to get a traslated string from a language other than the current one?

  • You don't at all address the reason why this doesn't work as explained in the page you linked, or how to solve that.
    – Milo
    Jul 28, 2017 at 13:22
  • You probably didn't read the article. It's not mine. It's an article explaining l10n "bad practices". Probably you didn't face this argument, so, apart from this thread, I strongly recommend you to read the article I linked, cause it's really important and useful. My question is about edge cases. Jul 28, 2017 at 13:27
  • I did read it. You haven't explained how strings can be extracted for translation. I assume you have done this manually?
    – Milo
    Jul 28, 2017 at 15:25
  • If you take a look to __2(), you'll see this: load_textdomain($textdomain, get_template_directory() . '/languages/'. $locale . '.mo'); Jul 28, 2017 at 15:33
  • 1
    I mean, how do you generate the file of strings to be translated? That's the problem with using variables in translation functions. The tool that extracts strings to be translated won't find anything, because the function calls contain variables, not static strings.
    – Milo
    Jul 28, 2017 at 16:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.