WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 5 years ago

Last modified 5 years ago

#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 :)

Attachments (1)

switcher.php (2.0 KB) - added by Rahe 6 years ago.
The switcher class

Download all attachments as: .zip

Change History (5)

@Rahe
6 years ago

The switcher class

#1 @ocean90
5 years ago

Related: #29783, #26511.

This ticket was mentioned in Slack in #core-i18n by ocean90. View the logs.


5 years ago

#3 @SergeyBiryukov
5 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Hi @Rahe, thanks for the ticket!

#26511 has a similar switch_to_locale() proposal, let's focus on this feature there.

#4 @Rahe
5 years ago

Hi,

Is this feature will be available on frontend too ?
I'm glad this feature can be merged in core :)

Note: See TracTickets for help on using tickets.