-
Notifications
You must be signed in to change notification settings - Fork 107
Browser redirection
When a user comes to the site for the very first time (from an external link or by typing URL), the language is set to the one encoded in the URL used. If the URL does not have language information encoded in it, qTranslate-XT detects the language and may redirect the browser to a canonical URL for the language detected before processing a request, which is done to improve consistency and compatibility with other plugins.
The canonical URL depends on the options:
- Default Language
- Detect Browser Preferred Language
- URL Modification Mode (‘query’, ‘pre-path’, ‘pre-domain’, ‘per-domain’)
- Hide URL language information for default language
Note that options about untranslated content (to hide content or show prefix) do not affect Canonical URL.
Examples:
https://example.com/somepageslug (no language information)
https://example.com/de/somepageslug (german in pre-path mode)
https://example.com/somepageslug?lang=fr (french in query mode)
If the URL does not have language information, then language is determined as follows, by order of priority:
- referrer URL
- cookie (
qtrans_front_language
) - browser setting (if main home page
/
) - default language (as set on Settings/Languages configuration page)
Once language is negotiated, a language cookie qtrans_front_language
is set. The language negotiated will be kept as such during all subsequent browsing. The language is only switched if a URL has new language encoded in it. This can be achieved through language menu, widget or a custom URL with language encoded set by the web administrator/designer to force a language switch.
When option “Hide URL language information for default language” is on, Canonical URL for default language does not have language encoded. Then language will be negotiated as the default one, except when cookie is already set or referrer has different language encoded, to preserve consistency during the browsing. If referrer or cookie specify non-default language, then a redirect to Canonical URL for that language will also occur.
For more info about links and hreflang, see https://support.google.com/webmasters/answer/189077
When SEO robots visit your site, they will get language encoded by URL or otherwise always the default language, since robots do not use cookies or referrer. All pages also set headers ‘<link hreflang=”xx” …>’ and ‘<link hreflang=”x-default” …>’ as recommended by Google. This makes it consistent for SEO purpose, in spite using temporary redirect. Permanent redirect is only useful for permanent move of the pages, but for language switching, it must be temporary redirect, since all urls with or without language encoded are valid and alive.
The nature of a common confusion is due to the dual ambiguity of a URL without language encoded, when option “Hide URL language information for default language” is on. On one hand, one may expect the URL without a language to be always resolved to the default language. On other hand, other users expect to preserve the active language during browsing, even if a URL without language encoded is used. The above is a description of how this ambiguity gets resolved.
Except language switching menu and widgets, all internal links on a page usually encode the active language, unless a link was created by the theme or plugin without passing it through an appropriate filter. Language cookie help to keep the same language then.
Having cookie qtrans_front_language
also helps on AJAX calls, which almost never have language encoded in URL. If option ‘Hide URL language information for default language’ is on, then there is no way to detect active language correctly on AJAX call, unless the cookie is enabled.
If cookies are disabled in browser settings, then the language detection on bare URLs is done based on referrer link, which replaces cookies functionality in most of the cases, including AJAX calls. Without cookies returning users might lose their language previously chosen, but once the language is chosen again, they should be fine.
On admin side, the cookie are always expected to be enabled, as WordPress require, and the current admin language is determined by qtrans_admin_language
cookie or the default language on the first visit. No redirections occur on admin side. The currently active Language Button is determined by browser session storage (no cookie).
Further reading of the original discussion at GitHub may help to those who wish to dig deeper.