• Resolved sagarprasai

    (@sagarprasai)


    Hello, I am developing a website which follows WCAG guideline, it means all the components should be accessible for people with disabilities and those using screen readers and keyboard-only navigation.

    The current language switcher provided by Shortcode of translate press is not very accessible and keyboard friendly. Thus, I want to create my own dropdown button called “Choose Language” and the options would be the two languages of the website. I can create the structure manually by following this example: https://www.w3.org/TR/wai-aria-practices/examples/listbox/listbox-collapsible.html

    However, the link let’s say “English” and “German” should be static and doesn’t update automatically as it does with the shortcode. For example, if I am on the about us page of English and If I click the german language button the same page should open in german but my manual language selector would be able to do so.

    In that case, how would I generate such dynamic links for my language links.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Cristian Antohe

    (@sareiodata)

    Hi,

    TranslatePress shortcode does render HTML, but as you mentioned doesn’t have the exact WCAG guidelines.

    You can create a similar structure using something like this, just modify the template to suit your needs.

    
    function trpc_language_switcher( $atts ){
    	ob_start();
    
    	global $TRP_LANGUAGE;
    
    	$shortcode_attributes = shortcode_atts( array(
    		'display' => 0,
    	), $atts );
    
    	$trp = TRP_Translate_Press::get_trp_instance();
    	$trp_languages = $trp->get_component( 'languages' );
    	$trp_settings = $trp->get_component( 'settings' );
    	$settings = $trp_settings->get_settings();
    	$url_converter = $trp->get_component( 'url_converter' );
    
    	if ( current_user_can(apply_filters( 'trp_translating_capability', 'manage_options' )) ){
    		$languages_to_display = $settings['translation-languages'];
    	}else{
    		$languages_to_display = $settings['publish-languages'];
    	}
    	$published_languages = $trp_languages->get_language_names( $languages_to_display );
    
    	$current_language = array();
    	$other_languages = array();
    
    	foreach( $published_languages as $code => $name ) {
    		if( $code == $TRP_LANGUAGE ) {
    			$current_language['code'] = $code;
    			$current_language['name'] = $name;
    		} else {
    			$other_languages[$code] = $name;
    		}
    	}
    
    ?>
    <div class="trp-language-switcher trp-language-switcher-container" data-no-translation <?php echo ( isset( $_GET['trp-edit-translation'] ) && $_GET['trp-edit-translation'] == 'preview' ) ? 'data-trp-unpreviewable="trp-unpreviewable"' : '' ?>>
        <div class="trp-ls-shortcode-current-language">
            <a href="#" class="trp-ls-shortcode-disabled-language trp-ls-disabled-language" title="<?php echo esc_attr( $current_language['name'] ); ?>" onclick="event.preventDefault()">
    			<?php echo $current_language['name']; // WPCS: ok. ?>
    		</a>
        </div>
        <div class="trp-ls-shortcode-language">
        <?php foreach ( $other_languages as $code => $name ){
            ?>
            <a href="<?php echo esc_url( $url_converter->get_url_for_language($code, false) ); ?>" title="<?php echo esc_attr( $name ); ?>">
                <?php echo $name; // WPCS: ok. ?>
            </a>
    
        <?php } ?>
        </div>
        <script type="application/javascript">
            // some javascript if needed.
        </script>
    </div>
    <?php
    
    	return ob_get_clean();
    }
    
    add_shortcode('custom-language-switcher', 'trpc_language_switcher');
    
    Thread Starter sagarprasai

    (@sagarprasai)

    Hello Cristian,
    Thank you for your help.
    I used the function you provided and changed the template to an accessible selector as available here in codepen: https://codepen.io/wyzykowski/pen/zWyEow

    The code provided was not making the available options as link. Thus, I modified the code as follows:

    <div class="trp-language-switcher trp-language-switcher-container" data-no-translation <?php echo ( isset( $_GET['trp-edit-translation'] ) && $_GET['trp-edit-translation'] == 'preview' ) ? 'data-trp-unpreviewable="trp-unpreviewable"' : '' ?>>
           <label for="native" class="hidden">Select language</label>
    			<select class="dropdown" id="native" title="Select language" onchange="location = this.value;">
                <option data-iso-country-code="<?php echo esc_attr( $current_language['code'] ); ?>" value="" aria-label="<?php echo esc_attr( $current_language['name'] ); ?> selected"> <a href="#" class="trp-ls-shortcode-disabled-language trp-ls-disabled-language" title="<?php echo esc_attr( $current_language['name'] ); ?>" onclick="event.preventDefault()">
    			<?php echo $current_language['name']; // WPCS: ok. ?>
    		</a></option>
    				<?php foreach ( $other_languages as $code => $name ){
    				?>
    			<option data-iso-country-code="<?php echo esc_attr( $code ); ?>" value="<?php echo esc_url( $url_converter->get_url_for_language($code, false) ); ?>" aria-label="<?php echo esc_attr( $name ); ?>"><a href="<?php echo esc_url( $url_converter->get_url_for_language($code, false) ); ?>" title="<?php echo esc_attr( $name ); ?>">
                <?php echo $name; // WPCS: ok. ?>
            </a></option>
                
    			<?php } ?>
                
    		</select>
    </div>

    Now when i see it in frontpage, I can change to second language and it works. Now when I again click the dropdown and chose the first language (which is now second language), it works however, it adds /#TRPLINKPROCESSED at the last of the site URL.

    For example: http://localhost/nwcsite/page/0/#TRPLINKPROCESSED

    What is causing this. Can you please help me with this. If this is solved then I would have created a fully accessible language selector for people with disabilities.

    THank you in advance

    Thread Starter sagarprasai

    (@sagarprasai)

    Waiting to hear from you

    Plugin Author Cristian Antohe

    (@sareiodata)

    Hi,

    Please replace

    $url_converter->get_url_for_language($code, false)

    with

    $url_converter->get_url_for_language($code, false, '')

    That should solve it.

    Thread Starter sagarprasai

    (@sagarprasai)

    Thank you, Cristian. It is solved now. I now have a fully accessible WCAG compliant, language selector, on my website.
    I just wish when you have some time, you could implement this natively in future updates of Translatepress. As making website accessible for all is everyone’s responsibility πŸ™‚

    Thank you

    Plugin Author Cristian Antohe

    (@sareiodata)

    I’m glad that’s solved. Would you be so kind as to share the code via a https://gist.github.com/ ?

    Perhaps we can include it in a future version.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Creating Accessible Dropdown Language Switcher’ is closed to new replies.