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 3 replies, has 2 voices.

Last updated by Andreas Neuber 6 years, 5 months ago.

Assigned support staff: Andreas Neuber.

Author Posts
January 25, 2013 at 3:52 pm #99414

Ad Label

Hello,

I am having trouble in trying to change the language in my code , just like this thread:

http://wpml.org/forums/topic/set-language-in-code-before-page-loads/

I have trien following the stepf here but no luck. Here's my code:

add_action('after_setup_theme', 'my_icl_set_current_language');

function my_icl_set_current_language() {
$lang = $_GET['lang'];
global $sitepress;
$sitepress->switch_lang($lang);
die(ICL_LANGUAGE_CODE );
}

The ICL_LANGUAGE_CODE constant never changes to the desired language.

Can you please provide some help?

Thanks in advance,

Luis

January 25, 2013 at 5:20 pm #99455

Andreas Neuber
Supporter

Languages: English (English ) German (Deutsch )

Hello AdLabel,

The code looks altogether correct. The constant ICL_LANGUAGE_CODE will have as its value the default language set.
So for your tests you would rather need to use a...

die($sitepress->get_current_language());

Do you need this function for AJAX?

January 29, 2013 at 4:50 pm #100271

Ad Label

Hi,

Yes, this is to be used in ajax.

In your documentation it reads that ICL_LANGUAGE_CODE holds the current language, shouldn't it reflect the language we chose?

January 30, 2013 at 8:17 am #100401

Andreas Neuber
Supporter

Languages: English (English ) German (Deutsch )

Hello AdLabel,

The problem here is that the value of constant ICL_LANGUAGE_CODE is set when the instance of the sitepress class is created. Or in other words, its value changes only with a new page call.

plugins/sitepress-multilingual-cms/inc/template-constants.php:

<?php
// part of SitePress class definition
if(!defined('ICL_LANGUAGE_CODE')){
    define('ICL_LANGUAGE_CODE', $this->this_lang);
}

However, with AJAX you are not calling the website new in the browser, just a part of it (the basic concept of AJAX).

The topic ‘[Closed] Change language programaticly’ is closed to new replies.