Language switcher for a multilingual Drupal 8 site

Parent Feed: 

Recently one of our clients asked us to come up with a better language detection and redirection solution for their multilingual Drupal 8 site. Most out of the box solutions do not provide great user experience and IP based detection do not always work as expected. Browser based redirection also not an ideal option since at some point a visitor might want to manually choose wich language they want to see.

Having this issue in hand I started looking into possible solutions, I looked at number of multilingual Drupal and non-Drupal sites and couldn't find anything that would work for our client. I thought what if we ask a visitor what to do by showing them a box with browser detected langauge. This is just as Chrome's transaltion prompt that asks you if you'd like to translate the site. The prompt that is very simple and not as annyoing as some auto redirect solutions.

So this is what I came up with. A simple to use Drupal 8 module with few configurations needed. I decided to call it Language Suggestion.

The module supports auto redirects based on visitor selection and custom prompt messages per langauge. Here is the full list of configurable options:

  • Container CSS element. This is where you would need to specify main page container. You may need this when you have some additional message box at the top of the page, e.g. Cookie usage etc.. By default this is set to "body" HTML tag.
  • Always redirect. This option adds auto redirect to the website based on previous suggested language selection.
  • Language switch CSS element. This option overrides auto language redirect. For instance when use after auto redirect still wants to switch to another language they will be able to do so. Make sure to specify langauge switch element class name or ID.
  • Language suggestion delay. This option allows the language suggestion box to appear with delay. The value is in seconds.
  • Dismiss Delay. This option indicates how long language suggestion box should be hidden and when to reappear.
  • The broser language mapping is the mapping where you specify when to show language suggestion.
Language Suggestion Drupal 8 module

This is how language suggestion box appears on the site:

Langauge Suggestion prompt box

Configuration page is located at (Administration > Configuration > Regional and language > Language Suggestion) /admin/config/regional/language-suggestion

To test the module please make sure you have only one language in your browser settings. The language should be different from your current website language. And make sure that langauge is enabled in your Drupal 8 site.

I tried to keep visitors interaction annoyance level with the langauge suggestion prompt to its minimum. I hope this solution could be useful for you and your clients or give you some ideas.

Download Language Suggestion from Github

Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web