This rule aims to avoid developers to display literal string directly to users without translating them.
Example of incorrect code:
/*eslint i18next/no-literal-string: "error"*/
<div>hello world</div>Example of correct code:
/*eslint i18next/no-literal-string: "error"*/
<div>{i18next.t('HELLO_KEY')}</div>The option's typing definition looks like:
type MySchema = {
[key in
| 'words'
| 'jsx-components'
| 'jsx-attributes'
| 'callees'
| 'object-properties'
| 'class-properties']?: {
include?: string[];
exclude?: string[];
};
} & {
framework: 'react' | 'vue';
mode?: 'jsx-text-only' | 'jsx-only' | 'all' | 'vue-template-only';
message?: string;
'should-validate-template'?: boolean;
};Instead of expanding options immoderately, a standard and scalable way to set options is provided
You can use exclude and include of each options to control which should be validated and which should be ignored.
The values of these two fields are treated as regular expressions.
- If both are used, both conditions need to be satisfied
- If both are emitted, it will be validated
words decides whether literal strings are allowed (in any situation), solely based on the content of the string
e.g. if .*foo.* is excluded, the following literals are allowed no matter where they are used
method('afoo');
const message = 'foob';
<Component value="foo" />;-
jsx-componentsdecides whether literal strings as children within a component are allowed, based on the component namee.g. by default,
Transis excluded, soHello Worldin the following is allowed.<Trans i18nKey="greeting">Hello World</Trans>
-
jsx-attributesdecides whether literal strings are allowed as JSX attribute values, based on the name of the attributee.g. if
data-testidis excluded,important-buttonin the following is allowed<button data-testid="important-button" onClick={handleClick}> {t('importantButton.label')} </button>
-
calleesdecides whether literal strings are allowed as function arguments, based on the identifier of the function being callede.g. if
window.openis excluded,http://example.comin the following is allowedwindow.open('http://example.com');
calleesalso covers object constructors, such asnew Error('string')ornew URL('string') -
object-propertiesdecides whether literal strings are allowed as object property values, based on the property keye.g. if
fieldNameis excluded butlabelis not,currency_codeis allowed butCurrencyis not:const fieldConfig = { fieldName: 'currency_code', label: 'Currency', };
-
class-propertiesdecides whether literal strings are allowed as class property values, based on the property keye.g. by default,
displayNameis excluded, soMyComponentis allowedclass My extends Component { displayName = 'MyComponent'; }
frameworkspecifies the type of framework currently in use.reactIt defaults to 'react' which means you want to validate react componentvueIf you want to validate vue component, can set the value to be this
modeprovides a straightforward way to decides the range you want to validate literal strings. It defaults tojsx-text-onlywhich only forbids to write plain text in JSX markup,available when framework option is 'react'jsx-onlyvalidates the JSX attributes as well,available when framework option is 'react'allvalidates all literal strings,available when the value of the framework option is 'react' and 'vue'vue-template-only, only validate vue component template part,available when framework option value is 'vue'.
messagedefines the custom error messageshould-validate-templatedecides if we should validate the string templates
You can see the default options here
Your project maybe not need to support multi-language or you don't care to spread literal string anywhere.