Commit 1c7a8b6f authored by Grand Francois's avatar Grand Francois
Browse files

sortie des messages d'erreur multilingues dans un fichier (src/error_messages.xx.json)

Showing with 112 additions and 10 deletions
+112 -10
......@@ -10,6 +10,7 @@
"dependencies": {},
"devDependencies": {
"@types/jasmine": "^2.5.47",
"@types/node": "^6.0.46",
"jasmine": "^2.6.0",
"jasmine-core": "^2.5.2",
"jasmine-node": "^1.14.5",
......@@ -21,8 +22,7 @@
"karma-typescript-preprocessor": "^0.3.0",
"requirejs": "^2.3.2",
"tslint": "^3.15.1",
"typescript": "^2.0.3",
"typings": "^2.1.1"
"typescript": "^2.0.3"
},
"scripts": {
"build": "./node_modules/typescript/bin/tsc --p src/tsconfig.app.json",
......
......@@ -2,11 +2,10 @@
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../build",
"types": []
// "types": [
"types": [
// "jasmine",
// "node"
// ]
"node" // pour pouvoir utiliser node dans le code
]
},
// "files": [
// "test.ts"
......
......@@ -424,8 +424,8 @@ export class Dichotomie extends Debug {
res = new ErrorMessage(ErrorCode.ERROR_DICHO_INIT_DOMAIN);
res.extraVar["targetSymbol"] = aps; // symbole de la variable calculée par la fonction
res.extraVar["targetValue"] = rTarget; // valeur cible pour la fonction
res.extraVar["variableInterval"] = intMax.toString(); // valeur de la fonction pour la valeur initiale de la variable
res.extraVar["variableSymbol"] = this.paramX.symbol; // symbole de la variable de la fonction
res.extraVar["variableInterval"] = intMax.toString(); // intervalle de valeurs pour la variable d'entrée de la fonction
res.extraVar["variableSymbol"] = this.paramX.symbol; // symbole de la variable d'entrée de la fonction
}
else {
if (intSearch.step < 0)
......
{
"ERROR_DICHO_INIT_DOMAIN": "Dichotomy : target %targetSymbol%=%targetValue% does not exist for variable %variableSymbol% valued in interval %variableInterval%",
"ERROR_DICHO_INITVALUE_LOW": "Dichotomy : initial value %variableSymbol%=%variableInitValue% is too low (target is %targetSymbol%=%targetValue%, %targetSymbol%(%variableSymbol%=%variableInitValue%)=%initTarget%)",
"ERROR_DICHO_INITVALUE_HIGH": "Dichotomy : initial value %variableSymbol%=%variableInitValue% is too high (target is %targetSymbol%=%targetValue%, %targetSymbol%(%variableSymbol%=%variableInitValue%)=%initTarget%)",
"ERROR_DICHO_NULL_STEP": "Dichotomy (initial interval search) : invalid null step",
"ERROR_DICHO_INVALID_STEP_GROWTH": "Dichotomy (initial interval search) : invalid null step growth",
"ERROR_DICHO_FUNCTION_VARIATION": "unable to determinate function direction of variation",
"ERROR_PARAMDOMAIN_INTERVAL_BOUNDS": "invalid %minValue%/%maxValue% min/max boundaries for 'interval' parameter definition domain",
"ERROR_PARAMDEF_CALC_UNDEFINED": "calculability of '%symbol%' parameter is undefined",
"ERROR_PARAMDEF_VALUE_UNDEFINED": "value of '%symbol%' parameter is undefined",
"ERROR_PARAMDEF_VALUE_FIXED": "value of '%symbol%' parameter cannot be changed",
"ERROR_PARAMDEF_VALUE_POS": "value %value% of '%symbol%' parameter is invalid (cannot be <=0)",
"ERROR_PARAMDEF_VALUE_POSNULL": "value %value% of '%symbol%' parameter is invalid (cannot be <0)",
"ERROR_PARAMDEF_VALUE_NULL": "value of '%symbol%' parameter cannot be 0",
"ERROR_PARAMDEF_VALUE_INTERVAL": "parameter '%symbol%' : value %value% is out of [%minValue%, %maxValue%] interval",
"ERROR_PARAMDOMAIN_INVALID": "parameter '%symbol%' : non supported '%domain%' definition domain",
"ERROR_INTERVAL_UNDEF": "Interval : invalid 'undefined' value",
"ERROR_INTERVAL_OUTSIDE": "Interval : value %value% is outside of %interval",
"ERROR_LANG_UNSUPPORTED": "internationalisation : unsupported '%locale%' locale"
}
\ No newline at end of file
{
"ERROR_DICHO_INIT_DOMAIN": "Dichotomie : la valeur cible %targetSymbol%=%targetValue% n'existe pas pour la variable %variableSymbol% prise dans l'intervalle %variableInterval%",
"ERROR_DICHO_INITVALUE_LOW": "Dichotomie : la valeur initiale %variableSymbol%=%variableInitValue% est trop petite (la valeur cible est %targetSymbol%=%targetValue%, %targetSymbol%(%variableSymbol%=%variableInitValue%)=%initTarget%)",
"ERROR_DICHO_INITVALUE_HIGH": "Dichotomie : la valeur initiale %variableSymbol%=%variableInitValue% est trop grande (la valeur cible est %targetSymbol%=%targetValue%, %targetSymbol%(%variableSymbol%=%variableInitValue%)=%initTarget%)",
"ERROR_DICHO_NULL_STEP": "Dichotomie : le pas pour la recherche de l'intervalle de départ ne devrait pas être nul",
"ERROR_DICHO_INVALID_STEP_GROWTH": "Dichotomie : l'augmentation du pas pour la recherche de l'intervalle de départ est incorrecte (=0)",
"ERROR_DICHO_FUNCTION_VARIATION": "Dichotomie : impossible de determiner le sens de variation de la fonction",
"ERROR_PARAMDOMAIN_INTERVAL_BOUNDS": "Les bornes (%minValue%/%maxValue%) de l'intervalle sont incorrectes",
"ERROR_PARAMDEF_CALC_UNDEFINED": "La calculabilité du paramètre %symbol% n'est pas définie",
"ERROR_PARAMDEF_VALUE_UNDEFINED": "La valeur du paramètre %symbol% n'est pas définie",
"ERROR_PARAMDEF_VALUE_FIXED": "La valeur du paramètre %symbol% ne peut pas être changée",
"ERROR_PARAMDEF_VALUE_POS": "La valeur %value% du paramètre '%symbol%' est incorrecte (<=0)",
"ERROR_PARAMDEF_VALUE_POSNULL": "La valeur %value% du paramètre '%symbol%' est incorrecte (<0)",
"ERROR_PARAMDEF_VALUE_NULL": "La valeur du paramètre '%symbol%' ne peut pas être nulle",
"ERROR_PARAMDEF_VALUE_INTERVAL": "Paramètre '%symbol%' : la valeur %value% est en dehors de l'intervalle [%minValue%, %maxValue%]",
"ERROR_PARAMDOMAIN_INVALID": "Paramètre '%symbol%' : le domaine de définition '%domain%' est incorrect",
"ERROR_INTERVAL_UNDEF": "Interval : valeur 'undefined' incorrecte",
"ERROR_INTERVAL_OUTSIDE": "Interval : la valeur %value% est hors de l'intervalle %interval",
"ERROR_LANG_UNSUPPORTED": "internationalisation : locale '%locale%' non prise en charge"
}
\ No newline at end of file
......@@ -7,3 +7,5 @@ export * from './lechaptcalmon';
export * from './regime_uniforme';
export * from './util/definedvalue';
export * from './util/numericalstring';
export * from './util/error';
export * from './internationalisation';
// import { Result, ResultCode } from "./base";
import { Result } from "./base";
import { ErrorMessage, ErrorCode } from "./util/error";
import * as fs from 'fs';
export enum Language {
FRENCH,
......@@ -12,6 +13,7 @@ export class Internationalisation {
private _lang: Language;
private _sLang: string;
private static _instance: Internationalisation;
private _errorMessages: { [key: string]: string };
private constructor() { }
......@@ -30,6 +32,57 @@ export class Internationalisation {
this._sLang = Language[l];
}
public setLocale(loc: string) {
let l: string = loc.substr(0, 2).toLowerCase();
if (l === "en")
this.lang = Language.ENGLISH;
else if (l === "fr")
this.lang = Language.FRENCH;
else {
let e = new ErrorMessage(ErrorCode.ERROR_LANG_UNSUPPORTED);
e.extraVar["locale"] = loc;
throw e;
}
this.loadErrorMessages();
}
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 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;
}
/*
public localizeErrorMessage(r: ErrorMessage): string {
let m: string;
let sCode: string = ErrorCode[r.code];
......@@ -287,4 +340,5 @@ export class Internationalisation {
return m;
}
*/
}
{
"extends": "../tsconfig.json",
"compilerOptions": {
"types": [],
"types": [
"node" // pour pouvoir utiliser node dans le code
],
"removeComments": false,
"declaration": true // génère le fichier .d.ts à partir de index.ts
},
......
......@@ -92,6 +92,11 @@ export enum ErrorCode {
* la valeur passée à une méthode de la classe Interval est hors de l'intervalle défini
*/
ERROR_INTERVAL_OUTSIDE = -301,
/**
* internationalisation : langue non prise en charge
*/
ERROR_LANG_UNSUPPORTED = -400,
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment