Skip to content

Commit c62d6eb

Browse files
alisaismailatiatarix83
authored andcommitted
Merged in task/dspace-cris-2023_02_x/DSC-1742 (pull request DSpace#2509)
[DSC-1742] Resolves language change using query params during SSR Approved-by: Giuseppe Digilio
2 parents 117c0f0 + 9270862 commit c62d6eb

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { NativeWindowRef } from '../services/window.service';
1111
import { RouteService } from '../services/route.service';
1212
import { routeServiceStub } from '../../shared/testing/route-service.stub';
1313
import { of as observableOf } from 'rxjs';
14+
import { PLATFORM_ID } from '@angular/core';
1415

1516
describe('LocaleService test suite', () => {
1617
let service: LocaleService;
@@ -24,6 +25,7 @@ describe('LocaleService test suite', () => {
2425
let routeService;
2526
let document;
2627
let spyOnGetLanguage;
28+
const platformId = 'browser';
2729

2830

2931
const translateServiceStub: any = {
@@ -62,6 +64,7 @@ describe('LocaleService test suite', () => {
6264
{ provide: RouteService, useValue: routeServiceStub },
6365
{ provide: TranslateService, useValue: translateServiceStub },
6466
{ provide: Document, useValue: document },
67+
{ provide: PLATFORM_ID, useValue: platformId },
6568
]
6669
});
6770
}));
@@ -72,7 +75,7 @@ describe('LocaleService test suite', () => {
7275
routeService = TestBed.inject(RouteService);
7376
window = new NativeWindowRef();
7477
document = { documentElement: { lang: 'en' } };
75-
service = new LocaleService(window, cookieService, translateService, authService, routeService, document);
78+
service = new LocaleService(window, cookieService, translateService, authService, routeService, document, platformId);
7679
serviceAsAny = service;
7780
spyOnGet = spyOn(cookieService, 'get');
7881
spyOnSet = spyOn(cookieService, 'set');

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Inject, Injectable } from '@angular/core';
1+
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
22

33
import { TranslateService } from '@ngx-translate/core';
44

@@ -10,7 +10,7 @@ import { combineLatest, Observable, of as observableOf } from 'rxjs';
1010
import { map, mergeMap, take } from 'rxjs/operators';
1111
import { NativeWindowRef, NativeWindowService } from '../services/window.service';
1212
import { RouteService } from '../services/route.service';
13-
import { DOCUMENT } from '@angular/common';
13+
import { DOCUMENT, isPlatformBrowser } from '@angular/common';
1414
import { LangConfig } from '../../../config/lang-config.interface';
1515

1616
export const LANG_COOKIE = 'dsLanguage';
@@ -41,7 +41,8 @@ export class LocaleService {
4141
protected translate: TranslateService,
4242
protected authService: AuthService,
4343
protected routeService: RouteService,
44-
@Inject(DOCUMENT) protected document: any
44+
@Inject(DOCUMENT) protected document: any,
45+
@Inject(PLATFORM_ID) protected platformId: string,
4546
) {
4647
this.initDefaults();
4748
}
@@ -53,7 +54,9 @@ export class LocaleService {
5354
this.routeService.getQueryParameterValue('lang').subscribe(lang => {
5455
if (lang && this.translate.getLangs().includes(lang)) {
5556
this.setCurrentLanguageCode(lang);
56-
this.routeService.removeQueryParam('lang');
57+
if (isPlatformBrowser(this.platformId)) {
58+
this.routeService.removeQueryParam('lang');
59+
}
5760
}
5861
});
5962
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { LANG_ORIGIN, LocaleService } from './locale.service';
2-
import { Inject, Injectable } from '@angular/core';
2+
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
33
import { combineLatest, Observable, of as observableOf } from 'rxjs';
44
import { map, mergeMap, take } from 'rxjs/operators';
55
import { hasValue, isEmpty, isNotEmpty } from '../../shared/empty.util';
@@ -21,9 +21,10 @@ export class ServerLocaleService extends LocaleService {
2121
protected translate: TranslateService,
2222
protected authService: AuthService,
2323
protected routeService: RouteService,
24-
@Inject(DOCUMENT) protected document: any
24+
@Inject(DOCUMENT) protected document: any,
25+
@Inject(PLATFORM_ID) protected platformId: string,
2526
) {
26-
super(_window, cookie, translate, authService, routeService, document);
27+
super(_window, cookie, translate, authService, routeService, document, platformId);
2728
}
2829

2930
/**

0 commit comments

Comments
 (0)