From c1c40d72da9f242ef175b00ce7491feb2d09797b Mon Sep 17 00:00:00 2001 From: floatingghost Date: Fri, 26 Aug 2022 09:31:25 +0000 Subject: [PATCH] resolve language from navigator langs (#139) Co-authored-by: FloatingGhost Reviewed-on: https://akkoma.dev/AkkomaGang/pleroma-fe/pulls/139 --- src/boot/after_store.js | 16 +++++++++++++++- .../interface_language_switcher.vue | 1 + 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/boot/after_store.js b/src/boot/after_store.js index 2a90ceee..ce93436f 100644 --- a/src/boot/after_store.js +++ b/src/boot/after_store.js @@ -49,6 +49,20 @@ const preloadFetch = async (request) => { } } +const resolveLanguage = (instanceLanguages) => { + // First language in navigator.languages that is listed as an instance language + // falls back to first instance language + const navigatorLanguages = navigator.languages.map((x) => x.split('-')[0]) + + for (const navLanguage of navigatorLanguages) { + if (instanceLanguages.includes(navLanguage)) { + return navLanguage + } + } + + return instanceLanguages[0] +} + const getInstanceConfig = async ({ store }) => { try { const res = await preloadFetch('/api/v1/instance') @@ -61,7 +75,7 @@ const getInstanceConfig = async ({ store }) => { store.dispatch('setInstanceOption', { name: 'accountApprovalRequired', value: data.approval_required }) // don't override cookie if set if (!Cookies.get('userLanguage')) { - store.dispatch('setOption', { name: 'interfaceLanguage', value: data.languages[0] }) + store.dispatch('setOption', { name: 'interfaceLanguage', value: resolveLanguage(data.languages) }) } if (vapidPublicKey) { diff --git a/src/components/interface_language_switcher/interface_language_switcher.vue b/src/components/interface_language_switcher/interface_language_switcher.vue index 7ad1fe2e..51c4a9e3 100644 --- a/src/components/interface_language_switcher/interface_language_switcher.vue +++ b/src/components/interface_language_switcher/interface_language_switcher.vue @@ -1,5 +1,6 @@