Skip to content

Commit

Permalink
refactor: improve translator setup
Browse files Browse the repository at this point in the history
  • Loading branch information
dominiq007 committed Feb 18, 2024
1 parent fe798fa commit 08a99aa
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/http/http_request.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,8 @@ export class HttpRequest {
return this.sessionObject;
}

public translate(text: string, quantity = 1): string {
return this.localizator.translate(this.locale, text, quantity);
public async translate(text: string, quantity = 1): Promise<string> {
return await this.localizator.translate(this.locale, text, quantity);
}

public url(): string {
Expand Down
22 changes: 17 additions & 5 deletions src/localizator/localizator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,22 @@ export class Localizator {
}
}

public all(locale: string): Record<string, string | string[]> {
public async all(locale: string): Promise<Record<string, string | string[]>> {
if (!this.translations.size) {
await this.loadTranslations();
}

return Object.fromEntries(
locale === this.configurator.entries.locales.default
? new Map()
: this.translations.get(locale) ?? new Map(),
);
}

public async reload(): Promise<void> {
await this.loadTranslations();
}

public set(
locale: string,
text: string,
Expand All @@ -52,11 +60,15 @@ export class Localizator {
this.translations.get(locale)!.set(text, translation);
}

public async setup(): Promise<void> {
await this.loadTranslations();
}
public async translate(
locale: string,
text: string,
quantity = 1,
): Promise<string> {
if (!this.translations.size) {
await this.loadTranslations();
}

public translate(locale: string, text: string, quantity = 1): string {
if (quantity > 1) {
const key = [...this.translations.keys()].find((key) => {
return key.startsWith(`${text}|`);
Expand Down
6 changes: 4 additions & 2 deletions src/localizator/localizator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { inject } from '../injector/functions/inject.function.ts';
Deno.test('localizator module', async (test) => {
const localizator = inject(Localizator);

await test.step('localizator properly sets translations', () => {
await test.step('localizator properly sets translations', async () => {
localizator.set('pl', 'hello world', 'witaj świecie');

expect(localizator.translate('pl', 'hello world')).toBe('witaj świecie');
expect(await localizator.translate('pl', 'hello world')).toBe(
'witaj świecie',
);
});

await test.step('localizator properly returns translation list', () => {
Expand Down
4 changes: 1 addition & 3 deletions src/server/server.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,6 @@ export class Server implements Disposable {

this.configurator.setup(this.options.config);

await this.localizator.setup();

if (!this.configurator.entries.isDenoDeploy) {
this.arguments.dev
? await this.setupDevelopmentEnvironment()
Expand Down Expand Up @@ -501,7 +499,7 @@ export class Server implements Disposable {
break;

case path.includes('locales'):
await this.localizator.setup();
await this.localizator.reload();

break;

Expand Down
4 changes: 2 additions & 2 deletions src/templates/template_compiler.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,8 @@ export class TemplateCompiler {
{},
);

const translationCallback = (text: string, quantity = 1) => {
return this.options.request?.translate(text, quantity) ?? text;
const translationCallback = async (text: string, quantity = 1) => {
return await this.options.request?.translate(text, quantity) ?? text;
};

const globalVariables = {
Expand Down
11 changes: 5 additions & 6 deletions src/validator/validator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,11 @@ export class Validator {
}

errors[fieldName].push(
this.localizator
.translate(
subject.locale ?? errorLocale ??
this.configurator.entries.locales.default,
ruleObject.errorMessage,
)
(await this.localizator.translate(
subject.locale ?? errorLocale ??
this.configurator.entries.locales.default,
ruleObject.errorMessage,
))
.replaceAll(':field', fieldName)
.replaceAll(
':value',
Expand Down

0 comments on commit 08a99aa

Please sign in to comment.