Added LocaleService as localization handler

This commit is contained in:
Giuseppe Digilio
2020-04-20 20:06:50 +02:00
parent b7a57035c4
commit 145b26d262
6 changed files with 254 additions and 91 deletions

View File

@@ -1,33 +1,36 @@
import { delay, filter, map, take } from 'rxjs/operators';
import { AfterViewInit, ChangeDetectionStrategy, Component, HostListener, Inject, OnInit, ViewEncapsulation } from '@angular/core';
import {
AfterViewInit,
ChangeDetectionStrategy,
Component,
HostListener,
Inject,
OnInit,
ViewEncapsulation
} from '@angular/core';
import { NavigationCancel, NavigationEnd, NavigationStart, Router } from '@angular/router';
import { BehaviorSubject, combineLatest as combineLatestObservable, Observable, of } from 'rxjs';
import { select, Store } from '@ngrx/store';
import { TranslateService } from '@ngx-translate/core';
import { Angulartics2GoogleAnalytics } from 'angulartics2/ga';
import { GLOBAL_CONFIG, GlobalConfig } from '../config';
import { MetadataService } from './core/metadata/metadata.service';
import { HostWindowResizeAction } from './shared/host-window.actions';
import { HostWindowState } from './shared/search/host-window.reducer';
import { NativeWindowRef, NativeWindowService } from './core/services/window.service';
import { isAuthenticated } from './core/auth/selectors';
import { AuthService } from './core/auth/auth.service';
import { Angulartics2GoogleAnalytics } from 'angulartics2/ga';
import variables from '../styles/_exposed_variables.scss';
import { CSSVariableService } from './shared/sass-helper/sass-helper.service';
import { MenuService } from './shared/menu/menu.service';
import { MenuID } from './shared/menu/initial-menus-state';
import { BehaviorSubject, combineLatest as combineLatestObservable, Observable, of } from 'rxjs';
import { slideSidebarPadding } from './shared/animations/slide';
import { HostWindowService } from './shared/host-window.service';
import { Theme } from '../config/theme.inferface';
import { isNotEmpty } from './shared/empty.util';
import { CookieService } from './core/services/cookie.service';
import { Angulartics2DSpace } from './statistics/angulartics/dspace-provider';
export const LANG_COOKIE = 'language_cookie';
import { LocaleService } from './core/locale/locale.service';
@Component({
selector: 'ds-app',
@@ -58,29 +61,16 @@ export class AppComponent implements OnInit, AfterViewInit {
private cssService: CSSVariableService,
private menuService: MenuService,
private windowService: HostWindowService,
private cookie: CookieService
private localeService: LocaleService
) {
// Load all the languages that are defined as active from the config file
translate.addLangs(config.languages.filter((LangConfig) => LangConfig.active === true).map((a) => a.code));
// Load the default language from the config file
translate.setDefaultLang(config.defaultLanguage);
// translate.setDefaultLang(config.defaultLanguage);
// Attempt to get the language from a cookie
const lang = cookie.get(LANG_COOKIE);
if (isNotEmpty(lang)) {
// Cookie found
// Use the language from the cookie
translate.use(lang);
} else {
// Cookie not found
// Attempt to get the browser language from the user
if (translate.getLangs().includes(translate.getBrowserLang())) {
translate.use(translate.getBrowserLang());
} else {
translate.use(config.defaultLanguage);
}
}
// set the current language code
this.localeService.setCurrentLanguageCode();
angulartics2GoogleAnalytics.startTracking();
angulartics2DSpace.startTracking();
@@ -130,15 +120,15 @@ export class AppComponent implements OnInit, AfterViewInit {
// More information on this bug-fix: https://blog.angular-university.io/angular-debugging/
delay(0)
).subscribe((event) => {
if (event instanceof NavigationStart) {
this.isLoading$.next(true);
} else if (
event instanceof NavigationEnd ||
event instanceof NavigationCancel
) {
this.isLoading$.next(false);
}
});
if (event instanceof NavigationStart) {
this.isLoading$.next(true);
} else if (
event instanceof NavigationEnd ||
event instanceof NavigationCancel
) {
this.isLoading$.next(false);
}
});
}
@HostListener('window:resize', ['$event'])