Hide language WPML

3.7k views Asked by At

I am using WPML language, and cant find solution for next thing:

On the Language switcher i want to hide language, lets say for example - "he", if current language is lets say for example "ar", so when we on arabic site we will not see on the selector the Hebrew, and same thing if we on Hebrew, the arabic will not display.

On shorten words: what i want is - if we on arabic site - the hebrew flag will be hidden.

What i tried:

function language_selector_flags(){
    $languages = icl_get_languages('skip_missing=0');
    if(!empty($languages)){
       if(ICL_LANGUAGE_CODE=='en') 
       {
$order = array('ar'); //Specify your sort order here
       }
elseif(ICL_LANGUAGE_CODE=='he')
{
$order = array('en', 'ar'); //Specify your sort order here
}

        foreach ($order as $l) {
            if (isset($languages[$l])) {
                $l = $languages[$l]; //grab this language from the unsorted array that is returned by icl_get_languages()

                //Display whatever way you want -- I'm just displaying flags in anchors  (CSS: a {float:left; display:block;width:18px;height:12px;margin:0 2px;overflow:hidden;line-height:100px;})
                if($l['active']) { $class = "active"; $url=""; } else { $class = ''; $url = 'href="'.$l['url'].'"'; }
                echo '<a '.$url.' style="background:url('.$l['country_flag_url'].') no-repeat;" class="flag '.$class.'">';
                echo $l['language_code'].'';
            }
        }
    }
}

Its not affect at all the selector.

4

There are 4 answers

5
Boris Belenski On BEST ANSWER
function language_selector_flags(){
    $languages = icl_get_languages('skip_missing=0');
    if(!empty($languages)){
        $filter = array();
        $filter['ar'] = array( 'he' );
        // set your other filters here

        $active_language = null;
        foreach ($languages as $l)
            if($l['active']) {
                $active_language = $l['language_code'];
                break;
            }

        $filter = $active_language && isset( $filter[$active_language] ) ? $filter[$active_language] : array();
        foreach ($languages as $l) {

                //Display whatever way you want -- I'm just displaying flags in anchors  (CSS: a {float:left; display:block;width:18px;height:12px;margin:0 2px;overflow:hidden;line-height:100px;})
                if( in_array( $l['language_code'], $filter) )
                    continue;
                if($l['active']) { $class = "active"; $url=""; } else { $class = ''; $url = 'href="'.$l['url'].'"'; }
                echo '<a '.$url.' class="flag '.$class.'"><img src="', $l['country_flag_url'], '" alt="', esc_attr( $l['language_code'] ), '" /></a>';
            }
        }
    }

EDIT: If I get this right, your client(I assume) doesn't want his customers (Israelis especiay) to know that he offer service also to the arabic speaking cusomers. If it so then you can parse the Accept-Language header and filter the language selector according the result.

3
birgire On

You can check out the plugin WPML Flag In Menu.

You could use the plugin_wpml_flag_in_menu() function from the plugin (see source code here) and replace:

// Exclude current viewing language             
if( $l['language_code'] != ICL_LANGUAGE_CODE )
{
    // ...
}

with

// Include only the current language                
if( $l['language_code'] == ICL_LANGUAGE_CODE )
{
    // ...
}

to show only the current language/flag, if I understand you correctly.

ps: If you need further assistance, you could for exampe show us the output of this debug function for the active language:

function debug_icl_active_language()
{
    $languages = icl_get_languages( 'skip_missing=0' );
    foreach( (array) $languages as $l )
    {
        if( $l['active'] )
        { 
            printf( '<pre> Total languages: %d - Active: %s </pre>', 
                    count( $languages ), 
                    print_r( $l, TRUE ) );
        }
    }
}
0
Simon Vetterli On

I have a similar problem/issue:

On this website: https://neu.member-diving.com/

I have languages I not need in the switcher. I tried the code above, but it nothing changed so far.

So, what I would like to do is, When a client is on the one "german" page, the other german languages in the switcher should not need to be there, only the english one and the actual german one.

Where do I need to put code like

function language_selector_flags(){
    $languages = icl_get_languages('skip_missing=0');
    if(!empty($languages)){
        $filter = array();
        $filter['ar'] = array( 'he' );
        // set your other filters here

        $active_language = null;
        foreach ($languages as $l)
            if($l['active']) {
                $active_language = $l['language_code'];
                break;
            }

        $filter = $active_language && isset( $filter[$active_language] ) ? $filter[$active_language] : array();
        foreach ($languages as $l) {

                //Display whatever way you want -- I'm just displaying flags in anchors  (CSS: a {float:left; display:block;width:18px;height:12px;margin:0 2px;overflow:hidden;line-height:100px;})
                if( in_array( $l['language_code'], $filter) )
                    continue;
                if($l['active']) { $class = "active"; $url=""; } else { $class = ''; $url = 'href="'.$l['url'].'"'; }
                echo '<a '.$url.' class="flag '.$class.'"><img src="', $l['country_flag_url'], '" alt="', esc_attr( $l['language_code'] ), '" /></a>';
            }
        }
    }