#32879 closed feature request (duplicate)
Live switching Language
Reported by: | Rahe | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.2.2 |
Component: | I18N | Keywords: | |
Focuses: | Cc: |
Description
Hi,
I'm currently working on a projet where I needed to change the loaded mo to another language.
In my cron I needed to change the locale to a user defined value and then send a mail to him in his/her language, then an another mail to someone else in on another language.
The main problem was I haven't context of the user language an needed to switch the loaded file.
My idea was to hook all load_textdomain, store them in a property and then switch the languages like a wp_switch_blog method but only with locale.
The code unload all textdomain and then try to load the previous stored values.
Since WordPress can store at different places the translation files, at this time this is not possible to only have the path to localized files.
And the Class have to be launched as soon as possible to get all mo files, like in the mu-plugins.
The performances with 11 files wasn't very bad, 0.010 sec to switch the files. But many sites have more !
I use it like this :
<?php Language_Switcher::get_instance()->switch_locale('fr_FR'); /** * Some text to translate in French */ Language_Switcher::get_instance()->restore_locale(); ?>
I do not touch the global $locale, just un/loading mo files.
Maybe there is an elegant way to integrate this in core ? Or this is an edge case and not usefull for the other developpers :)
The switcher class