How to change language in each page ? (9 posts)

  1. tonyparera
    Posted 4 years ago #


    I have setup a WP site with CMS pod. So it's more like a CMS than a blogging site.

    In this, I have setup 10 pages which represent different country. I want to set different language for different pages.

    So far I have done,
    1 .Created directory "wp-content/languages" and copied all languages .po and .mo file there.
    2. Tested this with changing define ('WPLANG', 'nb_NO') in wp-config.php

    Now the question is, how to change my language in each page ? I can detect the page by using get_the_title() in page template file. can I use dynamically 'define ('WPLANG', "$country_language")' in start of page template itself or any other method ?


  2. bcworkz
    Posted 4 years ago #

    There's no way to redefine any PHP constant, so what you suggest will not work. Try setting the global $locale to equal the language value for the particular page in it's head section. That just might do the trick.

  3. tonyparera
    Posted 4 years ago #

    Hi bcworks,

    Thanks, I will give a try of your suggestions, I guess following article also suggest to use this.

    setlocale(LC_ALL, "de_DE"); //at page's start of header
    setlocale(LC_NUMERIC, 'C'); //to avoid and numeric mismatched as nicely explained in below artible.


  4. tonyparera
    Posted 4 years ago #

    Well, I tried but It seems not working this way.

    I have created .mo files and placed into WP_LANG_DIR directory and use below two line, to set the language at the beginning of the page header file but it seems not taking any effect.

    setlocale(LC_ALL, "de_DE");
    setlocale(LC_NUMERIC, 'C');

  5. bcworkz
    Posted 4 years ago #

    I'm not sure what setlocale actually does, couldn't find it's source. You might also check the global $l10n which should contain arrays of translations. If not, the language files are not getting loaded.

    It looks like just setting locale is not enough, you need to explicitly load the related language files. Take a look at wp-includes/l10n.php. There is a load_textdomain() function, along with a bunch of variants for plugins and themes. They appear to be an important part of getting this to work, but I'm unclear how to actually make use of them.

    Are there any plugins that change languages after loading WP? You may learn something from how they do things.

  6. tonyparera
    Posted 4 years ago #

    true, I have already started to dig into l10n.php file and all load_textdomain() and other textdomain related functions.

    Initially I thought this could be a simple setup somewhere now it turn out to be some good stuff to explore and debug.

    My default is English and I not loading any other non-english plugins so I guess that is not an issue.

    Let me explore bit more with textdomain stuff and see.

  7. bcworkz
    Posted 4 years ago #

    I meant to say you should investigate any available plugins out there that change languages to see how they do things. Not that you have any on your installation. Could be another avenue for learning. I'm certainly not much help. Good luck!

  8. tonyparera
    Posted 3 years ago #

    After lots of hack and reading, I managed to solve this and getting it worked at http://www.oyetweet.com where it does translate to multiple languages in one post.

    //tony: add all language related mo files to initiate the MO array.
    global $jwp_lang;
    $jwp_lang['textdomain'] = 'brazil_portuguese';
    $domain = $jwp_lang['textdomain'];
    $mofile = ABSPATH . 'wp-content/languages/' . $jwp_lang['textdomain'] . '.mo';
    load_textdomain($domain, $mofile);

    than I simply select the textdomain based on country title.

    $title = get_the_title();
    if (strtolower($title) == 'brazil') $jwp_lang['textdomain'] = 'brazil_portuguese';
    if (strtolower($title) == 'germany') $jwp_lang['textdomain'] = 'german_germany';
    if (strtolower($title) == 'philippines') $jwp_lang['textdomain'] = 'philippines_filipino';
    if (strtolower($title) == 'malaysia') $jwp_lang['textdomain'] = 'malaysia_malay';

    and to display content, I use this

    global $jwp_lang;
    __('Suggest Celebrity', $jwp_lang['textdomain'])
  9. bcworkz
    Posted 3 years ago #

    Good work Tony! I know several people will want to make use of this. Thank you for sharing.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.