Set of eslint rules for gettext API
npm install eslint-plugin-gettextGettext linting rules for ESLint.
We're using this rule to lint against misuse of the jed gettext integration and the django JS catalog; so we're confident it should work against any gettext integration.
1. Install ESLint.
1. Install eslint-plugin-gettext plugin.
``sh`
npm install eslint --save-dev
npm install eslint-plugin-gettext --save-dev
1. Load plugin.
1. Enable rules.
`json`
{
"plugins": ["gettext"],
"rules": {
"gettext/no-variable-string": "error",
"gettext/required-positional-markers-for-multiple-variables": "error"
}
}
Disallow non literal strings inside common gettext functions. This is a very common mistake that disallow translation system from statically collecting the translatable strings.
`js
// Disallows any non string literals in string reserved fields:
gettext(variable)
gettext(123)
ngettext(varA, varB, 5)
pgettext(varA, varB)
npgettext(varA, varB, varC, 5)
// Allows:
gettext('hello')
ngettext('cat', '%d cats', 5)
pgettext('homepage', 'hello')
npgettext('homepage', 'cat', '%d cats', 5)
i18n.gettext('hello') // any object can expose the gettext API
this.gettext('hello')
`
Require that all strings containing multiple variables also includes positional marker. This allows translator to reorder variables, and prevents sprintf() errors if someone change the order of %s and %d.
`js
// Disallows:
gettext('There is %d more event in the %s.')
gettext('There is %d more event in the %1$s.')
// Allows:
gettext('There is %d more event in the game.')
ngettext('cat %1$s $2$s', '%1$d cats %2$d dogs', count)
``
MIT © App Annie
[npm-image]: https://badge.fury.io/js/eslint-plugin-gettext.svg
[npm-url]: https://npmjs.org/package/eslint-plugin-gettext
[travis-image]: https://travis-ci.org/appannie/eslint-plugin-gettext.svg?branch=master
[travis-url]: https://travis-ci.org/appannie/eslint-plugin-gettext
[daviddm-image]: https://david-dm.org/appannie/eslint-plugin-gettext.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/appannie/eslint-plugin-gettext
[coveralls-image]: https://coveralls.io/repos/appannie/eslint-plugin-gettext/badge.svg
[coveralls-url]: https://coveralls.io/r/appannie/eslint-plugin-gettext