Skip to content

Commit 4fc81c8

Browse files
committed
feat: using signal for defaultLang and currentLang in the store
1 parent 154997c commit 4fc81c8

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

projects/ngx-translate/src/lib/translate.service.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Inject, Injectable, InjectionToken } from "@angular/core";
1+
import {Inject, Injectable, InjectionToken, Signal} from "@angular/core";
22
import { concat, defer, forkJoin, isObservable, Observable, of } from "rxjs";
33
import { concatMap, map, shareReplay, switchMap, take } from "rxjs/operators";
44
import { MissingTranslationHandler } from "./missing-translation-handler";
@@ -122,17 +122,28 @@ export class TranslateService {
122122
/**
123123
* The default lang to fallback when translations are missing on the current lang
124124
*/
125-
get defaultLang(): Language {
125+
get defaultLang(): Language|undefined {
126126
return this.store.getDefaultLanguage();
127127
}
128128

129+
get $defaultLang(): Signal<Language|undefined> {
130+
return this.store.$defaultLang;
131+
}
132+
133+
129134
/**
130135
* The lang currently used
131136
*/
132-
get currentLang(): Language {
137+
get currentLang(): Language|undefined {
133138
return this.store.getCurrentLanguage();
134139
}
135140

141+
142+
get $currentLang(): Signal<Language|undefined> {
143+
return this.store.$currentLang;
144+
}
145+
146+
136147
/**
137148
* an array of langs
138149
*/
@@ -198,7 +209,7 @@ export class TranslateService {
198209
/**
199210
* Gets the default language used
200211
*/
201-
public getDefaultLang(): string {
212+
public getDefaultLang(): Language|undefined {
202213
return this.defaultLang;
203214
}
204215

@@ -510,7 +521,12 @@ export class TranslateService {
510521
/**
511522
* Sets the translated value of a key, after compiling it
512523
*/
513-
public set(key: string, translation: string|TranslationObject, lang: Language = this.currentLang): void {
524+
public set(key: string, translation: string|TranslationObject, lang: Language|undefined = this.currentLang): void {
525+
526+
if(lang === undefined)
527+
{
528+
throw new Error('Set the current language or pass a language code as a second parameter');
529+
}
514530

515531
this.store.setTranslations(
516532
lang,

projects/ngx-translate/src/lib/translate.store.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from "./translate.service";
77
import {Observable, Subject} from "rxjs";
88
import {getValue, mergeDeep} from "./util";
9+
import {Signal, signal, WritableSignal} from "@angular/core";
910

1011

1112
type DeepReadonly<T> = {
@@ -19,8 +20,8 @@ export class TranslateStore
1920
private _onLangChange: Subject<LangChangeEvent> = new Subject<LangChangeEvent>();
2021
private _onDefaultLangChange: Subject<DefaultLangChangeEvent> = new Subject<DefaultLangChangeEvent>();
2122

22-
private defaultLang!: Language;
23-
private currentLang!: Language;
23+
private _$defaultLang: WritableSignal<Language|undefined> = signal<Language|undefined>(undefined);
24+
private _$currentLang: WritableSignal<Language|undefined> = signal<Language|undefined>(undefined);
2425

2526
private translations: Record<Language, InterpolatableTranslationObject> = {};
2627
private languages: Language[] = [];
@@ -42,22 +43,32 @@ export class TranslateStore
4243
return this.languages;
4344
}
4445

45-
public getCurrentLanguage(): Language
46+
public getCurrentLanguage(): Language|undefined
4647
{
47-
return this.currentLang;
48+
return this.$currentLang();
4849
}
4950

50-
public getDefaultLanguage(): Language
51+
get $currentLang() : Signal<Language|undefined>
5152
{
52-
return this.defaultLang;
53+
return this._$currentLang.asReadonly();
54+
}
55+
56+
public getDefaultLanguage(): Language|undefined
57+
{
58+
return this._$defaultLang();
59+
}
60+
61+
get $defaultLang() : Signal<Language|undefined>
62+
{
63+
return this._$defaultLang.asReadonly();
5364
}
5465

5566
/**
5667
* Changes the default lang
5768
*/
5869
public setDefaultLang(lang: string, emitChange = true): void
5970
{
60-
this.defaultLang = lang;
71+
this._$defaultLang.set(lang);
6172
if (emitChange)
6273
{
6374
this._onDefaultLangChange.next({lang: lang, translations: this.translations[lang]});
@@ -66,7 +77,7 @@ export class TranslateStore
6677

6778
public setCurrentLang(lang: string, emitChange = true): void
6879
{
69-
this.currentLang = lang;
80+
this._$currentLang.set(lang);
7081
if (emitChange)
7182
{
7283
this._onLangChange.next({lang: lang, translations: this.translations[lang]});
@@ -123,10 +134,13 @@ export class TranslateStore
123134

124135
public getTranslation(key: string, useDefaultLang: boolean): InterpolatableTranslation
125136
{
126-
let text = this.getValue(this.currentLang, key);
127-
if(text === undefined && this.defaultLang != null && this.defaultLang !== this.currentLang && useDefaultLang)
137+
const currentLang = this.getCurrentLanguage();
138+
const defaultLang = this.getDefaultLanguage();
139+
140+
let text = (currentLang !== undefined) ? this.getValue(currentLang, key) : undefined;
141+
if(text === undefined && defaultLang != null && defaultLang !== currentLang && useDefaultLang)
128142
{
129-
text = this.getValue(this.defaultLang, key);
143+
text = this.getValue(defaultLang, key);
130144
}
131145
return text;
132146
}

0 commit comments

Comments
 (0)