From 334037b16109dcc626d746fd7dddda695f748279 Mon Sep 17 00:00:00 2001 From: Xeltica Date: Thu, 23 Jun 2022 00:12:46 +0900 Subject: [PATCH] =?UTF-8?q?=E8=A8=80=E8=AA=9E=E8=A8=AD=E5=AE=9A=E3=81=AE?= =?UTF-8?q?=E5=88=9D=E6=9C=9F=E5=80=A4=E3=81=8C=E3=81=8A=E3=81=8B=E3=81=97?= =?UTF-8?q?=E3=81=84=E4=B8=8D=E5=85=B7=E5=90=88=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/init.tsx | 8 +++++--- src/frontend/langs/index.ts | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/frontend/init.tsx b/src/frontend/init.tsx index e1e2a77..e9c3337 100644 --- a/src/frontend/init.tsx +++ b/src/frontend/init.tsx @@ -15,15 +15,17 @@ import 'dayjs/locale/ja'; dayjs.extend(relativeTime); -if (!localStorage[LOCALSTORAGE_KEY_LANG]) { - localStorage[LOCALSTORAGE_KEY_LANG] = getBrowserLanguage(); +let lng = localStorage[LOCALSTORAGE_KEY_LANG]; + +if (!lng) { + lng = localStorage[LOCALSTORAGE_KEY_LANG] = getBrowserLanguage(); } i18n .use(initReactI18next) .init({ resources, - lng: localStorage[LOCALSTORAGE_KEY_LANG], + lng, interpolation: { escapeValue: false // Reactは常にXSS対策をしてくれるので、i18next側では対応不要 } diff --git a/src/frontend/langs/index.ts b/src/frontend/langs/index.ts index ad4246f..93ef029 100644 --- a/src/frontend/langs/index.ts +++ b/src/frontend/langs/index.ts @@ -31,5 +31,5 @@ export type LanguageCode = keyof typeof resources; export const getBrowserLanguage = () => { const lang = navigator.language.replace('-', '_').toLowerCase(); - return (Object.keys(resources) as LanguageCode[]).map(l => l.toLowerCase()).find(k => k.startsWith(lang)) ?? 'en_US'; + return (Object.keys(resources) as LanguageCode[]).find(k => k.toLowerCase().startsWith(lang)) ?? 'en_US'; };