From 09588dd645e8873a05fd695f6ca5619a85ab07ff Mon Sep 17 00:00:00 2001 From: Likhith Kolayari Date: Fri, 7 Jun 2024 08:44:20 +0400 Subject: [PATCH 1/9] chore: updated supported language list and added functionality to filter languages --- src/utils/__tests__/lang-utils.spec.ts | 19 +++++++++++++++++++ src/utils/constants.ts | 4 ++++ src/utils/lang-utils.ts | 25 ++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 src/utils/__tests__/lang-utils.spec.ts diff --git a/src/utils/__tests__/lang-utils.spec.ts b/src/utils/__tests__/lang-utils.spec.ts new file mode 100644 index 0000000..4fb3f4f --- /dev/null +++ b/src/utils/__tests__/lang-utils.spec.ts @@ -0,0 +1,19 @@ +import { ALL_LANGUAGES } from "../constants"; +import { getAllowedLanguages } from "../lang-utils"; + +describe("getAllowedLanguages", () => { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const { ACH, ...languageList } = ALL_LANGUAGES; + + it("should return all languages if no excluded languages are provided", () => { + const allowedLanguages = getAllowedLanguages(); + expect(allowedLanguages).toEqual(languageList); + }); + + it("should return all languages except the excluded languages (ex: ID)", () => { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const { ID, ...filteredLanguages } = languageList; + const allowedLanguages = getAllowedLanguages(["ID"]); + expect(allowedLanguages).toEqual(filteredLanguages); + }); +}); diff --git a/src/utils/constants.ts b/src/utils/constants.ts index efdcae2..239e7e6 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -1,7 +1,9 @@ export const ALL_LANGUAGES = Object.freeze({ ACH: "Translations", + AR: "العربية", EN: "English", ES: "Español", + BN: "বাংলা", DE: "Deutsch", FR: "Français", ID: "Indonesian", @@ -9,7 +11,9 @@ export const ALL_LANGUAGES = Object.freeze({ KO: "한국어", PL: "Polish", PT: "Português", + SW: "Kiswahili", RU: "Русский", + SI: "සිංහල", TR: "Türkçe", VI: "Tiếng Việt", ZH_CN: "简体中文", diff --git a/src/utils/lang-utils.ts b/src/utils/lang-utils.ts index ea3067a..aa5f8a7 100644 --- a/src/utils/lang-utils.ts +++ b/src/utils/lang-utils.ts @@ -1,5 +1,7 @@ import { constants } from "@utils/index"; +type LanguageCode = keyof typeof constants.ALL_LANGUAGES; + export const getInitialLanguage = () => { const url_params = new URLSearchParams(window.location.search); const query_lang = url_params.get("lang"); @@ -18,9 +20,7 @@ export const getInitialLanguage = () => { return constants.DEFAULT_LANGUAGE; }; -export const loadIncontextTranslation = ( - lang: keyof typeof constants.ALL_LANGUAGES -) => { +export const loadIncontextTranslation = (lang: LanguageCode) => { const is_ach = lang.toUpperCase() === "ACH"; if (is_ach) { const jipt = document.createElement("script"); @@ -34,3 +34,22 @@ export const loadIncontextTranslation = ( document.head.appendChild(jipt); } }; + +/** + * Filter out unsupported languages and return an Object containing language code and language name + * @param excludedLanguages + * @returns Object containing language code and language name + */ +export const getAllowedLanguages = ( + excludedLanguages: Omit[] = [] +) => { + const unsupportedLanguages = ["ACH", ...excludedLanguages]; + const languageList = Object.keys(constants.ALL_LANGUAGES) + .filter((key) => !unsupportedLanguages.includes(key)) + .reduce((obj: { [key: string]: string }, key) => { + obj[key] = constants.ALL_LANGUAGES[key as LanguageCode]; + return obj; + }, {}); + + return languageList; +}; From 00adc455b040a12eb775306147d3500d6a16b412 Mon Sep 17 00:00:00 2001 From: Likhith Kolayari Date: Fri, 7 Jun 2024 15:51:08 +0400 Subject: [PATCH 2/9] fix: compatible with TS 4.6 --- package.json | 6 ------ 1 file changed, 6 deletions(-) diff --git a/package.json b/package.json index 4bc0e57..d84e9c1 100644 --- a/package.json +++ b/package.json @@ -9,12 +9,6 @@ "bin": { "deriv-extract-translations": "./dist/deriv-extract-translations.cjs" }, - "exports": { - ".": { - "import": "./dist/index.js", - "types": "./dist/index.d.ts" - } - }, "private": false, "version": "1.2.4", "scripts": { From 69c146372e40456a924b501358871d53545b0d87 Mon Sep 17 00:00:00 2001 From: Likhith Kolayari Date: Fri, 7 Jun 2024 21:10:56 +0400 Subject: [PATCH 3/9] fix: remove jsx-runtime from externals --- vite.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vite.config.ts b/vite.config.ts index 0fe3506..21bb682 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -15,7 +15,7 @@ export default defineConfig({ formats: ["es"], }, rollupOptions: { - external: ["react", "react-dom", "react/jsx-runtime", "react-dom/client"], + external: ["react", "react-dom", "react-dom/client"], output: { assetFileNames: "assets/[name]", entryFileNames: "[name].js", From e3db3715cd651e0e03257612ffac01fb0d225d50 Mon Sep 17 00:00:00 2001 From: Likhith Kolayari Date: Sun, 9 Jun 2024 19:44:39 +0400 Subject: [PATCH 4/9] fix: exported new function from lib --- src/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 0fba571..3dda466 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,8 @@ export { localize, getInitialLanguage, loadIncontextTranslation, - initializeI18n + getAllowedLanguages, + initializeI18n, } from "@utils/index"; export { Localize } from "@components/index"; export { useTranslations } from "@hooks/index"; From 3f5732d9a0084f9d091c15358d5d66e02eb7217a Mon Sep 17 00:00:00 2001 From: Likhith Kolayari Date: Mon, 10 Jun 2024 10:29:04 +0400 Subject: [PATCH 5/9] fix: removed the initialization event check --- src/provider/translation-provider.tsx | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/provider/translation-provider.tsx b/src/provider/translation-provider.tsx index 1238ca6..196094d 100644 --- a/src/provider/translation-provider.tsx +++ b/src/provider/translation-provider.tsx @@ -31,15 +31,18 @@ export default function TranslationProvider({ useEffect(() => { if (i18nInstance) { + console.log("Library: i18nInstance initialized."); const initialLang = i18nInstance.language || defaultLang || ""; setCurrentLanguage(initialLang); - - i18nInstance.on("initialized", () => { - setIsTranslationsLoaded(true); - }); + setIsTranslationsLoaded(true); } }, [i18nInstance, defaultLang]); + console.log("Library: ", { + i18nInstance, + isTranslationsLoaded, + }); + if (!i18nInstance || !isTranslationsLoaded) return null; return ( From 65285cfc421b5709a3d2a8465968e04f957e9c9d Mon Sep 17 00:00:00 2001 From: Likhith Kolayari Date: Mon, 10 Jun 2024 11:07:09 +0400 Subject: [PATCH 6/9] fix: removed the initialization event check --- src/provider/translation-provider.tsx | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/provider/translation-provider.tsx b/src/provider/translation-provider.tsx index 196094d..f3f93b6 100644 --- a/src/provider/translation-provider.tsx +++ b/src/provider/translation-provider.tsx @@ -31,18 +31,12 @@ export default function TranslationProvider({ useEffect(() => { if (i18nInstance) { - console.log("Library: i18nInstance initialized."); const initialLang = i18nInstance.language || defaultLang || ""; setCurrentLanguage(initialLang); setIsTranslationsLoaded(true); } }, [i18nInstance, defaultLang]); - console.log("Library: ", { - i18nInstance, - isTranslationsLoaded, - }); - if (!i18nInstance || !isTranslationsLoaded) return null; return ( From 3c3d088f8fbab696ece00fcb9bd943c190ac408f Mon Sep 17 00:00:00 2001 From: Likhith Kolayari Date: Mon, 10 Jun 2024 14:18:48 +0400 Subject: [PATCH 7/9] chore: added supported languages --- src/utils/constants.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 239e7e6..18f50b8 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -1,10 +1,10 @@ export const ALL_LANGUAGES = Object.freeze({ ACH: "Translations", - AR: "العربية", EN: "English", - ES: "Español", + AR: "العربية", BN: "বাংলা", DE: "Deutsch", + ES: "Español", FR: "Français", ID: "Indonesian", IT: "Italiano", @@ -14,11 +14,11 @@ export const ALL_LANGUAGES = Object.freeze({ SW: "Kiswahili", RU: "Русский", SI: "සිංහල", + TH: "ไทย", TR: "Türkçe", VI: "Tiếng Việt", ZH_CN: "简体中文", ZH_TW: "繁體中文", - TH: "ไทย", }); export const LANGUAGE_KEY = "i18n_language"; From 51d7b7cdf59c04aa295e0102e41a18aa57061af4 Mon Sep 17 00:00:00 2001 From: Likhith Kolayari Date: Mon, 10 Jun 2024 14:21:44 +0400 Subject: [PATCH 8/9] chore: added supported languages --- src/utils/constants.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 18f50b8..51c35cd 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -8,10 +8,10 @@ export const ALL_LANGUAGES = Object.freeze({ FR: "Français", ID: "Indonesian", IT: "Italiano", + SW: "Kiswahili", KO: "한국어", PL: "Polish", PT: "Português", - SW: "Kiswahili", RU: "Русский", SI: "සිංහල", TH: "ไทย", From 672575afa65ab006c2fe0cd7d5a47e131cddd8d1 Mon Sep 17 00:00:00 2001 From: Likhith Kolayari Date: Mon, 10 Jun 2024 15:20:00 +0400 Subject: [PATCH 9/9] chore: added support for config override --- src/utils/initialize-i18n.ts | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/utils/initialize-i18n.ts b/src/utils/initialize-i18n.ts index e8809dd..2b26991 100644 --- a/src/utils/initialize-i18n.ts +++ b/src/utils/initialize-i18n.ts @@ -1,29 +1,42 @@ -import i18next from "i18next"; +import i18next, { InitOptions } from "i18next"; import { str as crc32 } from "crc-32"; import { OtaSdk } from "@utils/index"; import { initReactI18next } from "react-i18next"; import { getInitialLanguage } from "@utils/index"; -const i18n_config = { +type TInstanceConfig = { + useSuspense?: Exclude["useSuspense"]; + enableDebug?: InitOptions["debug"]; +}; + +const setI18Config = ({ useSuspense, enableDebug }: TInstanceConfig) => ({ react: { hashTransKey(defaultValue: string) { return crc32(defaultValue); }, - useSuspense: true, + useSuspense, }, - debug: false, + debug: enableDebug, initImmediate: true, fallbackLng: "EN", interpolation: { escapeValue: false, }, -}; +}); -export default function initializeI18n({ cdnUrl }: { cdnUrl: string }) { +export default function initializeI18n({ + cdnUrl, + useSuspense = true, + enableDebug = false, +}: { + cdnUrl: string; +} & TInstanceConfig) { const module = new OtaSdk(cdnUrl); const initial_language = getInitialLanguage(); + const i18n_config = setI18Config({ useSuspense, enableDebug }); + i18next .use(module) .use(initReactI18next)