mirror of
https://github.com/sern-handler/tools
synced 2026-06-06 01:16:59 +00:00
localization should work
This commit is contained in:
@@ -4,7 +4,8 @@ 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'
|
||||
import { dfsApplyLocalization } from './internal'
|
||||
|
||||
|
||||
/**
|
||||
* @since 3.4.0
|
||||
@@ -12,7 +13,7 @@ import { applyLocalization, dfsApplyLocalization } from './internal'
|
||||
*/
|
||||
class ShrimpleLocalizer implements Init {
|
||||
private __localization!: LocalsProvider;
|
||||
currentLocale: string = "en-US";
|
||||
currentLocale: string = "en";
|
||||
|
||||
translationsFor(path: string): Record<string, any> {
|
||||
return this.__localization.localizationFor(path);
|
||||
@@ -27,7 +28,7 @@ class ShrimpleLocalizer implements Init {
|
||||
const map = await this.readLocalizationDirectory();
|
||||
this.__localization = new LocalsProvider({
|
||||
defaultLocale: this.currentLocale,
|
||||
fallbackLocale: "en-US",
|
||||
fallbackLocale: "en",
|
||||
locales: map
|
||||
});
|
||||
}
|
||||
@@ -35,7 +36,6 @@ 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'))
|
||||
@@ -59,11 +59,15 @@ export const local = (i: string, local: string) => {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* An init plugin to add localization fields to a command module.
|
||||
* Your localization configuration should look like,
|
||||
* @param root {string} If you have conflicting command names, you may configure the root of the name. (= command/{root})
|
||||
* Below is es.json (spanish)
|
||||
* {
|
||||
* ```json
|
||||
{
|
||||
"command/comer" : {
|
||||
"description": "Comer en Texas",
|
||||
"options": {
|
||||
@@ -74,13 +78,16 @@ export const local = (i: string, local: string) => {
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
*/
|
||||
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)
|
||||
deps['@sern/logger'].info({ message: "Localizing "+ module.name });
|
||||
const resolvedLocalization= 'command/'+(root??module.name);
|
||||
Reflect.set(module, 'name_localizations', deps.localizer.translationsFor(resolvedLocalization+".name"));
|
||||
Reflect.set(module, 'description_localizations', deps.localizer.translationsFor(resolvedLocalization+'.description'));
|
||||
const newOpts = module.options ?? [];
|
||||
//@ts-ignore
|
||||
dfsApplyLocalization(newOpts, deps, [resolvedLocalization]);
|
||||
@@ -89,8 +96,8 @@ export const localize = (root?: string) =>
|
||||
});
|
||||
return controller.next();
|
||||
} else {
|
||||
console.error("Cannot localize this type of module");
|
||||
return controller.stop();
|
||||
//@ts-ignore
|
||||
return controller.stop("Cannot localize this type of module " + module.name);
|
||||
}
|
||||
})
|
||||
|
||||
@@ -108,5 +115,5 @@ export const Localization = (defaultLocale?: string) => {
|
||||
if (defaultLocale) {
|
||||
localizer.currentLocale = defaultLocale;
|
||||
}
|
||||
return localizer;
|
||||
return localizer as {};
|
||||
}
|
||||
|
||||
@@ -5,11 +5,12 @@ export interface Option {
|
||||
options?: Array<Option>
|
||||
}
|
||||
|
||||
//Add props[i]_localizations fields to the current item
|
||||
export const applyLocalization =
|
||||
(item: Option, props: string[], basePath: string[], deps: any) => {
|
||||
for(const n of props) {
|
||||
const translated: string = deps.localizer.translationsFor(basePath.concat(n).join('.'))
|
||||
Reflect.set(item, n+'_localizations', translated)
|
||||
Reflect.set(item, n+'_localizations', translated)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,31 +20,31 @@ export function dfsApplyLocalization(
|
||||
deps: Record<string,unknown>,
|
||||
path: string[]) {
|
||||
|
||||
function dfs(item: Option, path: string[]) {
|
||||
const basePath = [...path, item.name]
|
||||
if (item.type === 1) {
|
||||
for (const subItem of item?.options??[]) {
|
||||
dfs(subItem, [...basePath, subItem.name]);
|
||||
}
|
||||
} else if (item.type === 2) {
|
||||
for (const subItem of item?.options??[]) {
|
||||
dfs(subItem, [...basePath, subItem.name]);
|
||||
}
|
||||
} else {
|
||||
//@ts-ignore
|
||||
if(Reflect.has(item, 'choices') && Array.isArray(item.choices)) {
|
||||
function dfs(item: Option, path: string[]) {
|
||||
const basePath = path
|
||||
if (item.type === 1) {
|
||||
for (const subItem of item?.options??[]) {
|
||||
dfs(subItem, [...basePath, subItem.name]);
|
||||
}
|
||||
} else if (item.type === 2) {
|
||||
for (const subItem of item?.options??[]) {
|
||||
dfs(subItem, [...basePath, subItem.name]);
|
||||
}
|
||||
} else {
|
||||
//@ts-ignore
|
||||
for(const choice of item.choices) {
|
||||
applyLocalization(choice, ['name'], [...basePath, 'choices'], deps)
|
||||
if(Reflect.has(item, 'choices') && Array.isArray(item.choices)) {
|
||||
//@ts-ignore
|
||||
for(const choice of item.choices) {
|
||||
applyLocalization(choice, ['name'], [...basePath, 'choices'], deps)
|
||||
}
|
||||
}
|
||||
}
|
||||
applyLocalization(item, ['name', 'description'], basePath, deps)
|
||||
}
|
||||
applyLocalization(item, ['name', 'description'], basePath, deps)
|
||||
}
|
||||
|
||||
for (const item of items) {
|
||||
dfs(item, [...path, 'options', item.name]);
|
||||
}
|
||||
for (const item of items) {
|
||||
dfs(item, [...path, 'options', item.name]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
"test": "vitest"
|
||||
},
|
||||
"dependencies": {
|
||||
"shrimple-locales": "^0.2.0"
|
||||
"shrimple-locales": "^0.2.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@sern/handler": "^3.3.0",
|
||||
|
||||
12
yarn.lock
12
yarn.lock
@@ -509,7 +509,7 @@ __metadata:
|
||||
dependencies:
|
||||
"@sern/handler": ^3.3.0
|
||||
discord.js: ^14.15.3
|
||||
shrimple-locales: ^0.2.0
|
||||
shrimple-locales: ^0.2.1
|
||||
vitest: ^1.2.2
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
@@ -2023,10 +2023,10 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"shrimple-locales@npm:^0.2.0":
|
||||
version: 0.2.0
|
||||
resolution: "shrimple-locales@npm:0.2.0"
|
||||
checksum: 9b5360e6ca5b2e75fdbaaeee0286015294b64773950924088f6088184f010837a89e84101eddfedcda5dd660a97df84fee6e1e95345e01f7e1edfff6b4ff2341
|
||||
"shrimple-locales@npm:^0.2.1":
|
||||
version: 0.2.1
|
||||
resolution: "shrimple-locales@npm:0.2.1"
|
||||
checksum: 18ed74cf56d7611e5cc83a38527f401eadaa20d4f50cc61c05a93edd22c61987d017a784216b30615f0dc4a0553ab9418fb64f7a4270d2f88b88f087da938cb5
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
@@ -2278,7 +2278,7 @@ __metadata:
|
||||
|
||||
"typescript@patch:typescript@^5.0.0#~builtin<compat/typescript>":
|
||||
version: 5.4.5
|
||||
resolution: "typescript@patch:typescript@npm%3A5.4.5#~builtin<compat/typescript>::version=5.4.5&hash=f3b441"
|
||||
resolution: "typescript@patch:typescript@npm%3A5.4.5#~builtin<compat/typescript>::version=5.4.5&hash=14eedb"
|
||||
bin:
|
||||
tsc: bin/tsc
|
||||
tsserver: bin/tsserver
|
||||
|
||||
Reference in New Issue
Block a user