| 1 | <?php
|
|---|
| 2 | class Language_Switcher {
|
|---|
| 3 | /**
|
|---|
| 4 | * @var Language_Switcher
|
|---|
| 5 | */
|
|---|
| 6 | private static $instance;
|
|---|
| 7 |
|
|---|
| 8 | /**
|
|---|
| 9 | * The array containing all the mofiles
|
|---|
| 10 | *
|
|---|
| 11 | * @var array
|
|---|
| 12 | */
|
|---|
| 13 | private $mo_files;
|
|---|
| 14 |
|
|---|
| 15 | protected function __construct() {
|
|---|
| 16 |
|
|---|
| 17 | /**
|
|---|
| 18 | * Store all the textdomain and files
|
|---|
| 19 | */
|
|---|
| 20 | add_filter( 'load_textdomain_mofile', array( $this, 'load_textdomain_mofile' ), 1, 2 );
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * @return self
|
|---|
| 25 | * @author Nicolas Juen
|
|---|
| 26 | */
|
|---|
| 27 | public static function get_instance() {
|
|---|
| 28 | if ( is_null( self::$instance ) ) {
|
|---|
| 29 | self::$instance = new self();
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | return self::$instance;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * Add the mo file to the class
|
|---|
| 37 | *
|
|---|
| 38 | * @param string $mo_file : the mo file path
|
|---|
| 39 | * @param string $domain : domain
|
|---|
| 40 | *
|
|---|
| 41 | * @author Nicolas Juen
|
|---|
| 42 | * @return string
|
|---|
| 43 | */
|
|---|
| 44 | public function load_textdomain_mofile( $mo_file, $domain ) {
|
|---|
| 45 | $this->mo_files[ $domain ][] = $mo_file;
|
|---|
| 46 | return $mo_file;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * Unload all registered files
|
|---|
| 51 | *
|
|---|
| 52 | * @author Nicolas Juen
|
|---|
| 53 | */
|
|---|
| 54 | private function unload_text_domains() {
|
|---|
| 55 | // Unload the files
|
|---|
| 56 | foreach ( $this->mo_files as $domain => $file ) {
|
|---|
| 57 | unload_textdomain( $domain );
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | unload_textdomain( 'default' );
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Load other locale files
|
|---|
| 65 | *
|
|---|
| 66 | * @param $locale_to : the locale to switch
|
|---|
| 67 | *
|
|---|
| 68 | * @author Nicolas Juen
|
|---|
| 69 | * @return bool
|
|---|
| 70 | */
|
|---|
| 71 | public function switch_locale( $locale_to ) {
|
|---|
| 72 | global $locale;
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * Do not switch locale if same
|
|---|
| 76 | * or empty mo files
|
|---|
| 77 | */
|
|---|
| 78 | if ( $locale === $locale_to || empty( $this->mo_files ) ) {
|
|---|
| 79 | return false;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | remove_filter( 'load_textdomain_mofile', array( $this, 'load_textdomain_mofile' ), 1, 2 );
|
|---|
| 83 | $this->unload_text_domains();
|
|---|
| 84 |
|
|---|
| 85 | // Reload the files
|
|---|
| 86 | foreach ( $this->mo_files as $domain => $files ) {
|
|---|
| 87 | foreach ( $files as $file ) {
|
|---|
| 88 | load_textdomain( $domain, str_replace( $locale, $locale_to, $file ) );
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | // Load WP text domain
|
|---|
| 93 | load_default_textdomain( $locale_to );
|
|---|
| 94 |
|
|---|
| 95 | return true;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | /**
|
|---|
| 99 | * Restore the original locale
|
|---|
| 100 | *
|
|---|
| 101 | * @author Nicolas Juen
|
|---|
| 102 | */
|
|---|
| 103 | public function restore_locale() {
|
|---|
| 104 | $this->unload_text_domains();
|
|---|
| 105 | $this->switch_locale( get_locale() );
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|