Skip to content

Commit e83a011

Browse files
committed
proposing api notation?
1 parent 14e231a commit e83a011

File tree

9 files changed

+22
-28
lines changed

9 files changed

+22
-28
lines changed

TEST PLEASE DELETE.js

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

src/core/contracts/localizer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
export interface Localizer {
3-
translate(text: string) : string;
3+
translate(text: string, local: string) : string;
44
}

src/core/ioc/dependency-injection.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from 'node:assert';
12
import type { IntoDependencies } from '../../types/ioc';
23
import { useContainerRaw } from './base';
34

@@ -33,7 +34,9 @@ export function transient<T>(cb: () => () => T) {
3334
*
3435
*/
3536
export function Service<const T extends keyof Dependencies>(key: T) {
36-
return useContainerRaw().get(key)!;
37+
const dep = useContainerRaw().get(key)!;
38+
assert(dep, "Requested key " + key + " returned undefined");
39+
return dep;
3740
}
3841
/**
3942
* @since 3.0.0
@@ -51,3 +54,9 @@ export function useContainer<const T extends Dependencies>() {
5154
return <V extends (keyof T)[]>(...keys: [...V]) =>
5255
keys.map(key => useContainerRaw().get(key as keyof Dependencies)) as IntoDependencies<V>;
5356
}
57+
58+
59+
export function $local(i: string, local: string) {
60+
return Service('@sern/localizer').translate(i, local)
61+
}
62+

src/core/ioc/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export { makeDependencies } from './base';
2-
export { Service, Services, single, transient } from './dependency-injection';
2+
export { Service, Services, single, transient, $local } from './dependency-injection';

src/core/module-loading.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const shouldHandle = (path: string, fpath: string) => {
1818

1919

2020
export type ModuleResult<T> = Promise<ImportPayload<T>>;
21+
2122
/**
2223
* Import any module based on the absolute path.
2324
* This can accept four types of exported modules

src/optional/localizer.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import type { Localizer, Init } from '../core/contracts'
22
import { Localization } from 'shrimple-locales'
33
import fs from 'node:fs/promises'
4+
import { existsSync } from 'node:fs'
45
import { join, resolve } from 'node:path';
56
import { filename } from '../core/module-loading'
7+
import assert from 'node:assert';
8+
69
/**
710
* @internal
811
* @since 2.0.0
@@ -11,7 +14,8 @@ import { filename } from '../core/module-loading'
1114
export class ShrimpleLocalizer implements Localizer, Init {
1215
private __localization!: Localization;
1316
constructor(){}
14-
translate(text: string): string {
17+
translate(text: string, local: string): string {
18+
this.__localization.changeLanguage(local);
1519
return this.__localization.get(text);
1620
}
1721

@@ -26,6 +30,7 @@ export class ShrimpleLocalizer implements Localizer, Init {
2630
private async readLocalizationDirectory() {
2731
const translationFiles = [];
2832
const localPath = resolve('locals');
33+
assert(existsSync(localPath), "You need to create a directory called \"locals\" for the localizer")
2934
for(const json of await fs.readdir(localPath)) {
3035
translationFiles.push({ [filename(json)]:
3136
JSON.parse(await fs.readFile(join(localPath, json), 'utf8')) })

src/sern.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { handleCrash } from './handlers/_internal';
22
import callsites from 'callsites';
3-
import { err, ok, Files } from './core/_internal';
3+
import { Files } from './core/_internal';
44
import { merge } from 'rxjs';
55
import { Services } from './core/ioc';
66
import { Wrapper } from './types/core';

src/types/core-modules.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { AnyCommandPlugin, AnyEventPlugin, ControlPlugin, InitPlugin } from './c
2020
import { Awaitable, Args, SlashOptions, SernEventsMapping } from './utility';
2121

2222

23-
2423
export interface CommandMeta {
2524
fullPath: string;
2625
id: string;

src/types/ioc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ export type DependencyList = [
2626

2727
export interface CoreDependencies {
2828
'@sern/client': () => Contracts.Emitter;
29-
'@sern/logger'?: () => Contracts.Logging;
3029
'@sern/emitter': () => Contracts.Emitter;
3130
'@sern/store': () => Contracts.CoreModuleStore;
3231
'@sern/modules': () => Contracts.ModuleManager;
3332
'@sern/errors': () => Contracts.ErrorHandling;
33+
'@sern/logger'?: () => Contracts.Logging;
34+
'@sern/localizer'?: () => Contracts.Localizer
3435
}
3536

3637
export type DependencyFromKey<T extends keyof Dependencies> = Dependencies[T];
@@ -45,7 +46,6 @@ export interface DependencyConfiguration {
4546

4647
//Extra modules that are preconfigured and ready to use! @sern/localizer is an example
4748
include?: string[]
48-
4949
build: (
5050
root: Container<Omit<CoreDependencies, '@sern/client'>, {}>,
5151
) => Container<Dependencies, {}>;

0 commit comments

Comments
 (0)