Please make sure to update to WPML 4.3.6 and check our list of Known Issues before reporting

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:
language switcher languages change position if you switch languages

Solution:

Funtions.php:

/**
 * Inline (horizontal) custom language selector
 *
 * To use call wpml_custom_language_selector(); in a template
 * Note: Take the time to study the output code. 
 */
function wpml_custom_language_selector(){
    // check if wpml is installed
    if (function_exists('icl_get_languages')) {
        // get active languages. Available params here: 
        // http://wpml.org/documentation/getting-started-guide/language-setup/custom-language-switcher/
        $languages = icl_get_languages('skip_missing=0&orderby=code&order=asc');            
        if(!empty($languages)){
            echo '<div class="lang_selector">';
                foreach($languages as $l){
                // from this point on, uncomment whatever output you like
                    // flags only - do not display current language 
                    /* if(!$l['active']){
                        echo '<a href="'.$l['url'].'">
                            <img src="'.$l['country_flag_url'].'" alt="'.$l['language_code'].'" />
                        </a>';
                    } */
                     
                // the output code options below will display the current language
                    // this adds an "active" class to the current language anchor
                    $class = $l['active'] ? ' class="active"' : NULL;
                    // code only
                    $langs .=  '<a ' . $class . ' href="'.$l['url'].'">' . strtoupper ($l['native_name']). '</a> ';
                    //flag-code divided by a |
                    /*$langs .=  '<a ' . $class . ' href="' . $l['url'] . '"><img src="' . $l['country_flag_url'] . '" alt="' . $l['language_code'] . '" />' . strtoupper ($l['language_code']). '</a> | ';*/
                }
                // strip the empty space and | from last language
                $langs = substr($langs,0,-3);
                echo $langs;
            echo '</div>';
        }
    }
}

Theme header.php:

<?php wpml_custom_language_selector();?>

This topic contains 7 replies, has 2 voices.

Last updated by Beda 5 years ago.

Assigned support staff: Beda.

Author Posts
February 14, 2015 at 1:27 am #560700

robp-4

I am trying to: keep the language in the switcher always in the same order.

I have read that I need to create a custom switcher, but isn't there an easier way?

Is there a step by step created to show how to create this?
Rob

February 15, 2015 at 5:32 pm #561003

Beda
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch ) Italian (Italiano )

Hello Rob

Thanks for the Debug Infos

I can't see more then the English Language Option in your language Switcher.

Have you hidden the secondary languages in WPML > Languages > Hide Languages?

Usually you will see the active language first in the list, unless you have a custom language switcher enabled.

Unfortunately there is also a BUG in WPML that makes it impossible ate the moment to order your languages other than in Alphabetical Order.

This is fixed in the latest Beta version that you could download from wpml.org > Downloads.
==> Please backup the site and database if you want to install it.

Can you elaborate why only one language is available on the page, and if you are already using a custom language switcher?

If you need to display always the same language, you will need a custom language switcher, as you correctly stated.
A workaround would be have the language switcher display as a horizontal list instead of a vertical drop down.

Please let me know if you need further help with this.

Thank you

February 15, 2015 at 5:39 pm #561007

robp-4

I have hidden the secondary languages in WPML

I will have Chinese (traditional) and English
We need it to always read:
English Chinese even if Chinese is the active language.

I am using a horizontal menu - Is there an override for language order?
Rob

February 16, 2015 at 12:04 pm #561335

Beda
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch ) Italian (Italiano )

Hello Rob

Yes, this should be possible.

Though, I would need temporary access (WP-Admin and FTP) to your site
- preferably to a test site where the problem has been replicated if possible -
in order to be of better help.

Your next answer will be private which means only you and I can see and have access to it.

==> Please backup your database and website <==

I need to hav e closer look to the code of your site to see how it can be achieved.

It will surely need a custom language switcher.

Please could you provide me above mentioned details so I can go ahead?

Thank you

February 17, 2015 at 3:29 am #561816

robp-4

Hi - if you are to work on this, I am told to make the language selector for Chinese be: 中文
(reminder... English on the left and Chinese on the right)
Thanks,
Rob

February 17, 2015 at 2:33 pm #562150

Beda
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch ) Italian (Italiano )

Hello Rob

Please could you check your site now, I have added a custom language switcher that should accomplish with your needs:

This is the Function that I added to Theme Valenti (CHILD): functions.php on line 33

/**
 * Inline (horizontal) custom language selector
 *
 * To use call wpml_custom_language_selector(); in a template
 * Note: Take the time to study the output code. 
 */
function wpml_custom_language_selector(){
	// check if wpml is installed
	if (function_exists('icl_get_languages')) {
		// get active languages. Available params here: 
		// http://wpml.org/documentation/getting-started-guide/language-setup/custom-language-switcher/
		$languages = icl_get_languages('skip_missing=0&orderby=code&order=asc');			
		if(!empty($languages)){
			echo '<div class="lang_selector">';
				foreach($languages as $l){
				// from this point on, uncomment whatever output you like
					// flags only - do not display current language 
					/* if(!$l['active']){
						echo '<a href="'.$l['url'].'">
							<img src="'.$l['country_flag_url'].'" alt="'.$l['language_code'].'" />
						</a>';
					} */
					
				// the output code options below will display the current language
					// this adds an "active" class to the current language anchor
					$class = $l['active'] ? ' class="active"' : NULL;
					// code only
					$langs .=  '<a ' . $class . ' href="'.$l['url'].'">' . strtoupper ($l['native_name']). '</a> ';
					//flag-code divided by a |
					/*$langs .=  '<a ' . $class . ' href="' . $l['url'] . '"><img src="' . $l['country_flag_url'] . '" alt="' . $l['language_code'] . '" />' . strtoupper ($l['language_code']). '</a> | ';*/
				}
				// strip the empty space and | from last language
				$langs = substr($langs,0,-3);
				echo $langs;
			echo '</div>';
		}
	}
}

I called it in your Theme Valenti (PARENT) header.php on line 191 with this:

<?php wpml_custom_language_selector();?>

I then disabled the language switcher in WPML > Langauges > Language Switcher Options

I also added a minimal CSS Style to WPML > Languages > Language Switcher Options > Additional CSS

div.lang_selector {
   float: right;
} 

Your Language switcher will now always display as EN / Chinese in their native language names.

Please let me know if this is what you want and if you like it.

If you need to change the Chinese Language name you can do so in
WPML > Languages > Site Languages > Edit, just change the native name of the Chinese language

Thank you

February 17, 2015 at 7:03 pm #562361

robp-4

Wow - thanks so much. Works perfectly.

If I can ask just one more thing. Which line in the code is what keeps the English 1st? The 'Languages order' option in the WPML admin has no effect.

Thanks again,
Rob

February 18, 2015 at 5:44 pm #562953

Beda
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch ) Italian (Italiano )

Hello Rob

That sorting option currently doesn't work on WPML due to a BUG which is fixed in the latest Beta available form wpml.org > downloads

==> a backup would be absolutely necessary if you try out that version

==> I can't guarantee that the above code will work with the beta version, though it should.

==> Also keep in mind that any theme update you will make, you will need to secure the code first, or it will get overwritten.

Thank you