diff --git a/.gitignore b/.gitignore index e9a53bf..2d53da6 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ node_modules/**/* packages/ioc/node_modules/* packages/poster/dts/discord.d.ts +packages/**/node_modules diff --git a/packages/localizer/index.ts b/packages/localizer/index.ts index d14e303..05816e1 100644 --- a/packages/localizer/index.ts +++ b/packages/localizer/index.ts @@ -1,9 +1,10 @@ -import { type Init, Service } from '@sern/handler' +import { type Init, Service, CommandInitPlugin, CommandType, controller } from '@sern/handler' import { Localization as LocalsProvider } from 'shrimple-locales' import fs from 'node:fs/promises' import { existsSync } from 'node:fs' import { join, resolve } from 'node:path'; import assert from 'node:assert'; +import { applyLocalization, dfsApplyLocalization } from './internal' /** * @since 3.4.0 @@ -11,7 +12,6 @@ import assert from 'node:assert'; */ class ShrimpleLocalizer implements Init { private __localization!: LocalsProvider; - constructor(){} currentLocale: string = "en-US"; translationsFor(path: string): Record { @@ -34,8 +34,8 @@ class ShrimpleLocalizer implements Init { private async readLocalizationDirectory() { const translationFiles = []; - const localPath = resolve('resources', 'locals'); - assert(existsSync(localPath), "No directory \"resources/locals\" found for the localizer") + const localPath = resolve('assets', 'locals'); + 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('.')); @@ -69,6 +69,41 @@ export const localsFor = (path: string) => { return Service('localizer').translationsFor(path) } + +/** + * An init plugin to add localization fields to a command module. + * Your localization configuration should look like, + * Below is es.json (spanish) + * { + "command/comer" : { + "description": "Comer en Texas", + "options": { + "chicken": { + "name": "pollo", + "description": "un pollo largo" + } + } + } + } + */ +export const localize = (root?: string) => + //@ts-ignore + CommandInitPlugin(({ updateModule, module, deps }) => { + if(module.type === CommandType.Slash || module.type === CommandType.Both) { + const resolvedLocalization= 'command/'+root??module.name; + applyLocalization(module, [resolvedLocalization, resolvedLocalization+'.description'], [], deps) + const newOpts = module.options ?? []; + //@ts-ignore + dfsApplyLocalization(newOpts, deps, [resolvedLocalization]); + updateModule({ + options: newOpts + }); + } else { + console.error("Cannot localize this type of module"); + return controller.next(); + } +}) + /** * A service which provides simple file based localization. Add this while making dependencies. * @example diff --git a/packages/localizer/internal.ts b/packages/localizer/internal.ts new file mode 100644 index 0000000..fc5b2f1 --- /dev/null +++ b/packages/localizer/internal.ts @@ -0,0 +1,49 @@ + +export interface Option { + name:string, + type: number, + options?: Array