Skip to content

Instantly share code, notes, and snippets.

@nico2che
Last active September 30, 2019 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nico2che/85441e50523d1a1d007b706533e3cd1e to your computer and use it in GitHub Desktop.
Save nico2che/85441e50523d1a1d007b706533e3cd1e to your computer and use it in GitHub Desktop.

Weglot x Cookie Information Proposal

This draft explains how Weglot and Cookie Information can interact together.

Problem

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.

Proposal

On load method

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";
}

On change method

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, then getDataCulture 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment