Skip to main content
5 of 5
code update
Luca Reghellin
  • 1.6k
  • 2
  • 21
  • 45

Is this really always bad? (variables in l10n functions)

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 :

http://ottopress.com/2012/internationalization-youre-probably-doing-it-wrong/

...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?

Luca Reghellin
  • 1.6k
  • 2
  • 21
  • 45