Newer
Older
francois.grand
committed
import { Injectable } from '@angular/core';
import { ErrorMessage, ErrorCode } from "jalhyd";
import { HttpService } from "../http/http.service";
francois.grand
committed
/*
language tag : fr-FR
primary subcode : fr
optional subcode : FR
*/
francois.grand
committed
export enum LanguageCode {
francois.grand
committed
FRENCH,
ENGLISH,
}
francois.grand
committed
export class Language {
private _code: LanguageCode;
private _tag: string;
private _label: string;
constructor(c: LanguageCode, t: string, l: string) {
this._code = c;
this._tag = t;
this._label = l;
}
get code(): LanguageCode {
return this._code;
}
get tag(): string {
return this._tag;
}
get label(): string {
return this._label;
}
}
francois.grand
committed
@Injectable()
export class InternationalisationService {
francois.grand
committed
private _currLang: Language;
francois.grand
committed
private _sLang: string;
private _errorMessages: { [key: string]: string };
francois.grand
committed
private _languages: Language[];
/**
* indique que la langue a été changée
*/
private _localeChanged: boolean = false;
francois.grand
committed
francois.grand
committed
public constructor(private httpService: HttpService) {
this._languages = [];
this._languages.push(new Language(LanguageCode.FRENCH, "fr", "Français"));
this._languages.push(new Language(LanguageCode.ENGLISH, "en", "English"));
francois.grand
committed
}
francois.grand
committed
public get languages() {
return this._languages;
}
francois.grand
committed
francois.grand
committed
public get currentLanguage() {
return this._currLang;
}
francois.grand
committed
francois.grand
committed
private getLanguageFromCode(lc: LanguageCode) {
for (let l of this._languages) {
if (l.code == lc)
return l;
}
throw new ErrorMessage(ErrorCode.ERROR_LANG_UNSUPPORTED);
francois.grand
committed
}
francois.grand
committed
private getLanguageFromTag(tag: string) {
for (let l of this._languages) {
if (l.tag === tag)
return l;
}
let e = new ErrorMessage(ErrorCode.ERROR_LANG_UNSUPPORTED);
e.extraVar["locale"] = tag;
throw e;
}
francois.grand
committed
public setLocale(lng: string | LanguageCode) {
if (this._currLang != undefined)
var oldLang: LanguageCode = this._currLang.code;
francois.grand
committed
if (typeof lng === "string") {
let t: string = lng.substr(0, 2).toLowerCase();
this._currLang = this.getLanguageFromTag(t);
}
else {
this._currLang = this.getLanguageFromCode(lng);
francois.grand
committed
this._localeChanged = this._currLang.code != oldLang;
// this.loadErrorMessages();
this.httpGetErrorMessages();
francois.grand
committed
// private loadErrorMessages() {
// let l;
// switch (this.lang) {
// case Language.FRENCH:
// l = "fr";
// break;
// default:
// l = "en";
// }
// let s: string = fs.readFileSync("src/error_messages." + l + ".json", "utf8");
// this._errorMessages = JSON.parse(s);
// }
private httpGetErrorMessages() {
let is: InternationalisationService = this;
let processData = function (s: string) {
// fermeture nécessaire pour capturer la valeur de this (undefined sinon)
is._errorMessages = JSON.parse(s);
}
let l;
francois.grand
committed
switch (this._currLang.code) {
case LanguageCode.FRENCH:
francois.grand
committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
l = "fr";
break;
default:
l = "en";
}
let f: string = "error_messages." + l + ".json"
this.httpService.httpGetRequest(undefined, undefined, undefined, "locale/" + f, processData);
}
private getErrorMessageFromCode(c: ErrorCode): string {
let sCode: string = ErrorCode[c];
return this._errorMessages[ErrorCode[c]];
}
private replaceAll(str: string, find: string, replace: string) {
return str.replace(new RegExp(find, 'g'), replace);
}
public localizeErrorMessage(r: ErrorMessage): string {
let sCode: string = ErrorCode[r.code];
let m: string = this.getErrorMessageFromCode(r.code);
for (let k in r.extraVar) {
m = this.replaceAll(m, "%" + k + "%", r.extraVar[k]);
}
return m;
}
francois.grand
committed
get localeChanged() {
return this._localeChanged;
}
francois.grand
committed
francois.grand
committed
public acknowledgeLocaleChanged() {
this._localeChanged = false;