mirror of
https://github.com/scratchfoundation/scratch-l10n.git
synced 2024-12-22 13:42:30 -05:00
Merge pull request #59 from chrisgarrity/fix-error-checking
Fix placeholder checking
This commit is contained in:
commit
3537e36626
1 changed files with 23 additions and 5 deletions
|
@ -9,12 +9,30 @@ const flattenJson = (translations) => {
|
|||
return JSON.stringify(messages, null, 4);
|
||||
};
|
||||
|
||||
// filter placeholders out of a message
|
||||
// parse('a message with a {value} and {count, plural, one {one} other {more}}.')
|
||||
// returns an array:
|
||||
// [ 'a message with a ',
|
||||
// [ 'value' ],
|
||||
// ' and ',
|
||||
// [ 'count', 'plural', 0, { one: [Array], other: [Array] } ],
|
||||
// '.'
|
||||
// ]
|
||||
// placeholders are always an array, so filter for array elements to find the placeholders
|
||||
const placeholders = message => (
|
||||
// this will throw an error if the message is not valid ICU
|
||||
parse(message).filter(item => Array.isArray(item))
|
||||
);
|
||||
|
||||
const validMessage = (message, source) => {
|
||||
// this will throw an error if the message is not valid icu
|
||||
const t = parse(message);
|
||||
const s = parse(source);
|
||||
// the syntax tree for both messages should have the same number of elements
|
||||
return t.length === s.length;
|
||||
const transPlaceholders = placeholders(message);
|
||||
const srcPlaceholders = placeholders(source);
|
||||
// different number of placeholders
|
||||
if (transPlaceholders.length !== srcPlaceholders.length) {
|
||||
return false;
|
||||
}
|
||||
// TODO: Add checking to make sure placeholders in source have not been translated
|
||||
return true;
|
||||
};
|
||||
|
||||
const validateTranslations = (translation, source) => {
|
||||
|
|
Loading…
Reference in a new issue