Skip to content

Commit

Permalink
CL-468 Split language list (#46)
Browse files Browse the repository at this point in the history
* CL-468 Split Language config

* CL-468 Fix typos
  • Loading branch information
slaviczavik authored Nov 13, 2024
1 parent 70f7eb7 commit dca1f82
Showing 1 changed file with 36 additions and 23 deletions.
59 changes: 36 additions & 23 deletions src/language.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type LanguageInfo = {
/**
* Two letter ISO code, such as `"en"` for English language.
* Two-letter ISO code, such as `"en"` for English language.
* Can be `null` if the language is a flag to be evaluated at runtime,
* as it is the case for some "modes".
*/
Expand All @@ -25,7 +25,7 @@ export type LanguageInfo = {

/**
* Some language descriptions corresponds to "modes" rather than to actual languages.
* For instance the "visitor" mode consists in displaying bylingual labels.
* For instance the "visitor" mode consists in displaying bilingual labels.
*/
isMode: boolean;

Expand All @@ -36,9 +36,9 @@ export type LanguageInfo = {
};

/**
* The complete list of languages
* List of "non-ISO specific" languages.
*/
export const Language = {
export const NonISOLanguage = {
/**
* Language mode to display the labels in the end user's device language.
*/
Expand Down Expand Up @@ -88,7 +88,12 @@ export const Language = {
isMode: false,
geocoding: false,
} as LanguageInfo,
} as const

/**
* List of "country specific" languages.
*/
export const ISOLanguage = {
/**
* Amharic language
*/
Expand Down Expand Up @@ -1012,33 +1017,41 @@ export const Language = {
isMode: false,
geocoding: false,
} as LanguageInfo,
} as const

/**
* The complete list of languages
*/
export const Language = {
...NonISOLanguage,
...ISOLanguage
} as const;

/**
* Get language infos from a provided language key, the key being the no-whitespace capital name.
* By default, the language dictionnary to look into is the one defined in this library, but another one could be provided
* By default, the language dictionary to look into is the one defined in this library, but another one could be provided
* Returns `null` if not found.
*/
export function getLanguageInfoFromKey(
languageKey: string,
languageDictionnary: { [k: string]: LanguageInfo } = Language,
languageDictionary: { [k: string]: LanguageInfo } = Language,
): LanguageInfo | null {
if (languageKey in languageDictionnary) {
if (languageKey in languageDictionary) {
return languageKey[languageKey];
}
return null;
}

/**
* Get the language info from a provided 2-character iso code.
* By default, the language dictionnary to look into is the one defined in this library, but another one could be provided
* By default, the language dictionary to look into is the one defined in this library, but another one could be provided
* Returns `null` if not found.
*/
export function getLanguageInfoFromCode(
languageCode: string,
languageDictionnary: { [k: string]: LanguageInfo } = Language,
languageDictionary: { [k: string]: LanguageInfo } = Language,
): LanguageInfo | null {
for (const lang of Object.values(languageDictionnary)) {
for (const lang of Object.values(languageDictionary)) {
if (lang.code === languageCode) {
return lang;
}
Expand All @@ -1047,16 +1060,16 @@ export function getLanguageInfoFromCode(
}

/**
* Get the language info from a language flag (eg. `"name:en"`).
* Get the language info from a language flag (e.g. `"name:en"`).
* This is also handy to check is a given language flag is a supported language.
* By default, the language dictionnary to look into is the one defined in this library, but another one could be provided
* By default, the language dictionary to look into is the one defined in this library, but another one could be provided
* Returns `null` if not found.
*/
export function getLanguageInfoFromFlag(
languageFlag: string,
languageDictionnary: { [k: string]: LanguageInfo } = Language,
languageDictionary: { [k: string]: LanguageInfo } = Language,
): LanguageInfo | null {
for (const lang of Object.values(languageDictionnary)) {
for (const lang of Object.values(languageDictionary)) {
if (lang.flag === languageFlag) {
return lang;
}
Expand Down Expand Up @@ -1103,17 +1116,17 @@ export function isLanguageInfo(obj: unknown): obj is LanguageInfo {
}

/**
* By default, the language dictionnary to look into is the one defined in this library, but another one could be provided
* By default, the language dictionary to look into is the one defined in this library, but another one could be provided
*/
export function toLanguageInfo(
lang: LanguageInfo | string,
languageDictionnary: { [k: string]: LanguageInfo } = Language,
languageDictionary: { [k: string]: LanguageInfo } = Language,
): LanguageInfo | null {
// Could be directly an object of type LanguageInfo
if (isLanguageInfo(lang)) {
// Yet we want to make sure the provided languageInfo obj is not corrupted or incomplete,
// so we ask for the equivalent original:
return getLanguageInfoFromFlag(lang.flag, languageDictionnary); // possibly returns null, which is fine.
return getLanguageInfoFromFlag(lang.flag, languageDictionary); // possibly returns null, which is fine.
}

// If it's not even a string, then it does not represent a language
Expand All @@ -1122,9 +1135,9 @@ export function toLanguageInfo(
}

return (
getLanguageInfoFromKey(lang, languageDictionnary) ||
getLanguageInfoFromCode(lang, languageDictionnary) ||
getLanguageInfoFromFlag(lang, languageDictionnary) ||
getLanguageInfoFromKey(lang, languageDictionary) ||
getLanguageInfoFromCode(lang, languageDictionary) ||
getLanguageInfoFromFlag(lang, languageDictionary) ||
null
);
}
Expand All @@ -1136,10 +1149,10 @@ export function toLanguageInfo(
export function areSameLanguages(
langA: string | LanguageInfo,
langB: string | LanguageInfo,
languageDictionnary: { [k: string]: LanguageInfo } = Language,
languageDictionary: { [k: string]: LanguageInfo } = Language,
): boolean {
const langAObj = toLanguageInfo(langA, languageDictionnary);
const langBObj = toLanguageInfo(langB, languageDictionnary);
const langAObj = toLanguageInfo(langA, languageDictionary);
const langBObj = toLanguageInfo(langB, languageDictionary);

return langAObj && langBObj && langAObj.flag === langBObj.flag;
}

0 comments on commit dca1f82

Please sign in to comment.