diff options
author | stuebinm | 2021-04-08 18:06:58 +0200 |
---|---|---|
committer | stuebinm | 2021-04-08 18:32:33 +0200 |
commit | 1c5b1e38820f411563efb265b18c4cba6c518074 (patch) | |
tree | fae39036ea35e43f1b66136ad0e6910a2865f7f5 /site/i18n.js | |
parent | 3890d8ac88b2fdfa7b54d433e8e2edf2164be6f0 (diff) |
site: attempt to guess default language
(from the browser's preferences set via navigator.language and
navigator.languages)
Diffstat (limited to 'site/i18n.js')
-rw-r--r-- | site/i18n.js | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/site/i18n.js b/site/i18n.js index 38ffe7b..5f882c5 100644 --- a/site/i18n.js +++ b/site/i18n.js @@ -54,7 +54,6 @@ let locale = "en"; // is found, it returns the key as a fallback value // (therefore, keys should themselves be understandable texts) export function _(key) { - console.log("getting translation for ", locale, key); return !(locale in langs) ? key : !(key in langs[locale]) ? key : langs[locale][key] @@ -64,4 +63,22 @@ export function setLang (loc) { locale = loc; } +/// attempt to set the site's language to the browser's preference. +/// in case it's not supported it tries any of the languages in +/// navigator.languages, and then falls back to english. +export function tryBrowserDefaultLang () { + let guess = navigator.language; + if (guess in langs) { + setLang(lang); + } else { + for (var lang of navigator.languages) { + if (lang in langs) { + setLang (lang); + return; + } + } + setLang("en"); + } +} + export default _; |