Skip to content

Commit

Permalink
feat: fallback language
Browse files Browse the repository at this point in the history
  • Loading branch information
Sv443 committed Jan 5, 2025
1 parent a65b341 commit 98da43c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/assets/translations/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"enabled": "Enabled",
"disabled": "Disabled",
"listSeparator": ", ",
"listSeparatorLast": " and ",
"shortListSeparator": " & "
"listSeparatorLast": " and "
},
"commands": {
"config": {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/VideoInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ export class VideoInfoCmd extends SlashCommand {

const catList = joinArrayReadable(
categories.map(cat => `${sponsorBlockCategoryColorEmojiMap[cat]} ${t(`commands.video_info.embedFields.sponsorBlockCategories.${cat as "sponsor"}`)}`),
t("general.shortListSeparator"),
t("general.shortListSeparator"),
t("general.listSeparator"),
t("general.listSeparator"),
);

const atVidEnd = valsWithin(segment[1], videoDuration);
Expand Down
26 changes: 21 additions & 5 deletions src/lib/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const valTransforms: Array<{

/** Currently set language */
let curLang = "";
/** Fallback language - if undefined, the trKey itself will be returned if the translation is not found */
let fallbackLang: string | undefined;

/** Common function to resolve the translation text in a specific language. */
function translate<TTrKey extends string = string>(language: string, key: TTrKey, ...trArgs: (Stringifiable | Record<string, Stringifiable>)[]): string {
Expand All @@ -79,7 +81,7 @@ function translate<TTrKey extends string = string>(language: string, key: TTrKey
const trObj = trans[language];

if(typeof language !== "string" || language.length === 0 || typeof trObj !== "object" || trObj === null)
return key;
return fallbackLang ? translate(fallbackLang, key, ...trArgs) : key;

const transformTrVal = (trKey: TTrKey, trValue: string): string => {
const tfs = valTransforms.filter(({ regex }) => new RegExp(regex).test(trValue));
Expand All @@ -94,8 +96,11 @@ function translate<TTrKey extends string = string>(language: string, key: TTrKey

const matches: RegExpExecArray[] = [];
let execRes: RegExpExecArray | null;
while((execRes = re.exec(trValue)) !== null)
while((execRes = re.exec(trValue)) !== null) {
if(matches.some(m => m[0] === execRes?.[0]))
break;
matches.push(execRes);
}

retStr = String(tf.fn({
language,
Expand Down Expand Up @@ -126,8 +131,8 @@ function translate<TTrKey extends string = string>(language: string, key: TTrKey
if(typeof value === "string")
return transformTrVal(key, value);

// default to translation key
return key;
// default to en-US or translation key
return fallbackLang ? translate(fallbackLang, key, ...trArgs) : key;
}

/**
Expand Down Expand Up @@ -186,6 +191,11 @@ const setLanguage = (language: string): void => {
curLang = language;
};

/** The fallback language to use when a translation key is not found in the currently active language - undefined to disable fallbacks and just return the translation key */
const setFallbackLanguage = (fallbackLanguage?: string): void => {
fallbackLang = fallbackLanguage;
};

/**
* Returns the active language set by {@linkcode tr.setLanguage()}
* If no language is set, this function will return `undefined`.
Expand Down Expand Up @@ -308,6 +318,7 @@ const tr = {
useTr,
addTranslations,
setLanguage,
setFallbackLanguage,
getLanguage,
getTranslations,
deleteTranslations,
Expand All @@ -329,6 +340,7 @@ export const defaultLocale = "en-US";

/** Array of tuples containing the regular expression and the transformation function */
const transforms = [
// replace placeholders in the format `${name}` with the value of the key `name` in the passed object or use positional mapping with the passed spread arguments
[
/\$\{([a-zA-Z0-9$_-]+)\}/gm,
({ matches, trArgs, trValue }) => {
Expand Down Expand Up @@ -391,6 +403,7 @@ export async function initTranslations(): Promise<void> {
tr.addTransform(regex, fn);

tr.setLanguage(enName ?? defaultLocale);
tr.setFallbackLanguage(enName ?? defaultLocale);
}

//#region getLocMap
Expand All @@ -411,8 +424,11 @@ export function getLocMap(trKey: TrKeyEn, prefix = ""): LocalizationMap {

const matches: RegExpExecArray[] = [];
let execRes: RegExpExecArray | null;
while((execRes = re.exec(trValue)) !== null)
while((execRes = re.exec(trValue)) !== null) {
if(matches.some(m => m[0] === execRes?.[0]))
break;
matches.push(execRes);
}

retStr = String(tf.fn({
language,
Expand Down

0 comments on commit 98da43c

Please # to comment.