Skip to content

Commit 14e231a

Browse files
committed
got optional localizer working
1 parent 09a8656 commit 14e231a

File tree

7 files changed

+79
-64
lines changed

7 files changed

+79
-64
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@
4848
"@typescript-eslint/eslint-plugin": "5.58.0",
4949
"@typescript-eslint/parser": "5.59.1",
5050
"discord.js": "^14.11.0",
51-
"esbuild": "^0.17.0",
5251
"eslint": "8.39.0",
5352
"prettier": "2.8.8",
53+
"shrimple-locales": "^0.1.2",
5454
"tsup": "^6.7.0",
5555
"typescript": "5.0.2",
5656
"vitest": "latest"
@@ -96,7 +96,7 @@
9696
"url": "git+https://github.com/sern-handler/handler.git"
9797
},
9898
"homepage": "https://sern.dev",
99-
"optionalDependencies": {
99+
"peerDependencies": {
100100
"shrimple-locales": "^0.1.2"
101101
}
102102
}

src/core/ioc/base.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import * as assert from 'assert';
2-
import { composeRoot, useContainer } from './dependency-injection';
3-
import type { DependencyConfiguration } from '../../types/ioc';
2+
import { useContainer } from './dependency-injection';
3+
import type { CoreDependencies, DependencyConfiguration } from '../../types/ioc';
44
import { CoreContainer } from './container';
55
import { Result } from 'ts-results-es';
66
import { DefaultServices } from '../_internal';
77
import { AnyFunction } from '../../types/utility';
88
import type { Logging } from '../contracts/logging';
99
import { requir } from '../module-loading';
10+
import { fileURLToPath } from 'node:url';
11+
import path from 'path';
1012

1113
//SIDE EFFECT: GLOBAL DI
1214
let containerSubject: CoreContainer<Partial<Dependencies>>;
@@ -93,13 +95,41 @@ export const insertLogger = (containerSubject: CoreContainer<any>) => {
9395
.upsert({'@sern/logger': () => new DefaultServices.DefaultLogging});
9496
}
9597

