i18n.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import i18n, { FallbackLng, FallbackLngObjList } from "i18next";
  2. import LanguageDetector from "i18next-browser-languagedetector";
  3. import toast from "react-hot-toast";
  4. import { initReactI18next } from "react-i18next";
  5. export const availableLocales = [
  6. "ar",
  7. "de",
  8. "en",
  9. "es",
  10. "fr",
  11. "hi",
  12. "hr",
  13. "it",
  14. "ja",
  15. "ko",
  16. "nl",
  17. "pl",
  18. "pt-BR",
  19. "ru",
  20. "sl",
  21. "sv",
  22. "tr",
  23. "uk",
  24. "vi",
  25. "zh-Hans",
  26. "zh-Hant",
  27. ] as const;
  28. const fallbacks = {
  29. "zh-HK": ["zh-Hant", "en"],
  30. "zh-TW": ["zh-Hant", "en"],
  31. zh: ["zh-Hans", "en"],
  32. } as FallbackLngObjList;
  33. i18n
  34. .use(LanguageDetector)
  35. .use(initReactI18next)
  36. .init({
  37. detection: {
  38. order: ["navigator"],
  39. },
  40. fallbackLng: {
  41. ...fallbacks,
  42. ...{ default: ["en"] },
  43. } as FallbackLng,
  44. });
  45. for (const locale of availableLocales) {
  46. import(`./locales/${locale}.json`)
  47. .then((translation) => {
  48. i18n.addResourceBundle(locale, "translation", translation.default, true, true);
  49. })
  50. .catch((err) => {
  51. toast.error(`Failed to load locale "${locale}".\n${err}`, { duration: 5000 });
  52. });
  53. }
  54. export default i18n;
  55. export type TLocale = typeof availableLocales[number];