index.ts 2.7 KB

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