Fix: i18n error message

This commit is contained in:
eight 2017-09-12 01:48:10 +08:00
parent f305719db3
commit 381ee88e94
2 changed files with 39 additions and 5 deletions

View File

@ -673,6 +673,37 @@
}
}
},
"styleMetaErrorCheckbox": {
"message": "Invalid @var checkbox: value must be 0 or 1",
"description": "Error displayed when the value of @var checkbox is invalid"
},
"styleMetaErrorColor": {
"message": "$COLOR$ is not a valid color",
"description": "Error displayed when the value of @var color is invalid",
"placeholders": {
"color": {
"content": "$1"
}
}
},
"styleMetaErrorSelectMissingKey": {
"message": "Invalid @var select: missing key '$KEY$'",
"description": "Error displayed when the value of @var select is not a valid key",
"placeholders": {
"key": {
"content": "$1"
}
}
},
"styleMetaErrorSelect": {
"message": "Invalid @var select: $ERROR$",
"description": "Error displayed when the value of @var select is invalid",
"placeholders": {
"error": {
"content": "$1"
}
}
},
"styleMissingMeta": {
"message": "Missing medata @$KEY$",
"description": "Error displayed when a mandatory metadata is missing",

View File

@ -59,7 +59,7 @@ var usercss = (function () {
function parse(color) {
el.style.color = color;
if (el.style.color === '') {
throw new Error(`"${color}" is not a valid color`);
throw new Error(chrome.i18n.getMessage('styleMetaErrorColor', color));
}
color = getComputedStyle(el).color;
el.style.color = '';
@ -158,7 +158,11 @@ var usercss = (function () {
// select type has an additional field
if (result.type === 'select') {
const match = matchString(source);
result.select = JSON.parse(match.follow);
try {
result.select = JSON.parse(match.follow);
} catch (err) {
throw new Error(chrome.i18n.getMessage('styleMetaErrorSelect', err.message));
}
source = match.value;
}
@ -266,11 +270,10 @@ var usercss = (function () {
}
function validVar(va, value = 'default') {
// FIXME: i18n
if (va.type === 'select' && !va.select[va[value]]) {
throw new Error(`Invalid @var select: missing key '${va[value]}'`);
throw new Error(chrome.i18n.getMessage('styleMetaErrorSelectMissingKey', va[value]));
} else if (va.type === 'checkbox' && !/^[01]$/.test(va[value])) {
throw new Error('Invalid @var checkbox: value must be 0 or 1');
throw new Error(chrome.i18n.getMessage('styleMetaErrorCheckbox'));
} else if (va.type === 'color') {
va[value] = colorParser.format(colorParser.parse(va[value]));
}