How to change the value WPLANG in WordPress by clicking a link (switch language)?

I successfully made files for localization using Gettext (called es.po and es.mo).

So far as I know I can change the language by defining the value of WPLANG in wp-config.php (in this case define (‘WPLANG’, ‘es’);)

Read More

I would like the user to change his/her preferred language by clicking a link.

What’s the best way of doing this?

Related posts

Leave a Reply

3 comments

  1. I accomplished this by building upon the jLanguage Plugin found at http://www.treutech.com/files/wordpress/jLanguage.zip. *The plugin is no longer available from the author’s site, so I’m hosting my updated version. This allows you to format your blog posts and pages using the syntax [english][/english]. It passes a querystring to the code to know which language to use. I started by modifiying the code so that it would use the standard two-letter language codes. I also did away with the flags that the code used to represent the various languages. However, after all this, the plugin still only translates the pages or posts. I wanted the rest of the site to be translated based on the user’s choice.

    WordPress allows for internationalization; however, once you pick a language you are stuck with it till you change it manually. So I created wp-lang.php. It looks first at what language the user chose from the available language links, It then stores that in a SESSION variable so that state will be persistent. Finally, if no choice was made and there is not SESSION variable, the code will look at the default languages for the browser.

    Now, all these changes are dependent upon whether you have downloaded a language package that corresponds with the choices of languages on the site. The language MO file contains translations of all the function names in WordPress. So if the user logins into the Admin panel, everything will be translated. If your theme is coded properly, then your menu headings and meta information will be translated as well. After all that, I modified the wp-config file to include wp-lang. Now the site this is set up on will switch between Spanish and English.

    *wp-lang.php

     session_start();
     if ( isset( $_GET['lang'] ) ) {
        $_SESSION['WPLANG'] = $_GET['lang'];
        define ('WPLANG', $_SESSION[WPLANG]);
     } else {
        if(isset($_SESSION['WPLANG'])) {
            define ('WPLANG', $_SESSION['WPLANG']);
            $_GET['lang'] = $_SESSION['WPLANG'];
        } else {
            if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) ) {
                $languages = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] );
                 $languages = explode( ",", $languages );
                $_SESSION['WPLANG'] = $languages[0];
                $_SESSION['WPLANG'] = str_replace("-", "_", $_SESSION['WPLANG']);
                $_GET['lang'] = substr($_SESSION['WPLANG'],0,2);
                define ('WPLANG', $_SESSION[WPLANG]);
            } else {
                define ('WPLANG', '');
            }
        }
     }
    

    *wp-config.php – Find the section where the constant WPLANG is defined. Add in the following line just before the WPLANG declaration.

     require_once(dirname(__FILE__).'/wp-lang.php');
     define ('WPLANG', ''); 
    

    This page will first check for the default language of the browser and then set the language. If not, the user can also set the language based on the the one they pick from the posts. The language is set in a session variable to hold state for the entire visit.

  2. My 2 cents: I do a similar thing, but I also remove the ?lang=XX parameter from URL.

    After all work on language detection is done, I do the 307-redirection in the code, to the same URL (with ?lang=xxx or &lan=xxx removed from it), then die(). That makes the URL clean off the GET variable, clean and potentially safe for bookmarking.

    Also I store the language preference in a cookie, to allow user see the same language upon next visit.

  3. My best solution was… (session_start(); on top)

    if( !isset( $_GET['lang'] ) && isset($_SESSION['WPLANG']) ) {
            define ('WPLANG', $_SESSION['WPLANG']);
    } else {
    
        if ( !isset( $_GET['lang'] ) ) {
          if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) ) {
            $languages = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] );
            $languages = explode( ",", $languages );
            $wplang = str_replace("-", "_", $languages[0]);
            $_GET['lang'] = substr($wplang,0,2);
          }
        }
    
        $language = isset( $_GET['lang'] ) ? htmlspecialchars($_GET['lang'], ENT_QUOTES) : 'es';
        switch ( $language ) {
            case 'en':
                define( 'WPLANG', 'en_US' );
                $_SESSION['WPLANG'] = 'en_US';
            break;
    
            case 'es':
            default:
                define( 'WPLANG', 'es_ES' );
                $_SESSION['WPLANG'] = 'es_ES';
        }
    
    }