index.ts 2.7 KB

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