Skip to content

feat(store): initialized$ #1575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions projects/ngx-translate/src/lib/translate.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inject, Injectable, InjectionToken } from "@angular/core";
import { concat, defer, forkJoin, isObservable, Observable, of } from "rxjs";
import { concatMap, map, shareReplay, switchMap, take } from "rxjs/operators";
import { concat, defer, firstValueFrom, forkJoin, isObservable, Observable, of } from "rxjs";
import { concatMap, filter, map, shareReplay, switchMap, take } from "rxjs/operators";
import { MissingTranslationHandler } from "./missing-translation-handler";
import { TranslateCompiler } from "./translate.compiler";
import { TranslateLoader } from "./translate.loader";
Expand Down Expand Up @@ -604,11 +604,18 @@ export class TranslateService implements ITranslateService {
/**
* Sets the translated value of a key, after compiling it
*/
public set(
public async set(
key: string,
translation: string | TranslationObject,
lang: Language = this.getCurrentLang(),
): void {
): Promise<void> {
await firstValueFrom(
this.store.initialized$.pipe(
filter((event) => (event ? true : false)),
take(1),
),
);

this.store.setTranslations(
lang,
insertValue(
Expand Down
16 changes: 11 additions & 5 deletions projects/ngx-translate/src/lib/translate.store.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Injectable } from "@angular/core";
import { BehaviorSubject, Observable, Subject } from "rxjs";
import {
InterpolatableTranslationObject,
FallbackLangChangeEvent,
InterpolatableTranslation,
InterpolatableTranslationObject,
LangChangeEvent,
TranslationChangeEvent,
Language,
InterpolatableTranslation,
TranslationChangeEvent,
} from "./translate.service";
import { Observable, Subject } from "rxjs";
import { getValue, mergeDeep } from "./util";
import { Injectable } from "@angular/core";

export type DeepReadonly<T> = {
readonly [K in keyof T]: T[K] extends object ? DeepReadonly<T[K]> : T[K];
Expand All @@ -28,6 +28,8 @@ export class TranslateStore {
private translations: Record<Language, InterpolatableTranslationObject> = {};
private languages: Language[] = [];

initialized$ = new BehaviorSubject(false);

public getTranslations(language: Language): DeepReadonly<InterpolatableTranslationObject> {
return this.translations[language];
}
Expand All @@ -46,6 +48,10 @@ export class TranslateStore {
lang: language,
translations: this.getTranslations(language),
});

if (!this.initialized$.getValue()) {
this.initialized$.next(true);
}
}

public getLanguages(): readonly Language[] {
Expand Down
14 changes: 7 additions & 7 deletions projects/ngx-translate/src/tests/translate.compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { Observable, of } from "rxjs";
import {
InterpolatableTranslationObject,
TranslateCompiler,
TranslateNoOpCompiler,
TranslateLoader,
TranslateNoOpCompiler,
TranslateService,
Translation,
TranslationObject,
provideTranslateService,
provideTranslateLoader,
provideTranslateCompiler,
provideTranslateLoader,
provideTranslateService,
} from "../public-api";

const translations: TranslationObject = { LOAD: "This is a test" };
Expand Down Expand Up @@ -58,8 +58,8 @@ describe("TranslateCompiler", () => {
expect(translate.instant("SET-TRANSLATION")).toBe("A manually added translation");
});

it("should use the compiler on manually adding a single translation", () => {
translate.set("SET", "Another manually added translation", "en");
it("should use the compiler on manually adding a single translation", async () => {
await translate.set("SET", "Another manually added translation", "en");
expect(translate.instant("SET")).toBe("Another manually added translation");
});
});
Expand Down Expand Up @@ -118,8 +118,8 @@ describe("TranslateCompiler", () => {
);
});

it("should use the compiler on manually adding a single translation", () => {
translate.set("SET", "Another manually added translation", "en");
it("should use the compiler on manually adding a single translation", async () => {
await translate.set("SET", "Another manually added translation", "en");
expect(translate.instant("SET")).toBe("Another manually added translation|compiled");
});
});
Expand Down
8 changes: 4 additions & 4 deletions projects/ngx-translate/src/tests/translate.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,17 +612,17 @@ describe("TranslateService", () => {
});

describe("set()", () => {
it("should set translations with nested key (string)", () => {
it("should set translations with nested key (string)", async () => {
translate.setTranslation("en", { profile: "test" });
translate.use("en");
translate.set("profile.name", "Profile Name", "en");
await translate.set("profile.name", "Profile Name", "en");
expect(translate.instant("profile.name")).toEqual("Profile Name");
});

it("should set translations with nested key (object)", () => {
it("should set translations with nested key (object)", async () => {
translate.setTranslation("en", { profile: "test" });
translate.use("en");
translate.set("a.b", { c: { d: "setting nested object" } }, "en");
await translate.set("a.b", { c: { d: "setting nested object" } }, "en");
expect(translate.instant("a.b.c.d")).toEqual("setting nested object");
});

Expand Down