index.ts 2.4 KB

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