Hello. I am using Botpress Cloud. I am trying to d...
# 💻developers
k
Hello. I am using Botpress Cloud. I am trying to detect the browser language settings using the following piece of code but I am getting the error "ReferenceError, navigator is not defined". Note I am aware it's possible to detect the user language after he typed his first message. The idea here is to write the welcoming message in the language defined in the user browser. Note this code works perfectly in developer console. But... is this even possible? @acceptable-kangaroo-64719 @rich-battery-69172
Copy code
function getBrowserLocales(options = {}) {
  const defaultOptions = {
    languageCodeOnly: false,
  };
  const opt = {
    ...defaultOptions,
    ...options,
  };
  const browserLocales =
    navigator.languages === undefined
      ? [navigator.language]
      : navigator.languages;
  if (!browserLocales) {
    return undefined;
  }
  return browserLocales.map(locale => {
    const trimmedLocale = locale.trim();
    return opt.languageCodeOnly
      ? trimmedLocale.split(/-|_/)[0]
      : trimmedLocale;
  });
}

workflow.detectedLanguage = getBrowserLocales()
console.log(workflow.detectedLanguage)
2 Views