[CST-2877] rm browser lang for SSR reqs

This commit is contained in:
Danilo Di Nuzzo
2020-06-30 16:42:58 +02:00
parent d611761548
commit 722fdc0124
4 changed files with 77 additions and 11 deletions

View File

@@ -4,7 +4,7 @@ import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/c
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { LocaleService } from './locale.service'; import { LocaleService } from './locale.service';
import { reduce, mergeMap, scan } from 'rxjs/operators'; import { mergeMap, scan } from 'rxjs/operators';
@Injectable() @Injectable()
export class LocaleInterceptor implements HttpInterceptor { export class LocaleInterceptor implements HttpInterceptor {

View File

@@ -7,7 +7,7 @@ import { CookieService } from '../services/cookie.service';
import { environment } from '../../../environments/environment'; import { environment } from '../../../environments/environment';
import { AuthService } from '../auth/auth.service'; import { AuthService } from '../auth/auth.service';
import { Observable, of as observableOf, combineLatest } from 'rxjs'; import { Observable, of as observableOf, combineLatest } from 'rxjs';
import { map, take, flatMap, tap } from 'rxjs/operators'; import { map, take, flatMap } from 'rxjs/operators';
import { NativeWindowService, NativeWindowRef } from '../services/window.service'; import { NativeWindowService, NativeWindowRef } from '../services/window.service';
export const LANG_COOKIE = 'language_cookie'; export const LANG_COOKIE = 'language_cookie';
@@ -21,11 +21,6 @@ export enum LANG_ORIGIN {
BROWSER BROWSER
}; };
/**
* Eperson language metadata
*/
const EPERSON_LANG_METADATA = 'eperson.language';
/** /**
* Service to provide localization handler * Service to provide localization handler
*/ */
@@ -34,11 +29,16 @@ const EPERSON_LANG_METADATA = 'eperson.language';
}) })
export class LocaleService { export class LocaleService {
/**
* Eperson language metadata
*/
EPERSON_LANG_METADATA = 'eperson.language';
constructor( constructor(
@Inject(NativeWindowService) protected _window: NativeWindowRef, @Inject(NativeWindowService) protected _window: NativeWindowRef,
private cookie: CookieService, protected cookie: CookieService,
private translate: TranslateService, protected translate: TranslateService,
private authService: AuthService) { protected authService: AuthService) {
} }
/** /**
@@ -81,7 +81,7 @@ export class LocaleService {
take(1), take(1),
map((eperson) => { map((eperson) => {
const languages: string[] = []; const languages: string[] = [];
const ePersonLang = eperson.firstMetadataValue(EPERSON_LANG_METADATA); const ePersonLang = eperson.firstMetadataValue(this.EPERSON_LANG_METADATA);
if (ePersonLang) { if (ePersonLang) {
languages.push(...this.setQuality( languages.push(...this.setQuality(
[ePersonLang], [ePersonLang],

View File

@@ -0,0 +1,60 @@
import { LocaleService, LANG_ORIGIN } from './locale.service';
import { Injectable } from '@angular/core';
import { Observable, combineLatest, of as observableOf } from 'rxjs';
import { take, flatMap, map } from 'rxjs/operators';
import { isNotEmpty, isEmpty } from 'src/app/shared/empty.util';
@Injectable()
export class ServerLocaleService extends LocaleService {
/**
* Get the languages list of the user in Accept-Language format
*
* @returns {Observable<string[]>}
*/
getLanguageCodeList(): Observable<string[]> {
const obs$ = combineLatest([
this.authService.isAuthenticated(),
this.authService.isAuthenticationLoaded()
]);
return obs$.pipe(
take(1),
flatMap(([isAuthenticated, isLoaded]) => {
let epersonLang$: Observable<string[]> = observableOf([]);
if (isAuthenticated && isLoaded) {
epersonLang$ = this.authService.getAuthenticatedUserFromStore().pipe(
take(1),
map((eperson) => {
const languages: string[] = [];
const ePersonLang = eperson.firstMetadataValue(this.EPERSON_LANG_METADATA);
if (ePersonLang) {
languages.push(...this.setQuality(
[ePersonLang],
LANG_ORIGIN.EPERSON,
!isEmpty(this.translate.currentLang)));
}
return languages;
})
);
}
return epersonLang$.pipe(
map((epersonLang: string[]) => {
const languages: string[] = [];
if (this.translate.currentLang) {
languages.push(...this.setQuality(
[this.translate.currentLang],
LANG_ORIGIN.UI,
false));
}
if (isNotEmpty(epersonLang)) {
languages.push(...epersonLang);
}
return languages;
})
)
})
);
}
}

View File

@@ -25,6 +25,8 @@ import { ServerSubmissionService } from '../../app/submission/server-submission.
import { Angulartics2DSpace } from '../../app/statistics/angulartics/dspace-provider'; import { Angulartics2DSpace } from '../../app/statistics/angulartics/dspace-provider';
import { Angulartics2RouterlessModule } from 'angulartics2/routerlessmodule'; import { Angulartics2RouterlessModule } from 'angulartics2/routerlessmodule';
import { ModuleMapLoaderModule } from '@nguniversal/module-map-ngfactory-loader'; import { ModuleMapLoaderModule } from '@nguniversal/module-map-ngfactory-loader';
import { ServerLocaleService } from 'src/app/core/locale/server-locale.service';
import { LocaleService } from 'src/app/core/locale/locale.service';
export function createTranslateLoader() { export function createTranslateLoader() {
return new TranslateJson5UniversalLoader('dist/server/assets/i18n/', '.json5'); return new TranslateJson5UniversalLoader('dist/server/assets/i18n/', '.json5');
@@ -73,6 +75,10 @@ export function createTranslateLoader() {
{ {
provide: SubmissionService, provide: SubmissionService,
useClass: ServerSubmissionService useClass: ServerSubmissionService
},
{
provide: LocaleService,
useClass: ServerLocaleService
} }
] ]
}) })