-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
49 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
}); | ||
} |