parent
abd0e08566
commit
277133fa1a
@ -22,7 +22,7 @@ import { AppComponent } from './app.component';
|
||||
import { GfHeaderModule } from './components/header/header.module';
|
||||
import { authInterceptorProviders } from './core/auth.interceptor';
|
||||
import { httpResponseInterceptorProviders } from './core/http-response.interceptor';
|
||||
import { LanguageManager } from './core/language-manager.service';
|
||||
import { LanguageService } from './core/language.service';
|
||||
|
||||
@NgModule({
|
||||
declarations: [AppComponent],
|
||||
@ -46,11 +46,11 @@ import { LanguageManager } from './core/language-manager.service';
|
||||
providers: [
|
||||
authInterceptorProviders,
|
||||
httpResponseInterceptorProviders,
|
||||
LanguageManager,
|
||||
LanguageService,
|
||||
{
|
||||
provide: DateAdapter,
|
||||
useClass: CustomDateAdapter,
|
||||
deps: [LanguageManager, MAT_DATE_LOCALE, Platform]
|
||||
deps: [LanguageService, MAT_DATE_LOCALE, Platform]
|
||||
},
|
||||
{ provide: MAT_DATE_FORMATS, useValue: DateFormats }
|
||||
],
|
||||
|
@ -1,126 +0,0 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import * as deDateFnsLocale from 'date-fns/locale/de/index';
|
||||
import * as frDateFnsLocale from 'date-fns/locale/fr/index';
|
||||
import { BehaviorSubject } from 'rxjs';
|
||||
|
||||
// TODO: Rename to language service
|
||||
|
||||
/**
|
||||
* Service that distributes onLanguageChanged events
|
||||
*/
|
||||
@Injectable()
|
||||
export class LanguageManager {
|
||||
private static readonly AVALABLE_LANGUAGES = ['de', 'fr'];
|
||||
private static readonly LANGUAGE_LABELS = {
|
||||
de: 'Deutsch',
|
||||
fr: 'Français'
|
||||
};
|
||||
private currentLanguage: string;
|
||||
private changeLoadLanguageStateSubject = new BehaviorSubject(false);
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
*/
|
||||
public constructor() {} // private translate: TranslateService // private dataLoaderManager: DataLoaderManager,
|
||||
|
||||
/**
|
||||
* Emits an event that the language has changed
|
||||
*/
|
||||
public changeLanguage(aLanguage: string) {
|
||||
if (aLanguage && aLanguage !== this.currentLanguage) {
|
||||
this.currentLanguage = aLanguage;
|
||||
|
||||
this.changeLoadLanguageStateSubject.next(true);
|
||||
|
||||
// this.translate.use(this.currentLanguage);
|
||||
|
||||
/*this.dataLoaderManager.changeLanguage(this.currentLanguage).then(() => {
|
||||
// Emit an event that loading has finished
|
||||
this.changeLoadLanguageStateSubject.next(false);
|
||||
});*/
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of available languages for admin
|
||||
*/
|
||||
public getAvailableLanguages() {
|
||||
return LanguageManager.AVALABLE_LANGUAGES;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current language
|
||||
*/
|
||||
public getCurrentLanguage(aReturnFullLocale = false) {
|
||||
// Check if the full locale is needed (e.g. for angular pipes like
|
||||
// '| percentage')
|
||||
if (aReturnFullLocale) {
|
||||
if (this.currentLanguage) {
|
||||
if (this.currentLanguage.match(/^de/)) {
|
||||
return 'de-CH';
|
||||
}
|
||||
|
||||
if (this.currentLanguage.match(/^fr/)) {
|
||||
return 'fr-CH';
|
||||
}
|
||||
}
|
||||
|
||||
// Default
|
||||
return 'de-CH';
|
||||
}
|
||||
|
||||
if (this.currentLanguage) {
|
||||
return this.currentLanguage;
|
||||
}
|
||||
|
||||
// Default
|
||||
return 'de';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the locale module of date-fns in the current language
|
||||
*/
|
||||
public getDateFnsLocale() {
|
||||
let currentDateFnsLocale = null;
|
||||
|
||||
switch (this.getCurrentLanguage()) {
|
||||
case 'de':
|
||||
currentDateFnsLocale = deDateFnsLocale;
|
||||
break;
|
||||
case 'fr':
|
||||
currentDateFnsLocale = frDateFnsLocale;
|
||||
break;
|
||||
default:
|
||||
currentDateFnsLocale = deDateFnsLocale;
|
||||
}
|
||||
|
||||
return currentDateFnsLocale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default language
|
||||
*/
|
||||
public getDefaultLanguage() {
|
||||
// return globals.defaultLanguage;
|
||||
return 'de';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a pretty label of the given language
|
||||
*/
|
||||
public getLanguageLabel(aLanguage: string) {
|
||||
if (LanguageManager.LANGUAGE_LABELS[aLanguage]) {
|
||||
return LanguageManager.LANGUAGE_LABELS[aLanguage];
|
||||
}
|
||||
|
||||
return aLanguage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an observable that emits true when loading is in progress and false
|
||||
* when loading is finished
|
||||
*/
|
||||
public onChangeLoadLanguageState() {
|
||||
return this.changeLoadLanguageStateSubject.asObservable();
|
||||
}
|
||||
}
|
6
apps/client/src/app/core/language.service.ts
Normal file
6
apps/client/src/app/core/language.service.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
|
||||
@Injectable()
|
||||
export class LanguageService {
|
||||
public constructor() {}
|
||||
}
|
@ -2,7 +2,6 @@ import { Injectable } from '@angular/core';
|
||||
import { BehaviorSubject } from 'rxjs';
|
||||
|
||||
const TOKEN_KEY = 'auth-token';
|
||||
// const USER_KEY = 'auth-user';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@ -12,10 +11,12 @@ export class TokenStorageService {
|
||||
|
||||
public constructor() {}
|
||||
|
||||
public signOut(): void {
|
||||
window.localStorage.clear();
|
||||
public getToken(): string {
|
||||
return window.localStorage.getItem(TOKEN_KEY);
|
||||
}
|
||||
|
||||
this.hasTokenChangeSubject.next();
|
||||
public onChangeHasToken() {
|
||||
return this.hasTokenChangeSubject.asObservable();
|
||||
}
|
||||
|
||||
public saveToken(token: string): void {
|
||||
@ -25,20 +26,9 @@ export class TokenStorageService {
|
||||
this.hasTokenChangeSubject.next();
|
||||
}
|
||||
|
||||
public getToken(): string {
|
||||
return window.localStorage.getItem(TOKEN_KEY);
|
||||
}
|
||||
public signOut(): void {
|
||||
window.localStorage.clear();
|
||||
|
||||
public onChangeHasToken() {
|
||||
return this.hasTokenChangeSubject.asObservable();
|
||||
this.hasTokenChangeSubject.next();
|
||||
}
|
||||
|
||||
/*public saveUser(user): void {
|
||||
window.localStorage.removeItem(USER_KEY);
|
||||
window.localStorage.setItem(USER_KEY, JSON.stringify(user));
|
||||
}
|
||||
|
||||
public getUser(): any {
|
||||
return JSON.parse(localStorage.getItem(USER_KEY));
|
||||
}*/
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user