This draft explains how Weglot and Cookie Information can interact together.
Example Website : https://www.rhanders.com/
In this example website, the original language is EN. If a visitor choose the DA language, the cookie information banner will not display in the right language.
First of all, Weglot has a public function available in Weglot
global object when app is
installed: Weglot.getCurrentLang()
:
Weglot.getCurrentLang(): string
This can be used on the first load to get the current language if Weglot is already initialized. It returns the language code (2 letters, eg. fr, da, es, de, en...).
Example, in your getDataCulture
function;
function getDataCulture() {
return ... || (window.Weglot && window.Weglot.getCurrentLang()) || "default";
}
Secondly, when the user change the current language we need to translate again the banner with the new
language. Here, we propose an event-driven solution with our method: Weglot.on("languageChanged", callback)
If the current language on the page is English:
Weglot.on("languageChanged", function(lang) {
console.log("User changed language with", lang);
})
In the callback function, the first argument is the new language code selected by the user. You could get
this language code with the first method too: Weglot.getCurrentLang()
, so a solution could be:
if (window.Weglot) {
Weglot.on("languageChanged", loadCategoriesConsent);
}
- Detect Weglot presence;
- When language change, call your
loadCategoriesConsent
function which should delete current cookie banner getCookieCategoriesTemplateUrl
should be called, thengetDataCulture
to get current language- Recreate cookie banner with the new language in
loadCategoriesConsent
Reference in the developer documentation: https://developers.weglot.com/integrations/javascript/client-side-api