i18n.ts 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import LanguageDetector from "i18next-browser-languagedetector";
  2. export const languages = [
  3. { lng: "en", label: "English", data: require("./locales/en.json") },
  4. { lng: "bg-BG", label: "Български", data: require("./locales/bg-BG.json") },
  5. { lng: "de-DE", label: "Deutsch", data: require("./locales/de-DE.json") },
  6. { lng: "es-ES", label: "Español", data: require("./locales/es-ES.json") },
  7. { lng: "ca-ES", label: "Catalan", data: require("./locales/ca-ES.json") },
  8. { lng: "el-GR", label: "Ελληνικά", data: require("./locales/el-GR.json") },
  9. { lng: "fr-FR", label: "Français", data: require("./locales/fr-FR.json") },
  10. {
  11. lng: "id-ID",
  12. label: "Bahasa Indonesia",
  13. data: require("./locales/id-ID.json"),
  14. },
  15. { lng: "it-IT", label: "Italiano", data: require("./locales/it-IT.json") },
  16. { lng: "hu-HU", label: "Magyar", data: require("./locales/hu-HU.json") },
  17. { lng: "nl-NL", label: "Nederlands", data: require("./locales/nl-NL.json") },
  18. { lng: "no-No", label: "Norsk", data: require("./locales/no-NO.json") },
  19. { lng: "pl-PL", label: "Polski", data: require("./locales/pl-PL.json") },
  20. { lng: "pt-PT", label: "Português", data: require("./locales/pt-PT.json") },
  21. { lng: "ru-RU", label: "Русский", data: require("./locales/ru-RU.json") },
  22. { lng: "uk-UA", label: "Українська", data: require("./locales/uk-UA.json") },
  23. { lng: "fi-FI", label: "Suomi", data: require("./locales/fi-FI.json") },
  24. { lng: "tr-TR", label: "Türkçe", data: require("./locales/tr-TR.json") },
  25. { lng: "ja-JP", label: "日本語", data: require("./locales/ja-JP.json") },
  26. { lng: "ko-KR", label: "한국어", data: require("./locales/ko-KR.json") },
  27. { lng: "zh-TW", label: "繁體中文", data: require("./locales/zh-TW.json") },
  28. { lng: "zh-CN", label: "简体中文", data: require("./locales/zh-CN.json") },
  29. {
  30. lng: "ar-SA",
  31. label: "العربية",
  32. data: require("./locales/ar-SA.json"),
  33. rtl: true,
  34. },
  35. {
  36. lng: "he-IL",
  37. label: "עברית",
  38. data: require("./locales/he-IL.json"),
  39. rtl: true,
  40. },
  41. ];
  42. let currentLanguage = languages[0];
  43. const fallbackLanguage = languages[0];
  44. export const setLanguage = (newLng: string | undefined) => {
  45. currentLanguage =
  46. languages.find((language) => language.lng === newLng) || fallbackLanguage;
  47. document.documentElement.dir = currentLanguage.rtl ? "rtl" : "ltr";
  48. languageDetector.cacheUserLanguage(currentLanguage.lng);
  49. };
  50. export const getLanguage = () => currentLanguage;
  51. const findPartsForData = (data: any, parts: string[]) => {
  52. for (var i = 0; i < parts.length; ++i) {
  53. const part = parts[i];
  54. if (data[part] === undefined) {
  55. return undefined;
  56. }
  57. data = data[part];
  58. }
  59. if (typeof data !== "string") {
  60. return undefined;
  61. }
  62. return data;
  63. };
  64. export const t = (path: string, replacement?: { [key: string]: string }) => {
  65. const parts = path.split(".");
  66. let translation =
  67. findPartsForData(currentLanguage.data, parts) ||
  68. findPartsForData(fallbackLanguage.data, parts);
  69. if (translation === undefined) {
  70. throw new Error(`Can't find translation for ${path}`);
  71. }
  72. if (replacement) {
  73. for (var key in replacement) {
  74. translation = translation.replace(`{{${key}}}`, replacement[key]);
  75. }
  76. }
  77. return translation;
  78. };
  79. const languageDetector = new LanguageDetector();
  80. languageDetector.init({
  81. languageUtils: {
  82. formatLanguageCode: (lng: string) => lng,
  83. isWhitelisted: () => true,
  84. },
  85. checkWhitelist: false,
  86. });
  87. setLanguage(languageDetector.detect());