Skip to content

Commit

Permalink
deep merge translations
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp committed Oct 25, 2023
1 parent c0fd650 commit 4fa252e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,12 @@ export class ExtendedClient extends Client {
});

const guilds = { global: [] };
commandsArr.forEach((el) => {
if (el.servers === null || el.servers === undefined) guilds.global.push(el.command);
commandsArr.forEach((cmd) => {
if (!cmd.servers) guilds.global.push(cmd.command);
else
el.servers.forEach((server) => {
cmd.servers.forEach((server) => {
if (guilds[server] === undefined) guilds[server] = [];
guilds[server].push(el.command);
guilds[server].push(cmd.command);
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/commands/faithful/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const command: SlashCommand = {
// fetch the whole collection since we're using it multiple times
const settings = (await axios.get(`${interaction.client.tokens.apiUrl}settings/raw`)).data;

if (choice == "all" || choice == "server") {
if (["all", "server"].includes(choice)) {
if (!interaction.hasPermission("manager")) return;

interaction
Expand Down
25 changes: 10 additions & 15 deletions src/helpers/strings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import commands from "@/lang/en-US/commands.json";
import errors from "@/lang/en-US/errors.json";
import { mergeDeep } from "@utility/objects";

export const JSONFiles = ["commands", "errors"];
export const baseTranslations = { ...commands, ...errors };
Expand All @@ -12,31 +13,25 @@ export type AllStrings = typeof baseTranslations;
*/
export function strings(forceEnglish = false): AllStrings {
const countryCode = this.locale;
let lang: AllStrings;
let baseLang: AllStrings;
// load all english strings into one lang object
for (const json of JSONFiles)
lang = {
...lang,
baseLang = {
...baseLang,
...require(`@/lang/en-US/${json}.json`),
};

if (countryCode == "en-GB" || countryCode == "en-US" || forceEnglish) return lang;
if (forceEnglish || ["en-GB", "en-US"].includes(countryCode)) return baseLang;

// not in english
for (const json of JSONFiles)
try {
// try importing before adding to prevent errors if language isn't done
const lang2 = require(`@/lang/${countryCode}/${json}.json`);
lang = { ...lang, ...lang2 };
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 lang;
return baseLang;
}

/**
* @important
* If Discord adds a language, what should I do?
* - [Add it to workflow file on crowdin](https://faithful.crowdin.com/u/projects/4/workflow)
* - [Update the language list below using](https://discord.com/developers/docs/reference#locales)
* - [Update mapping on crowdin](https://faithful.crowdin.com/u/projects/4/apps/system/github) (select the branch > edit branch configuration > edit file filter > language mapping)
*/
30 changes: 30 additions & 0 deletions src/helpers/utility/objects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Simple object check.
* @param item item to check.
* @returns true if item is an object
*/
export const isObject = (item: unknown) =>
item !== null && item !== undefined && typeof item === "object" && !Array.isArray(item);

/**
* Merge two objects into the targeted object/array
* @author Juknum
* @param target where to add sources to
* @param sources what to add
* @returns merged sources
*/
export function mergeDeep(target: any, ...sources: any[]) {
if (!sources.length) return target;
const source = sources.shift();

if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
mergeDeep(target[key], source[key]);
} else Object.assign(target, { [key]: source[key] });
}
}

return mergeDeep(target, ...sources);
}

0 comments on commit 4fa252e

Please sign in to comment.