Skip to content

Commit 6378eeb

Browse files
committed
fix(ngx-i18n): Add initializeLanguage
1 parent 591d46c commit 6378eeb

File tree

4 files changed

+44
-13
lines changed

4 files changed

+44
-13
lines changed

libs/i18n/src/lib/guards/i18n/i18n.guard.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('NgxI18nGuard', () => {
1414
currentLanguage: 'nl',
1515
availableLanguages: ['nl', 'en'],
1616
setCurrentLanguage: jasmine.createSpy(),
17+
initializeLanguage: jasmine.createSpy(),
1718
};
1819

1920
beforeEach(() => {

libs/i18n/src/lib/guards/i18n/i18n.guard.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ export const NgxI18nGuard: CanActivateFn = (route: ActivatedRouteSnapshot): bool
1616
const i18nService = inject(NgxI18nRootService);
1717
const config: NgxI18nConfiguration = inject(NgxI18nConfigurationToken);
1818

19+
// Iben: Initialize the current language of the application
20+
i18nService.initializeLanguage();
21+
1922
// Iben: Get the two language params
20-
const currentLanguage = i18nService.currentLanguage || config.defaultLanguage;
23+
const currentLanguage = i18nService.currentLanguage;
2124
const routeLanguage = getLanguage(route, config);
2225

2326
// Iben: If both languages are the same, we can continue

libs/i18n/src/lib/guards/set-language/set-language.guard.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { inject } from '@angular/core';
22
import { CanActivateFn, Router } from '@angular/router';
33

44
import { NgxI18nRootService } from '../../services';
5-
import { NgxI18nConfigurationToken } from '../../tokens';
6-
import { NgxI18nConfiguration } from '../../i18n.types';
75

86
/**
97
* Set the language in the url of the route
@@ -14,12 +12,9 @@ export const NgxI18nSetLanguageGuard: CanActivateFn = (): Promise<boolean> => {
1412
// Iben: Fetch all injectables
1513
const router: Router = inject(Router);
1614
const i18nService = inject(NgxI18nRootService);
17-
const config: NgxI18nConfiguration = inject(NgxI18nConfigurationToken);
1815

19-
// Iben: If no language was set already, we set the default language
20-
if (!i18nService.currentLanguage) {
21-
i18nService.setCurrentLanguage(config.defaultLanguage);
22-
}
16+
// Iben: Initialize the current language of the application
17+
i18nService.initializeLanguage();
2318

2419
//Iben: Navigate to the set currentLanguage
2520
return router.navigate(['/', i18nService.currentLanguage]);

libs/i18n/src/lib/services/root-i18n/root-i18n.service.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,56 @@ export class NgxI18nRootService {
4040
* @param language - The provided language
4141
*/
4242
public setCurrentLanguage(language: string): void {
43-
// Iben: If a language is set that's not part of the available languages, we return a warn
43+
// Iben: Save the new language
44+
let newLanguage = language;
45+
46+
// Iben: Check if the new language is part of the available languages
4447
if (!this.configuration.availableLanguages.includes(language)) {
48+
// Iben: If a language is set that's not part of the available languages, we return a warn
4549
console.warn(
4650
`NgxI18n: A language, ${language}, was attempted to be set that was not part of the available languages (${this.configuration.availableLanguages.join(
4751
', '
4852
)})`
4953
);
5054

51-
// Iben: Early exit
52-
return;
55+
// Iben: If there is already a language set, we early exit and keep the remaining language
56+
if (this.currentLanguage) {
57+
return;
58+
}
59+
60+
// Iben: If no language exists, we use the default language
61+
newLanguage = this.configuration.defaultLanguage;
5362
}
5463

5564
// Iben: Save the current language to the localStorage when we're in the browser
5665
if (isPlatformBrowser(this.platformId)) {
57-
localStorage.setItem('ngx-language', language);
66+
localStorage.setItem('ngx-i18n-language', newLanguage);
5867
}
5968

6069
// Iben: Update the subject
61-
this.currentLanguageSubject.next(language);
70+
this.currentLanguageSubject.next(newLanguage);
71+
}
72+
73+
/**
74+
* Sets the initial language of the application when no language is set yet.
75+
*
76+
* If a previous language was set in the local storage, said language is used. If not, the default language gets used.
77+
*/
78+
public initializeLanguage(): void {
79+
// Iben: If the current language already exists, we early exit
80+
if (this.currentLanguage) {
81+
return;
82+
}
83+
84+
// Iben: If the current language does not exist, we check if it exists in the local storage, if not, we use the default config
85+
let language = this.configuration.defaultLanguage;
86+
87+
if (isPlatformBrowser(this.platformId)) {
88+
language =
89+
localStorage.getItem('ngx-i18n-language') || this.configuration.defaultLanguage;
90+
}
91+
92+
// Iben: We set the new language
93+
this.setCurrentLanguage(language);
6294
}
6395
}

0 commit comments

Comments
 (0)