If you are having a notice about writing the .mo files please read this doc

Hi, Amit here, I am the WPML Support Manager, our current ticket queue is high, update your WPML plugins and make sure you meet the minimal requirements for running WPML before reporting an issue please - many tickets are resolved doing that

Please look at our updated list of Known Issues and you can also use our support search to find helpful information and of course review our documentation before opening a ticket.

If you do need to open a ticket please make sure to provide us with all the needed information as described in this page

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This thread is resolved. Here is a description of the problem and solution.

Problem:
The client is trying to set the default language for each multisite blog through the code.

Solution:

The basic idea is to run the code once . the below function hooked to wp_loaded hook helped to solve the issue. 
function my_icl_set_current_language() {
    //  check if the default langaue is set cookie 
     //  if cookie already set return 
     // example 
     if( $_COOKIE['set_lang']  == '1' ) {
       return ;
     } 
 
    global $sitepress;
    $language_code = get_blog_option( get_current_blog_id(), 'user_language' );
  
    if($language_code != ''){
        $sitepress->set_default_language($language_code);
        $sitepress->switch_lang($language_code);
    } else {
        $sitepress->set_default_language('nb');
        $sitepress->switch_lang('nb');
    }
  // Set Cookie set_lang for langauge switch here
}

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by bomannN 8 months, 3 weeks ago.

Assigned support staff: Raja Mohammed.

Author Posts
February 19, 2019 at 7:40 am #3215288

bomannN

I have a multisite setup.I display various settings for each blog in a custom frontend dashboard view where the client can make changes. One of the request has been to set a default language for a blog.

I am saving this language code as part of a blog option (not required)
update_blog_option( $id, 'site_language', $language );

global $sitepress;
$sitepress->set_default_language($language);

I have made sure that the language codes are same as that being used by WPML but haven't been able to get this to work. I want to set the default language per site. So when the site(url) is opened, the first thing they see is the default language. Also i do not have a language switcher in the UI to prevent users from seeing it in another language.

February 19, 2019 at 12:14 pm #3216496

bomannN

I have to refresh the page twice to see the strings reflect the language change.
Am I missing something?

add_action('init', 'my_icl_set_current_language');
function my_icl_set_current_language() {
    global $sitepress;
    $language_code = get_blog_option( get_current_blog_id(), 'user_language' );

    if($language_code != ''){
        $sitepress->set_default_language($language_code);
        $sitepress->switch_lang($language_code);
    } else {
        $sitepress->set_default_language('nb');
        $sitepress->switch_lang('nb');
    }
    
}
February 20, 2019 at 9:20 am #3220379

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi there,

Welcome to our forum

The "init" action hook is called very late after all the required plugins and themes are loaded Since all the settings are already loaded your function will be applied on the next cycle.

Try hookup your function to "plugins_loaded" or "after_setup_theme" action in place of init action.

Let me know the results

Kind regards
Raja

February 21, 2019 at 5:27 am #3224963

bomannN

"after_setup_theme" seems to be working in only some cases. I did come across "wp_loaded" hook which seems to work better. I have a few roadblocks though.

Hence I have decided to ignore this approach entirely, and base language of the site based on user's language instead.

I am referring this thread https://wpml.org/forums/topic/set-users-default-language-using-php/

The person has mentioned the following in one of his comments:
"Once the language has been set correctly, it's no longer possible to use the language switcher to change to a different language as on every page load the lang is reset to the default".

My scenario is that when user logs in, it will be in the language(X) he selected (pre-defined in user_meta). Now if he wants to change this language to another language(Y) from the language switcher, does he now have to switch every time to that language(Y) as he navigates across different pages?
Because if I use the "wp_loaded" hook, it will keep resetting the user's language(X) saved in user_meta.
How do I avoid this?

February 21, 2019 at 11:25 am #3226992

bomannN

If I am on the following link
hidden link

And I want to change my language, the URL in the switcher is
hidden link

Why doesn't it redirect to the same page i was on?
hidden link

Below is my language switcher
hidden link

February 21, 2019 at 12:26 pm #3227328

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi there,

What you have requested needs the selection of language to be updated or stored somewhere may be in a cookie.

so once the default language is set to blogs and after the user switches to another language, the selection needs to be stored in a cookie and checked in the wp_loaded action else the code will run again to set the default language. The solution is to make the code run only once.

The basic idea is

function my_icl_set_current_language() {
    //  check if the default langaue is set cookie 
     //  if cookie already set return 
     // example 
     if( $_COOKIE['set_lang']  == '1' ) {
       return ;
     } 

    global $sitepress;
    $language_code = get_blog_option( get_current_blog_id(), 'user_language' );
 
    if($language_code != ''){
        $sitepress->set_default_language($language_code);
        $sitepress->switch_lang($language_code);
    } else {
        $sitepress->set_default_language('nb');
        $sitepress->switch_lang('nb');
    }
  // Set Cookie set_lang for langauge switch here
}

This will prevent the code from executing again, remembers user selection and eliminate language switcher issue.

I hope this helps.

Kind regards
Raja

February 26, 2019 at 4:31 am #3243093

bomannN

My issue is resolved now. Thank you!