Skip to content

Commit

Permalink
add localsFor
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed Jan 27, 2024
1 parent 262fa6a commit 622f8a7
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"discord.js": "^14.11.0",
"eslint": "8.39.0",
"prettier": "2.8.8",
"shrimple-locales": "^0.1.2",
"shrimple-locales": "^0.2.0",
"tsup": "^6.7.0",
"typescript": "5.0.2",
"vitest": "latest"
Expand Down
2 changes: 1 addition & 1 deletion src/core/contracts/localizer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

export interface Localizer {
translate(text: string, local: string) : string;
// translationsFor(path: string): Record<string, any>
translationsFor(path: string): Record<string, any>
}
6 changes: 4 additions & 2 deletions src/core/ioc/dependency-injection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ export function Services<const T extends (keyof Dependencies)[]>(...keys: [...T]
}



export function useContainer<const T extends Dependencies>() {
return <V extends (keyof T)[]>(...keys: [...V]) =>
keys.map(key => useContainerRaw().get(key as keyof Dependencies)) as IntoDependencies<V>;
}


export function $local(i: string, local: string) {
export const local = (i: string, local: string) => {
return Service('@sern/localizer').translate(i, local)
}

export const localsFor = (path: string) => {
return Service('@sern/localizer').translationsFor(path)
}
2 changes: 1 addition & 1 deletion src/core/ioc/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { makeDependencies } from './base';
export { Service, Services, single, transient, $local } from './dependency-injection';
export { Service, Services, single, transient, local, localsFor } from './dependency-injection';
12 changes: 6 additions & 6 deletions src/optional/localizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import { filename } from '../core/module-loading'
import assert from 'node:assert';

/**
* @internal
* @since 2.0.0
* Version 4.0.0 will internalize this api. Please refrain from using ModuleStore!
* @since 3.4.0
*/
export class ShrimpleLocalizer implements Localizer, Init {
private __localization!: Localization;
private __localization_map!: Record<string, any>
constructor(){}

translationsFor(path: string): Record<string, any> {
return this.__localization.localizationFor(path);
}

translate(text: string, local: string): string {
this.__localization.changeLanguage(local);
return this.__localization.get(text);
Expand All @@ -28,13 +29,12 @@ export class ShrimpleLocalizer implements Localizer, Init {
fallbackLocale: "en",
locales: map
});
this.__localization_map = map
}

private async readLocalizationDirectory() {
const translationFiles = [];
const localPath = resolve('locals');
assert(existsSync(localPath), "You need to create a directory called \"locals\" for the localizer")
assert(existsSync(localPath), "No directory \"locals\" found for the localizer")
for(const json of await fs.readdir(localPath)) {
translationFiles.push({ [filename(json)]:
JSON.parse(await fs.readFile(join(localPath, json), 'utf8')) })
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ __metadata:
iti: ^0.6.0
prettier: 2.8.8
rxjs: ^7.8.0
shrimple-locales: ^0.1.2
shrimple-locales: ^0.2.0
ts-results-es: ^4.0.0
tsup: ^6.7.0
typescript: 5.0.2
Expand Down Expand Up @@ -3116,10 +3116,10 @@ __metadata:
languageName: node
linkType: hard

"shrimple-locales@npm:^0.1.2":
version: 0.1.2
resolution: "shrimple-locales@npm:0.1.2"
checksum: 3e30c4a6aff792e5ef51495f37e45704526a3b42d58fb21d5219fd7ae81007e7ca9b651d0551d2e89ab00c28457284d8f1fad2f2295095a22cd6b272172d55d1
"shrimple-locales@npm:^0.2.0":
version: 0.2.0
resolution: "shrimple-locales@npm:0.2.0"
checksum: 9b5360e6ca5b2e75fdbaaeee0286015294b64773950924088f6088184f010837a89e84101eddfedcda5dd660a97df84fee6e1e95345e01f7e1edfff6b4ff2341
languageName: node
linkType: hard

Expand Down

0 comments on commit 622f8a7

Please sign in to comment.