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

Andrew Nacin wp at andrewnacin.com
Thu Mar 7 15:08:32 UTC 2013


On Wed, Mar 6, 2013 at 8:23 AM, Manuel Schmalstieg <webdev at ms-studio.net>wrote:

> One more observation:
>
> The filter below *does* work partly, I can see that the
> language_attributes() in my header.php template react upon it,
> switching to lang="de_DE".
>
> But the date_i18n() function, and terms defined in the po/mo files
> with _e(), are not reacting to the locale change. How can I persuade
> them to collaborate?


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.

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.

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.

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.

Nacin


More information about the wp-hackers mailing list