• Hello,

    Is it possible to display language switcher only if the page has translation?

    If there is not translation, the switcher should be hidden.

    I can write PHP but i don’t know how to check the condition “there is translation” or “active translation”

    G.

    https://wordpress.org/plugins/sublanguage/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author maximeschoeni

    (@maximeschoeni)

    Hello,

    I am really sorry, I realised I totally forgot to answer your question. As it is 2 months old, I am sure you dont need it anymore, but here is the solution however.

    I also will try to make it simpler in a future version.

    add_action('sublanguage_custom_switch', function($languages, $sublanguage) {
    
    	if (is_singular()) {
    
    		// count translations
    
    		$queried_object = get_queried_object();
    
    		$translation_count = 0;
    
    		foreach ($languages as $language) {
    
    			if (!$sublanguage->is_current($language)) {
    
    				$translation = $sublanguage->get_post_translation($queried_object->ID, $language->ID);
    
    				if ($translation && $translation->post_content != $queried_object->post_content) {
    
    					$translation_count++;
    
    				}
    
    			}
    
    		}
    
    	}
    
    	if (!is_singular() || $translation_count > 0) {
    
    		// print the language switch 
    
    		$output = '<ul>';
    
    		foreach ($languages as $language) {
    
    			$output .= sprintf('<li class="%s%s"><a href="%s">%s</a></li>',
    				$language->post_name,
    				($sublanguage->current_language->ID == $language->ID ? ' current' : ''),
    				$sublanguage->get_translation_link($language),
    				apply_filters('sublanguage_language_name', $language->post_title, $language)
    			);
    
    		}
    
    		$output .= '</ul>';
    
    		echo $output;
    
    	}
    
    }, 10, 2);
    Laura

    (@syccylinders)

    Hi Maxime!

    I tried to put this code on my test wordpress but it’s not working. I put it on functions.php of the theme and I put it on the sublanguage.php but nothing. :/

    Regards

    Actually, this works only if you use the “custom” language switch. You’re probably using the switch into the wordpress menu.

    Laura

    (@syccylinders)

    ahh, What does mean “custom” language switch?

    Thanks!

    Laura

    (@syccylinders)

    Solution —

    I installed “In Page Script plugin” and I added the next code on the footer script section:

    <script type="text/javascript">
    jQuery(function ($) {
    	$("li.sublanguage a:contains('ES')").html('<img src="your-image-spain-flag.png"/>');
    	$("li.sublanguage a:contains('EN')").html('<img src="your-image-english-flag.png"/>');
    });
    </script>

    *You need to put in Sublanguage/Languages titles, a name short as is: ES, EN in my case for
    ES= spanish language and EN= UK language.

    One of the ways to solve this problem short term while the final version is not ready

    Thanks to Maxime for your help

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display lang switch only if there is translation’ is closed to new replies.