Harmonize JSON API error messages

This commit is contained in:
2026-08-25 23:44:26 +02:00
parent 0d4b159ab3
commit f1b90a4d83
18 changed files with 254 additions and 175 deletions
+6 -15
View File
@@ -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;
}
}