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 topic contains 13 replies, has 2 voices.

Last updated by Raja Mohammed 3 months, 1 week ago.

Assigned support staff: Raja Mohammed.

Author Posts
June 25, 2019 at 12:04 pm #4084357

Trevor

I am trying to: force the value of ICL_LANGUAGE_CODE so that dates appear in English, not Chinese.

Link to a page where the issue can be seen:

URL: hidden link

I expected to see:

Dates in English. I've added this at the top of the template code:

global $sitepress;
$lang = 'en';
$sitepress->switch_lang($lang);

And that is correctly forcing English content to load instead of (non-existent Chinese) - apart from the dates.

Instead, I got: dates in Chinese.

If I test by echoing ICL_LANGUAGE_CODE anywhere on the page, it tells me it is still "zh-hans". So it seems that $sitepress->switch_lang is working in terms of deciding what posts to include in a post loop, but it doesn't change the value of ICL_LANGUAGE_CODE, and that seems to be causing the date issue.

June 25, 2019 at 3:14 pm #4085895

Raja Mohammed
Supporter

Languages: English (English )

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

Hi there,

ICL_LANGUAGE_CODE is defined before the page loads and it fetches the language information set by the language switcher.

The swithc_lang function or the wpml_switch_language hook will only work for the loop.

Dates are handled by string translation, Please go to WPML > String translation, search for the months eg: Jan and translate all the occurrences to English. This will help you resolve your issue.

Kind regards
Raja

June 25, 2019 at 3:31 pm #4085971

Trevor

Thanks Raja. To clarify: the default language for the site is English. On this page, they're appearing in Chinese because of the ?lang=zh-hans in the URL (and therefore ICL_LANGUAGE_CODE). I just want to force them to appear in the default, English - but only on this one page, not on other Chinese pages that have dates. So I still need to enter translations?

June 26, 2019 at 6:00 am #4089207

Trevor

Update - I activated String Translation, went to search for "Jan", and see the attached - it tells me translation is already complete.

In case this is relevant: the dates that are appearing in Chinese are inside a custom loop inside the main loop (it's a function to display a list of latest posts).

Thanks again.

June 26, 2019 at 10:55 am #4091739

Raja Mohammed
Supporter

Languages: English (English )

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

Try using the "wpml_switch_language" hook instead of the swith_lang function.
https://wpml.org/wpml-hook/wpml_switch_language/. This hook will change the global language variable. I hope this helps you better.

Please let me know the results

Kind regards
Raja

June 26, 2019 at 11:19 am #4091905

Trevor

Thanks Raja. Doesn't work unfortunately - I've tried:

do_action( 'wpml_switch_language', 'en');

both inside and outside the main loop, and then if I echo ICL_LANGUAGE_CODE immediately afterwards, it's still set to 'zh-hans', and the dates on the custom loop are still Chinese.

Does this sound like a plugin conflict maybe? If so I'll try taking out all other plugins and switching to default theme, and see if that makes a difference?

June 26, 2019 at 1:34 pm #4092831

Raja Mohammed
Supporter

Languages: English (English )

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

The hook should be passed to the pre_get_posts action hook, Please check the example in the hook page https://wpml.org/wpml-hook/wpml_switch_language/. Can you please confirm is it passed in the same way as described in the example?

Kind regards
Raja

June 26, 2019 at 6:54 pm #4094677

Trevor

Sorry wasn't reading properly! Hmmm so if I remove my earlier attempt and then add this to functions.php (6104 is the ID of the page in question):

// using the hook on a custom query on the WordPress index front end page
function force_lang_to_english($query) {
    if(is_page(6104) && !$query->is_main_query()) { 
        // do_action( 'wpml_switch_language', "en" ); 
    }
}
add_action('pre_get_posts', 'force_lang_to_english');

I'm still getting the English posts with Chinese dates, and ICL_LANGUAGE_CODE is still 'zh-hans', inside and outside the custom loop. You can see the page ID and the language code in the bit of HTML attached.

Any other suggestions very welcome, thanks!

June 26, 2019 at 6:55 pm #4094705

Trevor

Sorry I pasted the wrong thing. The do_action is not commented out - this is the code in functions.php:

// using the hook on a custom query on the WordPress index front end page
function force_lang_to_english($query) {
    if(is_page(6104) && !$query->is_main_query()) { 
        do_action( 'wpml_switch_language', "en" ); 
    }
}
add_action('pre_get_posts', 'force_lang_to_english'); 
June 27, 2019 at 3:06 am #4096157

Raja Mohammed
Supporter

Languages: English (English )

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

Please share the piece of code how the date is handled in your custom query? I have enabled private fields for you to securely share the details.

Kind regards
Raja

June 27, 2019 at 5:33 am #4096765

Trevor

Thanks, it's like this (with irrelevant stuff removed):

while ( $news->have_posts() ) {
$news->the_post();
echo get_the_date();
}
June 27, 2019 at 7:08 am #4097141

Raja Mohammed
Supporter

Languages: English (English )

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

Instead of switching the language, replace the get_the_date with

get_post_time( get_option( 'time_format' ), false, $post, false );

I hope this would solve your issue.

Kind regards
Raja

July 6, 2019 at 7:25 am #4155219

Trevor

Hi, please don't close this yet (in response to auto-responder) - we're still working on it.

July 8, 2019 at 4:40 am #4159025

Raja Mohammed
Supporter

Languages: English (English )

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

Sure, Please keep us posted with any updates.

Kind regards
Raja