This commit is contained in:
Jacob Nguyen
2024-06-04 00:49:14 -05:00
parent 71505e104f
commit 4a2ce2e674
2 changed files with 4 additions and 14 deletions

View File

@@ -35,13 +35,14 @@ class ShrimpleLocalizer implements Init {
private async readLocalizationDirectory() {
const translationFiles = [];
const localPath = resolve('assets', 'locals');
console.log(localPath)
assert(existsSync(localPath), "No directory \"assets/locals\" found for the localizer")
for(const json_path of await fs.readdir(localPath)) {
const parsed = JSON.parse(await fs.readFile(join(localPath, json_path), 'utf8'))
const name = json_path.substring(0, json_path.lastIndexOf('.'));
translationFiles.push({ [name]: parsed })
}
return translationFiles.reduce((acc, cur ) => ({ ...cur, ...acc }), {});
return translationFiles.reduce((acc, cur) => ({ ...cur, ...acc }), {});
}
}
@@ -57,18 +58,6 @@ export const local = (i: string, local: string) => {
return Service('localizer').translate(i, local)
}
/**
* Returns a record of locales to their respective translations.
* Note: this method only works AFTER your container has been initiated
* @example
* ```ts
* assert.deepEqual(localsFor("salute.hello"), { "en-US": "hello", "es": "hola" })
* ```
*/
export const localsFor = (path: string) => {
return Service('localizer').translationsFor(path)
}
/**
* An init plugin to add localization fields to a command module.
@@ -109,7 +98,7 @@ export const localize = (root?: string) =>
* @example
* ```ts
* await makeDependencies(({ add }) => {
* add('@sern/localizer', Localization());
* add('localizer', Localization());
* });
* ```
**/

View File

@@ -5,6 +5,7 @@
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"watch": "tsc --watch",
"test": "vitest"
},
"dependencies": {