-
Notifications
You must be signed in to change notification settings - Fork 6
Language suggestion
Pierre Stoffe edited this page Apr 26, 2024
·
1 revision
Sometimes, you may not want your visitors to be automatically redirected to their preferred language. Instead, you only want to let them know that a better language match was found based on their preferences, and let them decide if the redirection is relevant.
Such a thing is possible using the following methods:
{% if craft.languageRedirector.hasLanguageSuggestion() %}
{% set currentLanguage = craft.app.i18n.getLocaleById(craft.app.language) %}
{% set guessedLanguage = craft.languageRedirector.getGuessedLanguage() %}
{% set guessedLanguageInformation = craft.languageRedirector.getInformationForLanguage(guessedLanguage)|first %}
{% if currentLanguage and guessedLanguageInformation %}
<p>{{ 'You are currently visiting the website in ' ~ currentLanguage.displayName|capitalize ~ ' while we think that you may prefer ' ~ guessedLanguageInformation.nativeName|capitalize ~ '.' }}</p>
<a href="{{ guessedLanguageInformation.url }}" hreflang="{{ guessedLanguageInformation.id }}" lang="{{ guessedLanguageInformation.id }}">{{ 'Switch to ' ~ guessedLanguageInformation.nativeName|capitalize ~ '?' }}</a>
{% endif %}
{% endif %}
You may want to check the Troubleshooting section if the code above doesn't work on your project.