-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
67 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,57 @@ | ||
import CN from './cn' | ||
import EN from './en' | ||
import TW from './tw' | ||
import CN from "./cn"; | ||
import EN from "./en"; | ||
import TW from "./tw"; | ||
|
||
export type { LocaleType } from './cn' | ||
export type { LocaleType } from "./cn"; | ||
|
||
type Lang = 'en' | 'cn' | 'tw' | ||
export const AllLangs = ["en", "cn", "tw"] as const; | ||
type Lang = typeof AllLangs[number]; | ||
|
||
const LANG_KEY = 'lang' | ||
const LANG_KEY = "lang"; | ||
|
||
function getItem(key: string) { | ||
try { | ||
return localStorage.getItem(key) | ||
} catch { | ||
return null | ||
} | ||
try { | ||
return localStorage.getItem(key); | ||
} catch { | ||
return null; | ||
} | ||
} | ||
|
||
function setItem(key: string, value: string) { | ||
try { | ||
localStorage.setItem(key, value) | ||
} catch { } | ||
try { | ||
localStorage.setItem(key, value); | ||
} catch {} | ||
} | ||
|
||
function getLanguage() { | ||
try { | ||
return navigator.language.toLowerCase() | ||
} catch { | ||
return 'cn' | ||
} | ||
try { | ||
return navigator.language.toLowerCase(); | ||
} catch { | ||
return "cn"; | ||
} | ||
} | ||
|
||
export function getLang(): Lang { | ||
const savedLang = getItem(LANG_KEY) | ||
const savedLang = getItem(LANG_KEY); | ||
|
||
if (['en', 'cn', 'tw'].includes(savedLang ?? '')) { | ||
return savedLang as Lang | ||
} | ||
if (AllLangs.includes((savedLang ?? "") as Lang)) { | ||
return savedLang as Lang; | ||
} | ||
|
||
const lang = getLanguage() | ||
const lang = getLanguage(); | ||
|
||
if (lang.includes('zh') || lang.includes('cn')) { | ||
return 'cn' | ||
} else if (lang.includes('tw')) { | ||
return 'tw' | ||
} else { | ||
return 'en' | ||
} | ||
if (lang.includes("zh") || lang.includes("cn")) { | ||
return "cn"; | ||
} else if (lang.includes("tw")) { | ||
return "tw"; | ||
} else { | ||
return "en"; | ||
} | ||
} | ||
|
||
export function changeLang(lang: Lang) { | ||
setItem(LANG_KEY, lang) | ||
location.reload() | ||
setItem(LANG_KEY, lang); | ||
location.reload(); | ||
} | ||
|
||
export default { en: EN, cn: CN, tw: TW }[getLang()] | ||
export default { en: EN, cn: CN, tw: TW }[getLang()]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters