Accessible Language Navigation

On a lot of websites users may choose to read contents in their mother tongue. In order to enable them to do so, they are often provided with some sort of navigation containing links to the translated pages. Look at a random Wikipedia article for example: on the left (desktop design) you'll almost always find a navigation linking to translations of the current article.

Here is for an accessible solution, given the fact that someone decided it would make sense to shorten language labels to two-letter abbreviations to safe space for very narrow screens.

Example Navigation

On the coding side of things

The wrapping element needs to be <nav> of course as we are dealing with a navigation of some sort. We have an unordered list of links, so the links are wrapped in list-elements which sit inside an unordered list element. Pretty straight forward I guess. The current language of the page is not wrapped in a link but in a strong tag, so it's already different to the rest without any styling. That way, without CSS and JS, the whole thing is accessible from the start.

We give non-sighted users full names for the abbreviations with aria-label and let assistive tech read it out in the language specified by setting lang="" on the element. Sighted users with a mouse can hover over the abbreviated labels and will see a tooltip thanks to the title attribute on the <abbr> element. We even tell search engines (or their crawlers) what kind of language to expect, when following said links by setting hreflang because poor crawlers, right?

Back to the language label mentioning the language on the page: while wrapping it in strong to make it stand out visually, I also gave it the aria-current="true" attribute, thus screen readers announce: “Current: English . I also set it visually apart from the links inside the navigation by adding a separator, which I've then hidden form assistive technologoy by adding aria-hidden="true" on the element. This is perfectly fine as it doesn't help a non-sighted user at all. Here is a quote from the ARIA specification on aria-hidden :

Authors MAY, with caution, use aria-hidden to hide visibly rendered content from assistive technologies only if the act of hiding this content is intended to improve the experience for users of assistive technologies by removing redundant or extraneous content. Authors using aria-hidden to hide visible content from screen readers MUST ensure that identical or equivalent meaning and functionality is exposed to assistive technologies.

TODO: following the article on accessible tooltips by Heydon Pickering I will make the tooltips not only work on hover but also on focus, as there are a lot of touch-/keyboard-users, who are able to see what they are doing – believe it or not.

What does the HTML look like?

<nav>
    <ul>
        <li>
            <a href="#it"
               hreflang="it"
               lang="it"
               aria-label="Italiano">
                <abbr title="Italiano">IT</abbr>
            </a>
        </li>
        <li>
            <a href="#fr"
               hreflang="fr"
               lang="fr"
               aria-label="Français">
                <abbr title="Français">FR</abbr>
            </a>
        </li>
        <li>
            <a href="#de"
               hreflang="de"
               lang="de"
               aria-label="Deutsch">
                <abbr title="Deutsch">DE</abbr>
            </a>
        </li>
        <li aria-hidden="true"> | </li>
        <li aria-current="true">
            <strong aria-label="English">
                <abbr title="English">EN</abbr>
            </strong>
        </li>
    </ul>
</nav>

On the visual side of things

The current language is visually set to the right, text is bold and yellow. Color contrast is pretty high so there won't be an issue. The pipe separates the current language from the links to the other options.

Some things to keep in mind here

While this pen is a demo for this single component, it takes for granted that you set a lang="xy" attribute on your <html> element in the first place. That way all content on the page will be read by assistive technology in the language specified.

Also it would have been a lot less code, if the language labels were not abbreviated. Always try to give full names when writing, as abbreviations are not always understood or interpreted the same way by everyone. ☝️

Why am I doing this?

As I stated in the introduction, this language navigation component is a common pattern in our job as Web Developers and/or Designers.