Setting WordPress language programmatically?

I have a user based website with WordPress and from their profile settings they are able to select the language, this info and other settings are set for every user in user_meta.

I know how to translate but, is there a way to set the theme language programmatically?

Read More

Thank you!

Edit: No plugins please, I need to do this as simple as possible.

Related posts

Leave a Reply

8 comments

  1. I found a different solution:

    // Set user selected language by loading the lang.mo file
    if ( is_user_logged_in() ) {
    
        // add local filter
        add_filter('locale', 'language');
    
        function language($locale) {
            /* Note: user_meta and user_info are two functions made by me,
               user_info will grab the current user ID and use it for
               grabbing user_meta */
    
            // grab user_meta "lang" value
            $lang = user_meta(user_info('ID', false), 'lang', false); 
    
            // if user_meta lang is not empty
            if ( !empty($lang) ) {
               $locale = $lang; /* set locale to lang */
            }
    
            return $locale; 
        }
    
        // load textdomain and .mo file if "lang" is set
        load_theme_textdomain('theme-domain', TEMPLATEPATH . '/lang');
    
    }
    

    Via: http://codex.wordpress.org/Function_Reference/load_theme_textdomain

  2. I came up with following solution as I needed to generate invoices from a plugin in different languages in the scope of the same request:

        global $locale;
    
        $locale = 'en_CA';
        load_plugin_textdomain('invoice', false, 'my-plugin/languages/');
        generateInvoice(); // produce the English localized invoice
    
        $locale = 'fr_CA';
        load_plugin_textdomain('invoice', false, 'my-plugin/languages/');
        generateInvoice(); // produce the French localized invoice
    
  3. I guess you’re looking for the override_load_textdomain filter, called just in the beginning of a load_textdomain function call.

    That would be something like:

    function my_load_textdomain ($retval, $domain, $mofile) {
    
        if ($domain != 'theme_domain')
            return false;
    
        $user = get_currentuserinfo()
        $user_lang = get_user_lang($user);
    
        if ($new_mofile = get_my_mofile($user_lang)) {
            load_textdomain('theme_domain', $new_mofile);
            return true;
        }
    
        return false;
    }
    add_filter('override_load_textdomain', 'my_load_textdomain');
    

    Code from brain to keyboard, not tested. You should do some more validations and so.

  4. A had same question, I resolved it this way:

    fist step you have to create a .mo with a text domain. You can use loco translate plugin: https://br.wordpress.org/plugins/loco-translate/

    Load your text domain linking .mo file:

    load_textdomain('your_text_domain', '/your/file.mo');
    

    Change wordpress location via hook. Create a function wich returns wanted location. Use WordPress Locale Id ever, you can see all locales Id here: https://wpastra.com/docs/complete-list-wordpress-locale-codes/

    Lets write the function:

    function wpsx_redefine_locale($locale){
      
      return 'ja';
    }
    add_filter('locale','wpsx_redefine_locale',10);
    

    You can change by user location. This way wordpress sets the same language was be set by user in its control panel:

    $user_locale = get_user_locale();
    function wpsx_redefine_locale($locale){
      global $user_locale;
      return $user_locale;
    }
    add_filter('locale','wpsx_redefine_locale',10);
    

    Obs: My wordpress version is 5.7. This way can no work in another versions. Good look!

  5. I had a similar problem and solved it like this:

    1. In my case I wanted to retrieve the locale by using the user locale: $userLocale = get_user_locale($userObject->ID);

    2. I created a custom function to load the correct theme_textdomain with dynamic locale. It’s almost the same as the WP function, but you can add a locale variable:

      /**
       * Loads text domain by custom locale
       * Based on a WP function, only change is the custom $locale
       * parameter so we can get translated strings on demand during runtime
       */
      function load_theme_textdomain_custom_locale($domain, $path = false, $locale)
      {
          /**
           * Filter a theme's locale.
           *
           * @since 3.0.0
           *
           * @param string $locale The theme's current locale.
           * @param string $domain Text domain. Unique identifier for retrieving translated strings.
           */
          $locale = apply_filters('theme_locale', $locale, $domain);
      
          if (!$path) {
              $path = get_template_directory();
          }
      
          // Load the textdomain according to the theme
          $mofile = "{$path}/{$locale}.mo";
          if ($loaded = load_textdomain($domain, $mofile)) {
              return $loaded;
          }
      
          // Otherwise, load from the languages directory
          $mofile = WP_LANG_DIR . "/themes/{$domain}-{$locale}.mo";
          return load_textdomain($domain, $mofile);
      }
      

    Based on:
    http://queryposts.com/function/load_theme_textdomain/