Skip to content

Commit f6bc46f

Browse files
authored
fix(ngx-i18n): Add initializeLanguage
1 parent 591d46c commit f6bc46f

File tree

4 files changed

+60
-17
lines changed

4 files changed

+60
-17
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: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,68 @@ 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: get the new language
44+
const newLanguage = this.getNewLanguage(language);
45+
46+
// Iben: Save the current language to the localStorage when we're in the browser
47+
if (isPlatformBrowser(this.platformId)) {
48+
localStorage.setItem('ngx-i18n-language', newLanguage);
49+
}
50+
51+
// Iben: Update the subject
52+
this.currentLanguageSubject.next(newLanguage);
53+
}
54+
55+
/**
56+
* Sets the initial language of the application when no language is set yet.
57+
*
58+
* If a previous language was set in the local storage, said language is used. If not, the default language gets used.
59+
*/
60+
public initializeLanguage(): void {
61+
// Iben: If the current language already exists, we early exit
62+
if (this.currentLanguage) {
63+
return;
64+
}
65+
66+
// Iben: If the current language does not exist, we check if it exists in the local storage, if not, we use the default config
67+
let language = this.configuration.defaultLanguage;
68+
69+
if (isPlatformBrowser(this.platformId)) {
70+
language =
71+
localStorage.getItem('ngx-i18n-language') || this.configuration.defaultLanguage;
72+
}
73+
74+
// Iben: We set the new language
75+
this.setCurrentLanguage(language);
76+
}
77+
78+
/**
79+
* Checks if the newly proposed language can be set, if not we return either the current language or the default language
80+
*
81+
* @param {string} language - The newly proposed language
82+
*/
83+
private getNewLanguage(language: string): string {
84+
// Iben: Save the currently being set language
85+
let newLanguage = language;
86+
87+
// Iben: Check if the new language is part of the available languages
4488
if (!this.configuration.availableLanguages.includes(language)) {
89+
// Iben: If a language is set that's not part of the available languages, we return a warn
4590
console.warn(
4691
`NgxI18n: A language, ${language}, was attempted to be set that was not part of the available languages (${this.configuration.availableLanguages.join(
4792
', '
4893
)})`
4994
);
5095

51-
// Iben: Early exit
52-
return;
53-
}
96+
// Iben: If there is already a language set, we early exit and keep the remaining language
97+
if (this.currentLanguage) {
98+
return this.currentLanguage;
99+
}
54100

55-
// Iben: Save the current language to the localStorage when we're in the browser
56-
if (isPlatformBrowser(this.platformId)) {
57-
localStorage.setItem('ngx-language', language);
101+
// Iben: If no language exists, we use the default language
102+
newLanguage = this.configuration.defaultLanguage;
58103
}
59104

60-
// Iben: Update the subject
61-
this.currentLanguageSubject.next(language);
105+
return newLanguage;
62106
}
63107
}

0 commit comments

Comments
 (0)