From f83ba72d8b8f5fa82a1940330513b729b68aa7e3 Mon Sep 17 00:00:00 2001 From: strbit Date: Thu, 29 Aug 2024 10:42:46 +0500 Subject: [PATCH] feat: better rewrite --- README.md | 2 - src/utils.ts | 101 +++++++++++++++++++++++++-------------------------- 2 files changed, 49 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 3d9b671d..d6f662b4 100644 --- a/README.md +++ b/README.md @@ -49,8 +49,6 @@ type MyContext = Context & I18nFlavor; const i18n = new I18n({ defaultLocale: "en", directory: "locales", - // In order for the above example to work, this must be uncommented. - // useNestedTranslations: true, }); // Create a bot as usual, but use the modified Context type. diff --git a/src/utils.ts b/src/utils.ts index f2819f59..039cf533 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,84 +1,81 @@ import { extname, join, SEP, walk, walkSync } from "./deps.ts"; import { NestedTranslation } from "./types.ts"; +function throwReadFileError(path: string) { + throw new Error( + `Something went wrong while reading the "${path}" file, usually, this can be caused by the file being empty. \ + If it is, please add at least one translation key to this file (or simply just delete it) to solve this error.`, + ); +} + export async function readLocalesDir( path: string, ): Promise { const files = new Array(); - const filtered = new Array(); - const locales = new Array(); + const locales = new Set(); for await (const entry of walk(path)) { - const extension = extname(entry.name); - if (entry.isFile && extension === ".ftl") { - const decoder = new TextDecoder("utf-8"); - const filePath = join(path, entry.path.replace(path, "")); - const contents = await Deno.readFile(filePath); + if (entry.isFile && extname(entry.name) === ".ftl") { + try { + const decoder = new TextDecoder("utf-8"); + const filePath = join(path, entry.path.replace(path, "")); + const contents = Deno.readFileSync(filePath); - if (!contents) { - throw new Error( - `Translation file ${entry.name} resulted in an empty string, which means the file is most likely empty. \ -Please add at least one translation key to this file (or simply just delete it) to solve this error.`, - ); - } + const belongsTo = entry.path.split(SEP)[1].split(".")[0]; + const translationSource = decoder.decode(contents); - files.push({ - belongsTo: entry.path.split(SEP)[1].split(".")[0], - translationSource: decoder.decode(contents), - }); + files.push({ + belongsTo, + translationSource, + }); + locales.add(belongsTo); + } catch { + throwReadFileError(entry.path); + } } } - for (const file of files) { - if (locales.indexOf(file.belongsTo) === -1) locales.push(file.belongsTo); - } - for (const locale of locales) { + + return Array.from(locales).map((locale) => { const sameLocale = files.filter((file) => file.belongsTo === locale); const sourceOnly = sameLocale.map((file) => file.translationSource); - - filtered.push({ + return { belongsTo: locale, translationSource: sourceOnly.join("\n"), - }); - } - return filtered; + }; + }); } export function readLocalesDirSync(path: string): NestedTranslation[] { const files = new Array(); - const filtered = new Array(); - const locales = new Array(); + const locales = new Set(); for (const entry of walkSync(path)) { - const extension = extname(entry.name); - if (entry.isFile && extension === ".ftl") { - const decoder = new TextDecoder("utf-8"); - const filePath = join(path, entry.path.replace(path, "")); - const contents = Deno.readFileSync(filePath); + if (entry.isFile && extname(entry.name) === ".ftl") { + try { + const decoder = new TextDecoder("utf-8"); + const filePath = join(path, entry.path.replace(path, "")); + const contents = Deno.readFileSync(filePath); - if (!contents) { - throw new Error( - `Translation file ${entry.name} resulted in an empty string, which means the file is most likely empty. \ -Please add at least one translation key to this file (or simply just delete it) to solve this error.`, - ); - } + const belongsTo = entry.path.split(SEP)[1].split(".")[0]; + const translationSource = decoder.decode(contents); - files.push({ - belongsTo: entry.path.split(SEP)[1].split(".")[0], - translationSource: decoder.decode(contents), - }); + files.push({ + belongsTo, + translationSource, + }); + locales.add(belongsTo); + } catch { + throwReadFileError(entry.path); + } } } - for (const file of files) { - if (locales.indexOf(file.belongsTo) === -1) locales.push(file.belongsTo); - } - for (const locale of locales) { + + return Array.from(locales).map((locale) => { const sameLocale = files.filter((file) => file.belongsTo === locale); const sourceOnly = sameLocale.map((file) => file.translationSource); - - filtered.push({ + return { belongsTo: locale, translationSource: sourceOnly.join("\n"), - }); - } - return filtered; + }; + }); }