Fix pluralization issues in translations
This commit is contained in:
parent
808c0beae7
commit
9e28f4dbe0
5 changed files with 108 additions and 60 deletions
37
build/intl-linter/rules/pluralPound.ts
Normal file
37
build/intl-linter/rules/pluralPound.ts
Normal file
|
@ -0,0 +1,37 @@
|
|||
// Copyright 2022 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import type { PluralElement } from '@formatjs/icu-messageformat-parser';
|
||||
import { rule } from '../utils/rule';
|
||||
|
||||
export default rule('pluralPound', context => {
|
||||
const stack: Array<PluralElement> = [];
|
||||
return {
|
||||
enterPlural(element) {
|
||||
stack.push(element);
|
||||
},
|
||||
exitPlural() {
|
||||
stack.pop();
|
||||
},
|
||||
enterLiteral(element, parent) {
|
||||
// Note: Without the stack this could be turned into a rule to check for
|
||||
// explicit numbers anywhere in the message.
|
||||
if (parent == null) {
|
||||
return;
|
||||
}
|
||||
if (parent !== stack.at(-1)) {
|
||||
return;
|
||||
}
|
||||
// Adapted from https://github.com/TalhaAwan/get-numbers
|
||||
// Checks for explicit whitespace before and after the number.
|
||||
const index = element.value.search(/(^| )(-\d+|\d+)(,\d+)*(\.\d+)*($| )/);
|
||||
if (index > -1) {
|
||||
context.report(
|
||||
'Use # instead of an explicit number',
|
||||
element.location,
|
||||
index
|
||||
);
|
||||
}
|
||||
},
|
||||
};
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue