Skip to content

Commit

Permalink
rename Localizer -> Localization
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed Feb 4, 2024
1 parent 99aa7bc commit 6b3643c
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/core/contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export * from './module-store';
export * from './init';
export * from './emitter';
export * from './disposable'
export * from './localizer'
export * from './localization'
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* @since 3.4.0
* Represents a localizer which can translate text for you.
* Represents a localization service which can translate text for you.
* The internal representation of localization may differ between Localizers,
* But we are insured that translate, translateFor, currentLocale are always available
*/
export interface Localizer {
export interface Localization {
translate(text: string, local: string) : string;
translationsFor(path: string): Record<string, any>
currentLocale: string
Expand Down
3 changes: 0 additions & 3 deletions src/core/ioc/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ function composeRoot(
if (!hasLogger) {
insertLogger(container);
}
// if(conf.include?.includes('@sern/localizer')) {
// insertLocalizer(container);
// }
//Build the container based on the callback provided by the user
conf.build(container as CoreContainer<Omit<CoreDependencies, '@sern/client'>>);

Expand Down
1 change: 1 addition & 0 deletions src/core/ioc/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class CoreContainer<T extends Partial<Dependencies>> extends Container<T,
'dispose' in value ? [key] : []);

for(const key of otherDisposables) {
//possible source of bug: dispose is a property.
this.addDisposer({ [key]: (dep: Disposable) => dep.dispose() } as never);
}
await super.disposeAll();
Expand Down
16 changes: 12 additions & 4 deletions src/core/ioc/dependency-injection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useContainerRaw } from './base';
import { fileURLToPath } from 'node:url';
import path from 'node:path';
import { requir } from '../module-loading';
import type { Localizer } from '../contracts';
import type { Localization } from '../contracts';

/**
* @__PURE__
Expand Down Expand Up @@ -79,12 +79,20 @@ export const local = (i: string, local: string) => {
export const localsFor = (path: string) => {
return Service('@sern/localizer').translationsFor(path)
}

export const Localization = (defaultLocale?: string) => {
/**
* A service which provides simple file based localization. Add this while making dependencies.
* @example
* ```ts
* await makeDependencies(({ add }) => {
* add('@sern/localizer', DefaultLocalization());
* });
* ```
**/
export const DefaultLocalization = (defaultLocale?: string) => {
const packageDirectory = fileURLToPath(import.meta.url);
const pathToLocalizer= path.resolve(packageDirectory, "../", "optional", "localizer");
const { ShrimpleLocalizer } = requir(pathToLocalizer);
const localizer = new ShrimpleLocalizer as Localizer;
const localizer = new ShrimpleLocalizer as Localization;
if (defaultLocale) {
localizer.currentLocale = defaultLocale;
}
Expand Down
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, localsFor, Localization } from './dependency-injection';
export { Service, Services, single, transient, local, localsFor, DefaultLocalization } from './dependency-injection';
10 changes: 5 additions & 5 deletions src/optional/localizer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Localizer, Init } from '../core/contracts'
import { Localization } from 'shrimple-locales'
import type { Localization, Init } from '../core/contracts'
import { Localization as LocalsProvider } from 'shrimple-locales'
import fs from 'node:fs/promises'
import { existsSync } from 'node:fs'
import { join, resolve } from 'node:path';
Expand All @@ -10,8 +10,8 @@ import assert from 'node:assert';
* @since 3.4.0
* @internal
*/
export class ShrimpleLocalizer implements Localizer, Init {
private __localization!: Localization;
export class ShrimpleLocalizer implements Localization, Init {
private __localization!: LocalsProvider;
constructor(){}
currentLocale: string = "en-US";

Expand All @@ -26,7 +26,7 @@ export class ShrimpleLocalizer implements Localizer, Init {

async init() {
const map = await this.readLocalizationDirectory();
this.__localization = new Localization({
this.__localization = new LocalsProvider({
defaultLocale: this.currentLocale,
fallbackLocale: "en-US",
locales: map
Expand Down
2 changes: 1 addition & 1 deletion src/types/ioc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface CoreDependencies {
'@sern/modules': () => Contracts.ModuleManager;
'@sern/errors': () => Contracts.ErrorHandling;
'@sern/logger'?: () => Contracts.Logging;
'@sern/localizer'?: () => Contracts.Localizer
'@sern/localizer'?: () => Contracts.Localization
}

export type DependencyFromKey<T extends keyof Dependencies> = Dependencies[T];
Expand Down

0 comments on commit 6b3643c

Please sign in to comment.