Skip to content

Commit

Permalink
feat: better rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
strbit committed Aug 29, 2024
1 parent f8b0dc1 commit f83ba72
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 54 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ type MyContext = Context & I18nFlavor;
const i18n = new I18n<MyContext>({
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.
Expand Down
101 changes: 49 additions & 52 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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<NestedTranslation[]> {
const files = new Array<NestedTranslation>();
const filtered = new Array<NestedTranslation>();
const locales = new Array<string>();
const locales = new Set<string>();

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<NestedTranslation>();
const filtered = new Array<NestedTranslation>();
const locales = new Array<string>();
const locales = new Set<string>();

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;
};
});
}

0 comments on commit f83ba72

Please sign in to comment.