index.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import cn from "./cn";
  2. import en from "./en";
  3. import tw from "./tw";
  4. import id from "./id";
  5. import fr from "./fr";
  6. import es from "./es";
  7. import it from "./it";
  8. import tr from "./tr";
  9. import jp from "./jp";
  10. import de from "./de";
  11. import vi from "./vi";
  12. import ru from "./ru";
  13. import no from "./no";
  14. import cs from "./cs";
  15. import ko from "./ko";
  16. import ar from "./ar";
  17. import bn from "./bn";
  18. import { merge } from "../utils/merge";
  19. import type { LocaleType } from "./cn";
  20. export type { LocaleType, PartialLocaleType } from "./cn";
  21. const ALL_LANGS = {
  22. cn,
  23. en,
  24. tw,
  25. jp,
  26. ko,
  27. id,
  28. fr,
  29. es,
  30. it,
  31. tr,
  32. de,
  33. vi,
  34. ru,
  35. cs,
  36. no,
  37. ar,
  38. bn,
  39. };
  40. export type Lang = keyof typeof ALL_LANGS;
  41. export const AllLangs = Object.keys(ALL_LANGS) as Lang[];
  42. export const ALL_LANG_OPTIONS: Record<Lang, string> = {
  43. cn: "简体中文",
  44. en: "English",
  45. tw: "繁體中文",
  46. jp: "日本語",
  47. ko: "한국어",
  48. id: "Indonesia",
  49. fr: "Français",
  50. es: "Español",
  51. it: "Italiano",
  52. tr: "Türkçe",
  53. de: "Deutsch",
  54. vi: "Tiếng Việt",
  55. ru: "Русский",
  56. cs: "Čeština",
  57. no: "Nynorsk",
  58. ar: "العربية",
  59. bn: "বাংলা",
  60. };
  61. const LANG_KEY = "lang";
  62. const DEFAULT_LANG = "en";
  63. const fallbackLang = en;
  64. const targetLang = ALL_LANGS[getLang()] as LocaleType;
  65. // if target lang missing some fields, it will use fallback lang string
  66. merge(fallbackLang, targetLang);
  67. export default fallbackLang as LocaleType;
  68. function getItem(key: string) {
  69. try {
  70. return localStorage.getItem(key);
  71. } catch {
  72. return null;
  73. }
  74. }
  75. function setItem(key: string, value: string) {
  76. try {
  77. localStorage.setItem(key, value);
  78. } catch {}
  79. }
  80. function getLanguage() {
  81. try {
  82. return navigator.language.toLowerCase();
  83. } catch {
  84. return DEFAULT_LANG;
  85. }
  86. }
  87. export function getLang(): Lang {
  88. const savedLang = getItem(LANG_KEY);
  89. if (AllLangs.includes((savedLang ?? "") as Lang)) {
  90. return savedLang as Lang;
  91. }
  92. const lang = getLanguage();
  93. for (const option of AllLangs) {
  94. if (lang.includes(option)) {
  95. return option;
  96. }
  97. }
  98. return DEFAULT_LANG;
  99. }
  100. export function changeLang(lang: Lang) {
  101. setItem(LANG_KEY, lang);
  102. location.reload();
  103. }
  104. export function getISOLang() {
  105. const isoLangString: Record<string, string> = {
  106. cn: "zh-Hans",
  107. tw: "zh-Hant",
  108. };
  109. const lang = getLang();
  110. return isoLangString[lang] ?? lang;
  111. }