From 4598d83c6fb25440ebe81f2ebbfb2e2645f2653b Mon Sep 17 00:00:00 2001 From: magicfelix Date: Mon, 28 Jun 2021 17:04:38 +0200 Subject: [PATCH] Autodiscover preferred browser language, enhances #4 --- .gitignore | 1 + config.js.dist | 2 ++ index.html | 1 + scripts/main.js | 14 +++++++++++++- 4 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 config.js.dist diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1bf4259 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.js diff --git a/config.js.dist b/config.js.dist new file mode 100644 index 0000000..5afc4c5 --- /dev/null +++ b/config.js.dist @@ -0,0 +1,2 @@ +var supportedLanguages = ['de', 'en', 'fr', 'ro'] +var defaultLanguage = 'en' diff --git a/index.html b/index.html index bcfbe0a..cc1ddec 100644 --- a/index.html +++ b/index.html @@ -37,6 +37,7 @@

+ diff --git a/scripts/main.js b/scripts/main.js index 535a137..4f7c15f 100644 --- a/scripts/main.js +++ b/scripts/main.js @@ -94,7 +94,19 @@ i18n.once(I18nText.event.LOCALE_CHANGE, function (data) { rehash(); }); - i18n.setLocale('en'); + + var preferredLocale, setLocale = false; + for (preferredLocale of navigator.languages) { + if (supportedLanguages.includes(preferredLocale)) { + i18n.setLocale(preferredLocale); + setLocale = true; + break; + } + } + if (!setLocale) { + i18n.setLocale(defaultLocale); + } + // functionality if (navigator.userAgent.indexOf("Android") >= 0) {