argos/dmp-frontend/src/app/core/services/language/language.service.ts

60 lines
1.9 KiB
TypeScript

import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { environment } from 'environments/environment';
import { Observable } from 'rxjs';
import { HttpResponse, HttpClient } from '@angular/common/http';
import { BaseHttpService } from '../http/base-http.service';
import { Language } from '@app/models/language/Language';
import { ConfigurationService } from '../configuration/configuration.service';
import { BaseHttpParams } from '@common/http/base-http-params';
import { InterceptorType } from '@common/http/interceptors/interceptor-type';
@Injectable()
export class LanguageService {
private currentLanguage: string;
private get apiBase(): string { return `${this.configurationService.server}language`; }
constructor(
private translate: TranslateService,
private http: HttpClient,
private baseHttp: BaseHttpService,
private configurationService: ConfigurationService
) {
this.currentLanguage = this.configurationService.defaultLanguage || 'en';
}
public changeLanguage(lang: string) {
this.currentLanguage = lang;
this.translate.use(lang);
}
public getCurrentLanguage() {
return this.currentLanguage;
}
public getCurrentLanguageJSON(): Observable<HttpResponse<Blob>> {
const params = new BaseHttpParams();
// params.interceptorContext = {
// excludedInterceptors: [
// InterceptorType.AuthToken,
// ]
// };
return this.http.get(`${this.apiBase}/${this.currentLanguage}`, { params: params, responseType: 'blob', observe: 'response' });
}
public updateLanguage(json: string): Observable<String> {
return this.baseHttp.post<string>(`${this.apiBase}/update/${this.currentLanguage}`, json);
}
public getCurrentLanguageName() {
let result: string = '';
this.configurationService.availableLanguages.forEach(language => {
if (language.value === this.currentLanguage) {
result = this.translate.instant(language.label);
}
});
return result;
}
}