From b4b11a470f7123ed6619a224f8cd0ad92381a3b5 Mon Sep 17 00:00:00 2001 From: Yifei Zhang Date: Mon, 20 Mar 2023 16:25:27 +0000 Subject: [PATCH] fix: build errors --- app/locales/index.ts | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/app/locales/index.ts b/app/locales/index.ts index 1398a584..07bf84c6 100644 --- a/app/locales/index.ts +++ b/app/locales/index.ts @@ -6,14 +6,37 @@ export type { LocaleType } from './cn' type Lang = 'en' | 'cn' const LANG_KEY = 'lang' + +function getItem(key: string) { + try { + return localStorage.getItem(key) + } finally { + return null + } +} + +function setItem(key: string, value: string) { + try { + localStorage.setItem(key, value) + } finally { } +} + +function getLanguage() { + try { + return navigator.language.toLowerCase() + } finally { + return 'cn' + } +} + export function getLang(): Lang { - const savedLang = localStorage?.getItem(LANG_KEY) + const savedLang = getItem(LANG_KEY) if (['en', 'cn'].includes(savedLang ?? '')) { return savedLang as Lang } - const lang = navigator.language.toLowerCase() + const lang = getLanguage() if (lang.includes('zh') || lang.includes('cn')) { return 'cn' @@ -23,7 +46,7 @@ export function getLang(): Lang { } export function changeLang(lang: Lang) { - localStorage.setItem(LANG_KEY, lang) + setItem(LANG_KEY, lang) location.reload() }