ng-jhipster
Version:
A Jhipster util library for Angular 2
54 lines (53 loc) • 2.13 kB
JavaScript
import { Injectable } from '@angular/core';
import { TranslateService } from 'ng2-translate';
import { ConfigHelper } from '../helper';
export var JhiLanguageService = (function () {
function JhiLanguageService(translateService) {
this.translateService = translateService;
this.defaultLocation = 'global';
this.currentLang = 'en';
this.locations = [];
this.init();
}
JhiLanguageService.prototype.init = function () {
var config = ConfigHelper.getConfig();
this.defaultLocation = config.defaultI18nLocation;
this.currentLang = config.defaultI18nLang;
this.translateService.setDefaultLang(this.currentLang);
this.translateService.currentLang = this.currentLang;
};
JhiLanguageService.prototype.changeLanguage = function (languageKey) {
this.currentLang = languageKey;
this.reload();
};
JhiLanguageService.prototype.setLocations = function (locations) {
this.locations = locations;
this.locations.push(this.defaultLocation);
this.reload();
};
JhiLanguageService.prototype.addLocation = function (location) {
if (this.locations.indexOf(location) === -1) {
this.locations.push(location);
this.reload();
}
};
JhiLanguageService.prototype.reload = function () {
this.translateService.setDefaultLang(this.currentLang);
var translatePartialLoader = this.translateService.currentLoader;
translatePartialLoader.setLocations(this.locations);
// reset the language cache //FIXME not ideal as this increases the http requests
this.translateService.resetLang(this.currentLang);
this.translateService.use(this.currentLang);
};
JhiLanguageService.prototype.getCurrent = function () {
return Promise.resolve(this.currentLang);
};
JhiLanguageService.decorators = [
{ type: Injectable },
];
/** @nocollapse */
JhiLanguageService.ctorParameters = function () { return [
{ type: TranslateService, },
]; };
return JhiLanguageService;
}());