diff --git a/src/helpers/detect-device.ts b/src/helpers/detect-device.ts index 4a9e516..e50f74e 100644 --- a/src/helpers/detect-device.ts +++ b/src/helpers/detect-device.ts @@ -8,7 +8,7 @@ export const windowIsDefined = () => { export const getDevice = (): 'mobile' | 'pc' => { return windowIsDefined() && - /Android|webOS|iPhone|iPod|BlackBerry/i.test(window.navigator.userAgent) + /Android|webOS|iPhone|iPod|BlackBerry/i.test(window.navigator?.userAgent) ? 'mobile' : 'pc'; }; diff --git a/src/i18n/language.ts b/src/i18n/language.ts index 87f5c90..7cbabaf 100644 --- a/src/i18n/language.ts +++ b/src/i18n/language.ts @@ -1,3 +1,4 @@ +import { windowIsDefined } from '@/helpers/detect-device'; import { getSearchObj } from '@/helpers/location'; /** @@ -5,7 +6,10 @@ import { getSearchObj } from '@/helpers/location'; */ export function getLanguage(): string { const query = getSearchObj(); - const lang = (query.lang as string) || navigator.language || 'zh-CN'; + const lang = + (query.lang as string) || + (windowIsDefined() ? navigator?.language : 'zh-CN') || + 'zh-CN'; typeof document !== 'undefined' && document.body.setAttribute('lang', lang); return lang; }