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:
cannot change language programmatically for WC()->countries
Solution:

do_action( 'wpml_switch_language', "en" );
		$countries_obj   = new WC_Countries();
		$lang = apply_filters( 'wpml_current_language', NULL );
		echo 'WC_Countries::get_countries() : <br/>';
		echo '<pre>'; print_r($countries_obj->get_countries()); echo '</pre>
<p>';</p>
<p>		do_action( 'wpml_switch_language', "ja" );<br />
		$countries_obj   = new WC_Countries();<br />
		$lang = apply_filters( 'wpml_current_language', NULL );<br />
		echo 'WC_Countries::get_countries() : <br />';<br />
		echo '</p>
<pre>'; print_r($countries_obj->get_countries()); echo '</pre>
<p>';<br />

It is about initializing the class WC_Countries(); after changing the language.

This topic contains 10 replies, has 2 voices.

Last updated by Michele 2 months ago.

Assigned support staff: Bruno Kos.

Author Posts
August 19, 2019 at 9:47 am #4414957

Michele

In the website I am working on, Japanese is the primary language

I am trying to:
global $sitepress;
$sitepress->switch_lang('en', true);
error_log('WC()->countries : ' . print_r( WC()->countries,true));
error_log('$sitepress->get_current_language() : ' . $sitepress->get_current_language());
$sitepress->switch_lang('ja', true);
error_log('WC()->countries : ' . print_r( WC()->countries,true));
error_log('$sitepress->get_current_language() : ' . $sitepress->get_current_language());

I expected to see:
get a list of countries in English first and then in Japanese

Instead, I got:
$sitepress->get_current_language() returns the language set with switch_lang, both lists of countries are in Japanese

I tried the code above in another website and I get the same result, both lists of countries are in Japanese

August 19, 2019 at 10:49 am #4415377

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

I've asked our 2nd tier about this and will get back here with more questions and updates!

Regards,
Bruno Kos

August 19, 2019 at 10:51 am #4415385

Michele

thanks a lot! I'll wait to hear back from you on it.

August 19, 2019 at 11:24 am #4415533

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

Some quick things to try:
- can you translate this page and tell me about the result there?
- what if you try using hidden link after changing the language?

Can you try on the sandbox and let me know?

Regards,
Bruno Kos

August 19, 2019 at 11:57 pm #4419511

Michele

I just did.

in the Japanese page (default language) this is the result :
Lang: en
country by code JP : 日本
-----------------------
Lang: ja
country by code JP : 日本

in the English page:
Lang: en
country by code JP : Japan
-----------------------
Lang: ja
country by code JP : Japan

August 20, 2019 at 5:45 am #4420567

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

So these are the results with both your code and the update with get_countries() code?

Regards,
Bruno Kos

August 20, 2019 at 6:23 am #4420673

Michele

oops, sorry I didn't understood your request earlier.

I changed the code in the sandbox to this:
$countries_obj = new WC_Countries();

global $sitepress;
$sitepress->switch_lang('en', true);
echo 'Lang: ' . $sitepress->get_current_language() . '<br/>';
echo 'country by code JP : ' . WC()->countries->countries['JP'] . '<br/>';
echo 'WC_Countries::get_countries() : <br/>';
echo '<pre>'; print_r($countries_obj->get_countries()); echo '</pre><br/>';

echo '-----------------------' . '<br/>';
$sitepress->switch_lang('ja', true) . '<br/>';
echo 'Lang: ' . $sitepress->get_current_language() . '<br/>';
echo 'country by code JP : ' . WC()->countries->countries['JP'] . '<br/>';
echo 'WC_Countries::get_countries() : <br/>';
echo '<pre>'; print_r($countries_obj->get_countries()); echo '</pre><br/>';

you can see the results:
hidden link
hidden link

in short:
WC()->countries->countries['JP'] is returning the country in the page's language, not the one I switch to ($sitepress->switch_lang('en', true);)

WC_Countries()->get_countries() is returning a list of countries in English, no matter the language page or if I use switch_lang

August 20, 2019 at 7:59 am #4421357

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

This issue has been escalated to 2nd tier. As for fixing it, I can't give you any dates, it could end up in the next release or in 6 months. This is totally up to developers - severity of the issue and number of reported cases (among other things) decide its queue on the list.

I will keep this thread updated as soon as I get any new information from them! In the meantime, maybe you can check this also:
https://wpml.org/wpml-hook/wpml_switch_language/

And try various combinations using that code and examples.

Regards,
Bruno Kos

August 20, 2019 at 8:30 am #4421583

Michele

well, thanks for trying.

I hope at least the other issue I am having with Gutemberg not respecting user language will be fixed.

August 20, 2019 at 10:13 am #4422477

Bruno Kos
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+02:00)

Hi,

Can you try this?

do_action( 'wpml_switch_language', "en" );
		$countries_obj   = new WC_Countries();
		$lang = apply_filters( 'wpml_current_language', NULL );
		echo 'WC_Countries::get_countries() : <br/>';
		echo '<pre>'; print_r($countries_obj->get_countries()); echo '</pre><br/>';

		do_action( 'wpml_switch_language', "ja" );
		$countries_obj   = new WC_Countries();
		$lang = apply_filters( 'wpml_current_language', NULL );
		echo 'WC_Countries::get_countries() : <br/>';
		echo '<pre>'; print_r($countries_obj->get_countries()); echo '</pre><br/>';

It is about initializing the class WC_Countries(); after changing the language.

hidden link

Regards,
Bruno Kos

August 20, 2019 at 11:05 am #4423071

Michele

Worked like a charm also in my own website.

You really helped me a lot here.

My issue is resolved now. Thank you!