Skip to content

Commit

Permalink
chore: updated supported language list and added functionality to fil…
Browse files Browse the repository at this point in the history
…ter languages
  • Loading branch information
likhith-deriv committed Jun 7, 2024
1 parent 7c82be5 commit 09588dd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
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);
});
});
4 changes: 4 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
export const ALL_LANGUAGES = Object.freeze({
ACH: "Translations",
AR: "العربية",
EN: "English",
ES: "Español",
BN: "বাংলা",
DE: "Deutsch",
FR: "Français",
ID: "Indonesian",
IT: "Italiano",
KO: "한국어",
PL: "Polish",
PT: "Português",
SW: "Kiswahili",
RU: "Русский",
SI: "සිංහල",
TR: "Türkçe",
VI: "Tiếng Việt",
ZH_CN: "简体中文",
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;
};

0 comments on commit 09588dd

Please sign in to comment.