Skip to content

Commit

Permalink
Merge pull request #41 from likhith-deriv/likhith/function-to-filter-…
Browse files Browse the repository at this point in the history
…languages

chore: updated supported language list and language utility
  • Loading branch information
ali-hosseini-deriv committed Jun 11, 2024
2 parents 7c82be5 + 672575a commit a757c80
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 23 deletions.
6 changes: 0 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ export {
localize,
getInitialLanguage,
loadIncontextTranslation,
initializeI18n
getAllowedLanguages,
initializeI18n,
} from "@utils/index";
export { Localize } from "@components/index";
export { useTranslations } from "@hooks/index";
Expand Down
5 changes: 1 addition & 4 deletions src/provider/translation-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ export default function TranslationProvider({
if (i18nInstance) {
const initialLang = i18nInstance.language || defaultLang || "";
setCurrentLanguage(initialLang);

i18nInstance.on("initialized", () => {
setIsTranslationsLoaded(true);
});
setIsTranslationsLoaded(true);
}
}, [i18nInstance, defaultLang]);

Expand Down
19 changes: 19 additions & 0 deletions src/utils/__tests__/lang-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
8 changes: 6 additions & 2 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
export const ALL_LANGUAGES = Object.freeze({
ACH: "Translations",
EN: "English",
ES: "Español",
AR: "العربية",
BN: "বাংলা",
DE: "Deutsch",
ES: "Español",
FR: "Français",
ID: "Indonesian",
IT: "Italiano",
SW: "Kiswahili",
KO: "한국어",
PL: "Polish",
PT: "Português",
RU: "Русский",
SI: "සිංහල",
TH: "ไทย",
TR: "Türkçe",
VI: "Tiếng Việt",
ZH_CN: "简体中文",
ZH_TW: "繁體中文",
TH: "ไทย",
});

export const LANGUAGE_KEY = "i18n_language";
Expand Down
25 changes: 19 additions & 6 deletions src/utils/initialize-i18n.ts
Original file line number Diff line number Diff line change
@@ -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<InitOptions["react"], undefined>["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)
Expand Down
25 changes: 22 additions & 3 deletions src/utils/lang-utils.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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");
Expand All @@ -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<LanguageCode, "ACH">[] = []
) => {
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;
};
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit a757c80

Please sign in to comment.