Skip to content

Commit 1deadb8

Browse files
alisaismailatiatarix83
authored andcommitted
[DSC-1742] Resolves language change using query params during SSR
# Conflicts: # src/app/core/locale/locale.service.spec.ts # src/app/core/locale/locale.service.ts # src/app/core/locale/server-locale.service.ts
1 parent 7ed6fbc commit 1deadb8

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

src/app/core/locale/locale.service.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { PLATFORM_ID } from '@angular/core';
12
import {
23
TestBed,
34
waitForAsync,
@@ -34,6 +35,7 @@ describe('LocaleService test suite', () => {
3435
let routeService;
3536
let document;
3637
let spyOnGetLanguage;
38+
const platformId = 'browser';
3739

3840

3941
const translateServiceStub: any = {
@@ -72,6 +74,7 @@ describe('LocaleService test suite', () => {
7274
{ provide: RouteService, useValue: routeServiceStub },
7375
{ provide: TranslateService, useValue: translateServiceStub },
7476
{ provide: Document, useValue: document },
77+
{ provide: PLATFORM_ID, useValue: platformId },
7578
],
7679
});
7780
}));
@@ -82,7 +85,7 @@ describe('LocaleService test suite', () => {
8285
routeService = TestBed.inject(RouteService);
8386
window = new NativeWindowRef();
8487
document = { documentElement: { lang: 'en' } };
85-
service = new LocaleService(window, cookieService, translateService, authService, routeService, document);
88+
service = new LocaleService(window, cookieService, translateService, authService, routeService, document, platformId);
8689
serviceAsAny = service;
8790
spyOnGet = spyOn(cookieService, 'get');
8891
spyOnSet = spyOn(cookieService, 'set');

src/app/core/locale/locale.service.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import { DOCUMENT } from '@angular/common';
1+
import {
2+
DOCUMENT,
3+
isPlatformBrowser,
4+
} from '@angular/common';
25
import {
36
Inject,
47
Injectable,
8+
PLATFORM_ID,
59
} from '@angular/core';
610
import { TranslateService } from '@ngx-translate/core';
711
import {
@@ -58,6 +62,7 @@ export class LocaleService {
5862
protected authService: AuthService,
5963
protected routeService: RouteService,
6064
@Inject(DOCUMENT) protected document: any,
65+
@Inject(PLATFORM_ID) protected platformId: string,
6166
) {
6267
this.initDefaults();
6368
}
@@ -69,7 +74,9 @@ export class LocaleService {
6974
this.routeService.getQueryParameterValue('lang').subscribe(lang => {
7075
if (lang && this.translate.getLangs().includes(lang)) {
7176
this.setCurrentLanguageCode(lang);
72-
this.routeService.removeQueryParam('lang');
77+
if (isPlatformBrowser(this.platformId)) {
78+
this.routeService.removeQueryParam('lang');
79+
}
7380
}
7481
});
7582
}

src/app/core/locale/server-locale.service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { DOCUMENT } from '@angular/common';
22
import {
33
Inject,
44
Injectable,
5+
PLATFORM_ID,
56
} from '@angular/core';
67
import { TranslateService } from '@ngx-translate/core';
78
import {
@@ -44,8 +45,9 @@ export class ServerLocaleService extends LocaleService {
4445
protected authService: AuthService,
4546
protected routeService: RouteService,
4647
@Inject(DOCUMENT) protected document: any,
48+
@Inject(PLATFORM_ID) protected platformId: string,
4749
) {
48-
super(_window, cookie, translate, authService, routeService, document);
50+
super(_window, cookie, translate, authService, routeService, document, platformId);
4951
}
5052

5153
/**

0 commit comments

Comments
 (0)