Skip to content

Commit

Permalink
simpler reduction algorithm for loading strings
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp committed Oct 25, 2023
1 parent 460b4fd commit 4620212
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/client/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ declare module "discord.js" {
/**
* Load strings for a given interaction
* @author Evorp
* @param forceEnglish whether to only use english or determine locale
* @returns all strings in the correct locale
* @param forceEnglish whether to only use english or determine language
* @returns all strings in the correct language
*/
strings(forceEnglish?: boolean): AllStrings;
/**
Expand Down
32 changes: 12 additions & 20 deletions src/helpers/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,21 @@ export type AllStrings = typeof baseTranslations;
/**
* Load strings based on interaction language
* @author Evorp
* @returns string output in correct language
* @param forceEnglish whether to only use english or determine language
* @returns all strings in the correct language
*/
export function strings(forceEnglish = false): AllStrings {
const countryCode = this.locale;
let baseLang: AllStrings;
// load all english strings into one lang object
for (const json of JSONFiles)
baseLang = {
...baseLang,
...require(`@/lang/en-US/${json}.json`),
};

if (forceEnglish || ["en-GB", "en-US"].includes(countryCode)) return baseLang;
if (forceEnglish || ["en-GB", "en-US"].includes(this.locale)) return baseTranslations;

// not in english
for (const json of JSONFiles)
return JSONFiles.reduce((acc, json) => {
let out: AllStrings;
try {
// try importing before adding to prevent errors if language isn't done
const translatedLang = require(`@/lang/${countryCode}/${json}.json`);

// merge all properties of object (if translation not done/updated yet on crowdin it falls back to english)
baseLang = mergeDeep({}, baseLang, translatedLang);
} catch {} // file not found

return baseLang;
// prevent errors if language isn't done
out = mergeDeep({}, acc, require(`@/lang/${this.locale}/${json}.json`));
} catch {
out = acc;
}
return out;
}, baseTranslations);
}

0 comments on commit 4620212

Please sign in to comment.