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

Andrew Nacin wp at andrewnacin.com
Thu Mar 7 16:52:08 UTC 2013


On Thu, Mar 7, 2013 at 11:31 AM, Manuel Schmalstieg <webdev at ms-studio.net>wrote:

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

Generally speaking, you should wait until init to register a taxonomy. In
this case, you will need to register it earlier. Since this is presumably a
private taxonomy without any rewrite rules, you should be fine. Note that
"registered_taxonomy" fires when a taxonomy is registered, and is not for
registering taxonomies themselves.

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
> "plugins_loaded"?
>

load_textdomain fires every time load_textdomain() is called.

Default textdomain files are loaded after plugins_loaded and setup_theme,
but before the theme is loaded and before after_setup_theme fires. Loading
English then re-loading the textdomain into German on the init hook would
be fine, performance-wise, as English has no mo files. But loading Spanish
by default then switching to German would not. Likewise, switching from
Spanish to German then back to Spanish would not be, as the way core API
works, it will unload Spanish only to load it again.

Nacin


More information about the wp-hackers mailing list