[wp-hackers] Multilanguage: how to redefine locale on the fly

Manuel Schmalstieg webdev at ms-studio.net
Thu Mar 7 16:31:05 UTC 2013

Nacin, thanks for those insights!

> If you want to load a different locale to begin with, you need to do so
> very early — no later than the setup_theme hook, which fires *before* a
> theme's functions.php file is included. Plugins are loaded earlier, so you
> can do it there. The plugins_loaded hook is fine.

Hmm, looking at http://codex.wordpress.org/Plugin_API/Action_Reference
I see that the "registered_taxonomy" action comes very early, before
"plugins_loaded" and "setup_theme" ... does this imply that a custom
taxonomy ("language") can be safely used by a plugin in order to
activate the "locale switch"?

> It is possible to "switch out" a locale after the default locale is loaded,
> but I would advise against that as it is inefficient, unless your default
> language is English, in which case it is not so bad.

That's good to know. By "inefficient", you refer to the double loading
and processing of .mo files? Wouldn't the default language get loaded
only at the "load_textdomain" action, which comes after

> If you need to switch your language with each post when viewing a
> multiple-post archive page, I must warn you each time you load or switch a
> locale, you will be taking a performance hit. But as long as you are just
> setting a single locale for that pageload, you're fine.

Indeed, I envision this method to be used only for single posts/pages,
*not* for switching the language multiple times on archive pages (I
didn't even think about that possibility ... you're giving me
dangerous ideas :)

> Also:
>  - Please avoid relying on globals as much as possible. So, use the locale
> filter, not the $locale global.
>  - qTranslate does some really weird things to achieve its goals. I
> wouldn't recommend studying it for advice.

My goal is actually *not* to do *weird things*, but to use built-in WP
functions to allow for a basic multi-language capacity.

I admit that I just had a look at qTranslate. I see that it uses it's
own custom methods for handling the date format, which I don't fully

For redefining the $locale, it uses this filter:

function qtrans_localeForCurrentLanguage($locale){
	global $q_config;
	// try to figure out the correct locale
	$locale = array();
	$locale[] = $q_config['locale'][$q_config['language']].".utf8";
	$locale[] = $q_config['locale'][$q_config['language']]."@euro";
	$locale[] = $q_config['locale'][$q_config['language']];
	$locale[] = $q_config['windows_locale'][$q_config['language']];
	$locale[] = $q_config['language'];
	// return the correct locale and most importantly set it (wordpress
doesn't, which is bad)
	// only set LC_TIME as everyhing else doesn't seem to work with windows
	setlocale(LC_TIME, $locale);
	return $q_config['locale'][$q_config['language']];


Anything in there I shouldn't try at home? :)


More information about the wp-hackers mailing list