eslint-plugin-i18next
ESLint plugin for i18n
Installation
$ npm install eslint-plugin-i18next --save-dev
Usage
Add i18next
to the plugins section of your .eslintrc
configuration file.
{
"plugins": ["i18next"]
}
Then configure the rules you want to use under the rules section.
{
"rules": {
"i18next/no-literal-string": 2
}
}
or
{
"extends": ["plugin:i18next/recommended"]
}
Rule no-literal-string
This rule aims to avoid developers to display literal string to users
in those projects which need to support multi-language.
Note: Disable auto-fix because key in the call i18next.t(key)
ussally was not the same as the literal
Rule Details
It will find out all literal strings and validate them.
Examples of incorrect code for this rule:
const a = 'foo';
Examples of correct code for this rule:
var FOO = 'foo';
var a = {
BAR: 'bar',
[FOO]: 'foo'
};
var foo = 'FOO';
i18n
This rule allows to call i18next translate function.
Correct code:
var bar = i18next.t('bar');
var bar2 = i18n.t('bar');
Maybe you use other internationalization libraries
not i18next. You can use like this:
const bar = yourI18n('bar');
const bar = yourI18n.method('bar');
HTML Markup
All literal strings in html template are typically mistakes. For JSX example:
<div>foo</div>
They should be translated by i18next translation api:
<div>{i18next.t('foo')}</div>
Same for Vue template:
<template>
foo
</template>
<template>
{{ i18next.t('foo') }}
</template>
Redux/Vuex
This rule also works with those state managers like
Redux and Vuex.
Correct code:
var bar = store.dispatch('bar');
var bar2 = store.commit('bar');
Typescript
The following cases are correct:
var a: Type['member'];
var a: Omit<T, 'key'>;
var a: { t: 'button' } = { t: 'button' };
var a: 'abc' | 'name' = 'abc';
We require type information to work properly, so you need to add some options in your .eslintrc
:
"parserOptions": {
"project": "./tsconfig.json"
}
See
here
for more deteils.
Import/Export
The following cases are correct:
import mod from 'm';
import('mod');
require('mod');
export { named } from 'm';
export * from 'm';
Options
ignore
The ignore
option specifies exceptions not to check for
literal strings that match one of regexp paterns.
Examples of correct code for the { "ignore": ['foo'] }
option:
const a = 'afoo';
ignoreCallee
THe ignoreCallee
option speficies exceptions not check for
function calls whose names match one of regexp patterns.
Examples of correct code for the { "ignoreCallee": ["foo"] }
option:
const bar = foo('bar');