views:

19

answers:

1

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');)

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

What's the best way of doing this?

+1  A: 

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.

Scott Gottreu
@Scott Gottreu Hey that was a very nice implementation but then how should I write the anchor links for the user to change the language?Something like this? <a href="index.php?lang=es">ESPAÑOL</a>
janoChen
Yes. The first part of wp_lang.php takes $_GET['lang'] from the link and sets the seesion variable and then sets the WP_LANG constant for WordPress.
Scott Gottreu