Harmonize JSON API error messages
This commit is contained in:
+6
-15
@@ -5,29 +5,20 @@ export default class Lang {
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
get(key = '', params = []) {
|
||||
if(key === '') return '';
|
||||
get(sLangId = '', params = []) {
|
||||
if(sLangId === '') return '';
|
||||
|
||||
const normalizedParams = Array.isArray(params) ? params : [params];
|
||||
|
||||
if(Object.prototype.hasOwnProperty.call(this.translations, key)) {
|
||||
let text = this.translations[key];
|
||||
if(Object.prototype.hasOwnProperty.call(this.translations, sLangId)) {
|
||||
let text = this.translations[sLangId];
|
||||
normalizedParams.forEach((param, index) => {
|
||||
text = text.replace('$' + index, param);
|
||||
});
|
||||
return text;
|
||||
}
|
||||
|
||||
console.warn('Missing translation:', key);
|
||||
return key;
|
||||
}
|
||||
|
||||
parse(message = '') {
|
||||
let sLangKey = this.getLangKey(message);
|
||||
return (sLangKey != '')?this.get(sLangKey):message;
|
||||
}
|
||||
|
||||
getLangKey(message = '') {
|
||||
return (this.prefix && typeof message === 'string' && message.startsWith(this.prefix))?message.slice(this.prefix.length):'';
|
||||
console.warn('Missing translation:', sLangId);
|
||||
return sLangId;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user