From 6b3643caeb668ef7998bdd59cf0646c58e0a3d5c Mon Sep 17 00:00:00 2001 From: jacob Date: Sun, 4 Feb 2024 16:28:00 -0600 Subject: [PATCH] rename Localizer -> Localization --- src/core/contracts/index.ts | 2 +- .../contracts/{localizer.ts => localization.ts} | 4 ++-- src/core/ioc/base.ts | 3 --- src/core/ioc/container.ts | 1 + src/core/ioc/dependency-injection.ts | 16 ++++++++++++---- src/core/ioc/index.ts | 2 +- src/optional/localizer.ts | 10 +++++----- src/types/ioc.ts | 2 +- 8 files changed, 23 insertions(+), 17 deletions(-) rename src/core/contracts/{localizer.ts => localization.ts} (75%) diff --git a/src/core/contracts/index.ts b/src/core/contracts/index.ts index 5c336761..20910fbe 100644 --- a/src/core/contracts/index.ts +++ b/src/core/contracts/index.ts @@ -5,4 +5,4 @@ export * from './module-store'; export * from './init'; export * from './emitter'; export * from './disposable' -export * from './localizer' +export * from './localization' diff --git a/src/core/contracts/localizer.ts b/src/core/contracts/localization.ts similarity index 75% rename from src/core/contracts/localizer.ts rename to src/core/contracts/localization.ts index 25663c32..f1b40a82 100644 --- a/src/core/contracts/localizer.ts +++ b/src/core/contracts/localization.ts @@ -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 currentLocale: string diff --git a/src/core/ioc/base.ts b/src/core/ioc/base.ts index d978ed93..dbf363db 100644 --- a/src/core/ioc/base.ts +++ b/src/core/ioc/base.ts @@ -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>); diff --git a/src/core/ioc/container.ts b/src/core/ioc/container.ts index 5cacc44b..d01b2fc0 100644 --- a/src/core/ioc/container.ts +++ b/src/core/ioc/container.ts @@ -48,6 +48,7 @@ export class CoreContainer> extends Container dep.dispose() } as never); } await super.disposeAll(); diff --git a/src/core/ioc/dependency-injection.ts b/src/core/ioc/dependency-injection.ts index 531a6774..7c7d67dd 100644 --- a/src/core/ioc/dependency-injection.ts +++ b/src/core/ioc/dependency-injection.ts @@ -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__ @@ -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; } diff --git a/src/core/ioc/index.ts b/src/core/ioc/index.ts index 8961acb2..90b296b9 100644 --- a/src/core/ioc/index.ts +++ b/src/core/ioc/index.ts @@ -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'; diff --git a/src/optional/localizer.ts b/src/optional/localizer.ts index c6c0654f..8d7bdf92 100644 --- a/src/optional/localizer.ts +++ b/src/optional/localizer.ts @@ -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'; @@ -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"; @@ -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 diff --git a/src/types/ioc.ts b/src/types/ioc.ts index bcf4d53f..4b05c319 100644 --- a/src/types/ioc.ts +++ b/src/types/ioc.ts @@ -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 = Dependencies[T];