96-
export const insertLocalizer = async (containerSubject: CoreContainer<any>) => {
97-
const { ShrimpleLocalizer } = requir('../structures/services/localizer');
98+
const insertLocalizer = async (containerSubject: CoreContainer<any>) => {
99+
const packageDirectory = fileURLToPath(import.meta.url);
100+
const pathToLocalizer= path.resolve(packageDirectory, "../", "optional", "localizer");
101+
const { ShrimpleLocalizer } = requir(pathToLocalizer);
98102
containerSubject
99-
.add({'@sern/localizer': new ShrimpleLocalizer() });
103+
.upsert({'@sern/localizer': new ShrimpleLocalizer() });
100104
}
101105

106+
/**
107+
* Given the user's conf, check for any excluded/included dependency keys.
108+
* Then, call conf.build to get the rest of the users' dependencies.
109+
* Finally, update the containerSubject with the new container state
110+
* @param conf
111+
*/
112+
function composeRoot(
113+
container: CoreContainer<Partial<Dependencies>>,
114+
conf: DependencyConfiguration,
115+
) {
116+
//container should have no client or logger yet.
117+
const hasLogger = conf.exclude?.has('@sern/logger');
118+
if (!hasLogger) {
119+
insertLogger(container);
120+
}
121+
if(conf.include?.includes('@sern/localizer')) {
122+
insertLocalizer(container);
123+
}
124+
//Build the container based on the callback provided by the user
125+
conf.build(container as CoreContainer<Omit<CoreDependencies, '@sern/client'>>);
126+
127+
if (!hasLogger) {
128+
container.get('@sern/logger')?.info({ message: 'All dependencies loaded successfully.' });
129+
}
102130

131+
container.ready();
132+
}
103133

104134
export async function makeDependencies<const T extends Dependencies>
105135
(conf: ValidDependencyConfig) {
@@ -118,7 +148,7 @@ export async function makeDependencies<const T extends Dependencies>
118148
}
119149

120150
if(included.includes('@sern/localizer')) {
121-
await insertLocalizer(containerSubject);
151+
insertLocalizer(containerSubject);
122152
}
123153

124154
containerSubject.ready();

src/core/ioc/dependency-injection.ts

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import type { CoreDependencies, DependencyConfiguration, IntoDependencies } from '../../types/ioc';
2-
import { requir } from '../module-loading';
3-
import { insertLogger, useContainerRaw } from './base';
4-
import { CoreContainer } from './container';
1+
import type { IntoDependencies } from '../../types/ioc';
2+
import { useContainerRaw } from './base';
53

64
/**
75
* @__PURE__
@@ -47,33 +45,7 @@ export function Services<const T extends (keyof Dependencies)[]>(...keys: [...T]
4745
return keys.map(k => container.get(k)!) as IntoDependencies<T>;
4846
}
4947

50-
/**
51-
* Given the user's conf, check for any excluded/included dependency keys.
52-
* Then, call conf.build to get the rest of the users' dependencies.
53-
* Finally, update the containerSubject with the new container state
54-
* @param conf
55-
*/
56-
export function composeRoot(
57-
container: CoreContainer<Partial<Dependencies>>,
58-
conf: DependencyConfiguration,
59-
) {
60-
//container should have no client or logger yet.
61-
const hasLogger = conf.exclude?.has('@sern/logger');
62-
if (!hasLogger) {
63-
insertLogger(container);
64-
}
65-
if(conf.include?.includes('@sern/localizer')) {
66-
container.add({ '@sern/localizer': requir('shrimple-locales') });
67-
}
68-
//Build the container based on the callback provided by the user
69-
conf.build(container as CoreContainer<Omit<CoreDependencies, '@sern/client'>>);
70-
71-
if (!hasLogger) {
72-
container.get('@sern/logger')?.info({ message: 'All dependencies loaded successfully.' });
73-
}
7448

75-
container.ready();
76-
}
7749

7850
export function useContainer<const T extends Dependencies>() {
7951
return <V extends (keyof T)[]>(...keys: [...V]) =>

src/core/structures/optional/localizer.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/optional/localizer.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { Localizer, Init } from '../core/contracts'
2+
import { Localization } from 'shrimple-locales'
3+
import fs from 'node:fs/promises'
4+
import { join, resolve } from 'node:path';
5+
import { filename } from '../core/module-loading'
6+
/**
7+
* @internal
8+
* @since 2.0.0
9+
* Version 4.0.0 will internalize this api. Please refrain from using ModuleStore!
10+
*/
11+
export class ShrimpleLocalizer implements Localizer, Init {
12+
private __localization!: Localization;
13+
constructor(){}
14+
translate(text: string): string {
15+
return this.__localization.get(text);
16+
}
17+
18+
async init() {
19+
this.__localization = new Localization({
20+
defaultLocale: "en",
21+
fallbackLocale: "en",
22+
locales: await this.readLocalizationDirectory()
23+
});
24+
}
25+
26+
private async readLocalizationDirectory() {
27+
const translationFiles = [];
28+
const localPath = resolve('locals');
29+
for(const json of await fs.readdir(localPath)) {
30+
translationFiles.push({ [filename(json)]:
31+
JSON.parse(await fs.readFile(join(localPath, json), 'utf8')) })
32+
}
33+
return translationFiles.reduce((acc, cur ) => ({ ...cur, ...acc }), {});
34+
}
35+
}

tsup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineConfig } from 'tsup';
22
const shared = {
3-
entry: ['src/index.ts', 'src/core/structures/optional/localizer.ts'],
3+
entry: ['src/index.ts', 'src/optional/localizer.ts'],
44
external: ['discord.js', 'iti', 'shrimple-locales'],
55
platform: 'node',
66
clean: true,

yarn.lock

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,6 @@ __metadata:
629629
"@typescript-eslint/parser": 5.59.1
630630
callsites: ^3.1.0
631631
discord.js: ^14.11.0
632-
esbuild: ^0.17.0
633632
eslint: 8.39.0
634633
iti: ^0.6.0
635634
prettier: 2.8.8
@@ -639,9 +638,8 @@ __metadata:
639638
tsup: ^6.7.0
640639
typescript: 5.0.2
641640
vitest: latest
642-
dependenciesMeta:
643-
shrimple-locales:
644-
optional: true
641+
peerDependencies:
642+
shrimple-locales: ^0.1.2
645643
languageName: unknown
646644
linkType: soft
647645

@@ -1453,7 +1451,7 @@ __metadata:
14531451
languageName: node
14541452
linkType: hard
14551453

1456-
"esbuild@npm:^0.17.0, esbuild@npm:^0.17.6":
1454+
"esbuild@npm:^0.17.6":
14571455
version: 0.17.19
14581456
resolution: "esbuild@npm:0.17.19"
14591457
dependencies:

0 commit comments

Comments
 (0)