mirror of
https://github.com/sern-handler/tools
synced 2026-06-06 01:16:59 +00:00
get more localizer work done
This commit is contained in:
@@ -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<string, any> {
|
||||
@@ -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
|
||||
|
||||
49
packages/localizer/internal.ts
Normal file
49
packages/localizer/internal.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
|
||||
export interface Option {
|
||||
name:string,
|
||||
type: number,
|
||||
options?: Array<Option>
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export function dfsApplyLocalization(
|
||||
items: Array<Option>,
|
||||
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)) {
|
||||
//@ts-ignore
|
||||
for(const choice of item.choices) {
|
||||
applyLocalization(choice, ['name'], [...basePath, 'choices'], deps)
|
||||
}
|
||||
}
|
||||
}
|
||||
applyLocalization(item, ['name', 'description'], basePath, deps)
|
||||
}
|
||||
|
||||
for (const item of items) {
|
||||
dfs(item, [...path, 'options', item.name]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,13 +4,16 @@
|
||||
"description": "Localizer",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"build": "tsc"
|
||||
"build": "tsc",
|
||||
"test": "vitest"
|
||||
},
|
||||
"dependencies": {
|
||||
"shrimple-locales": "^0.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@sern/handler": "^3.3.0"
|
||||
"@sern/handler": "^3.3.0",
|
||||
"discord.js": "^14.15.3",
|
||||
"vitest": "^1.2.2"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
|
||||
98
packages/localizer/test/index.test.ts
Normal file
98
packages/localizer/test/index.test.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import { test, expect, describe, it, vi, afterEach } from 'vitest'
|
||||
import { Localization } from '../index'
|
||||
import { applyLocalization, dfsApplyLocalization, Option } from '../internal';
|
||||
|
||||
test('Localization Instance', () => {
|
||||
expect(Localization()).toBeTruthy()
|
||||
})
|
||||
|
||||
describe('applyLocalization', () => {
|
||||
it('should apply localizations to the given item', () => {
|
||||
const item: Record<string,any> = {};
|
||||
const props = ['name', 'description'];
|
||||
const basePath = ['app', 'pages'];
|
||||
const localizer = {
|
||||
translationsFor: vi.fn((key: string) => `translated.${key}`),
|
||||
};
|
||||
const deps = { localizer };
|
||||
|
||||
//@ts-ignore
|
||||
applyLocalization(item, props, basePath, deps);
|
||||
expect(item.name_localizations).toBe('translated.app.pages.name');
|
||||
expect(item.description_localizations).toBe('translated.app.pages.description');
|
||||
});
|
||||
|
||||
it('should not modify the item if no props are provided', () => {
|
||||
//@ts-ignore
|
||||
const item: Option = {};
|
||||
const props: string[] = [];
|
||||
const basePath = ['app', 'pages'];
|
||||
const localizer = {
|
||||
translationsFor: vi.fn(),
|
||||
};
|
||||
const deps = { localizer };
|
||||
|
||||
applyLocalization(item, props, basePath, deps);
|
||||
|
||||
expect(item).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('dfsApplyLocalization', () => {
|
||||
afterEach(() => {
|
||||
vi.resetAllMocks();
|
||||
});
|
||||
|
||||
it('should apply localizations to top-level items', () => {
|
||||
const items = [{ type: 3, name: 'item1' }, { type: 3, name: 'item2' }];
|
||||
const deps = { localizer: { translationsFor: vi.fn() } };
|
||||
const path = ['root'];
|
||||
|
||||
dfsApplyLocalization(items, deps, path);
|
||||
|
||||
});
|
||||
|
||||
it('should apply localizations to nested items', () => {
|
||||
const items: Option[] = [
|
||||
{
|
||||
name: 'item1',
|
||||
type: 1,
|
||||
options: [{ type: 3, name: 'subItem1' }],
|
||||
},
|
||||
];
|
||||
const deps = { localizer: { translationsFor: vi.fn() } };
|
||||
const path = ['root'];
|
||||
|
||||
dfsApplyLocalization(items, deps, path);
|
||||
|
||||
});
|
||||
|
||||
it('should apply localizations to choices', () => {
|
||||
const items: Option[] = [
|
||||
{
|
||||
name: 'item1',
|
||||
//@ts-ignore
|
||||
choices: [{ name: 'choice1' }, { name: 'choice2' }],
|
||||
},
|
||||
];
|
||||
const deps = { localizer: { translationsFor: vi.fn(() => "a") } };
|
||||
const path = ['root'];
|
||||
|
||||
dfsApplyLocalization(items, deps, path);
|
||||
console.log(items[0].choices)
|
||||
});
|
||||
it('should call applyLocalization n times, n = num of options', () => {
|
||||
|
||||
const items: Option[] = [
|
||||
{
|
||||
name: 'item1',
|
||||
type: 3,
|
||||
},
|
||||
{
|
||||
name: "item2",
|
||||
type: 4
|
||||
}
|
||||
];
|
||||
})
|
||||
});
|
||||
@@ -1,4 +1,8 @@
|
||||
{
|
||||
"files": [
|
||||
"./index.ts",
|
||||
"./internal.ts"
|
||||
],
|
||||
"compilerOptions": {
|
||||
/* Visit https://aka.ms/tsconfig to read more about this file */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user