Skip to content

Commit

Permalink
refactor: simplify useTranslations
Browse files Browse the repository at this point in the history
  • Loading branch information
imnaiyar committed Nov 11, 2024
1 parent 1416d59 commit 606ac63
Showing 1 changed file with 10 additions and 74 deletions.
84 changes: 10 additions & 74 deletions src/bot/handlers/useTranslation.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,18 @@
import type { LangKeys } from "#bot/i18n";
// import i18next initialization file to insure it's initialized before calling the below function
import * as i from "../i18n.js";
import { t } from "i18next";
import { supportedLang } from "../libs/constants/supportedLang.js";
import fs from "node:fs";
import path from "node:path";
import { pathToFileURL } from "node:url";
import type { LocalizationMap } from "discord.js";

const allowed_langs = [
"id",
"da",
"de",
"en-US",
"es-419",
"fr",
"hr",
"it",
"lt",
"hu",
"nl",
"no",
"pl",
"pt-BR",
"ro",
"fi",
"sv-SE",
"vi",
"ts",
"cs",
"el",
"bg",
"ru",
"uk",
"hi",
"th",
"zh-CN",
"ja",
"zh-TW",
"ko",
];

const files = fs.readdirSync("locales");
// Only include allowed and supported langs
const languages = files.filter((f) => allowed_langs.includes(f)).filter((f) => supportedLang.some((l) => l.value === f));
const datas: Record<string, Record<string, any>> = Object.fromEntries(languages.map((l) => [l, {}]));
for (const lg of languages) {
const namespaces = fs.readdirSync(path.resolve("locales", lg));
for (const ns of namespaces) {
const { default: translations } = await import(pathToFileURL(path.resolve("locales", lg, ns)).href, {
with: { type: "json" },
});
datas[lg][ns.replaceAll(".json", "")] = translations;
}
}
/**
* Get API compatible localization data for all the available (and allowed) languages
* @param key translation keys
* @returns
* @returns localization data
*/
export function useTranslations(key: LangKeys): Partial<Record<string, string>> {
const [ns, _k] = key.split(":");
const keys = _k.split(".");
const last_key = keys.at(-1) === "name";
const t: Partial<Record<string, string>> = {};
const langs = Object.keys(datas);
for (const l of langs) {
const filename = l;
let data = datas[l][ns];
for (const k of keys) {
if (data[k] !== undefined) {
data = data[k];
} else {
data = undefined;
break;
}
}

if (data !== undefined) {
if (typeof data !== "string") throw new TypeError(`Expected a string, recieved ${typeof data}.\n\nRecieved: ${data}`);
t[filename] = last_key ? data.trim().replaceAll(" ", "-").toLocaleLowerCase(filename) : data;
}
export function useTranslations(key: i.LangKeys): LocalizationMap {
const data: LocalizationMap = {};
for (const { value } of supportedLang) {
data[value] = t(key, { lng: value });
}
return t;
return data;
}

0 comments on commit 606ac63

Please sign in to comment.