eslint-plugin-i18next

repository·main·Indexed 41 days ago

https://github.com/edvardchen/eslint-plugin-i18next

An ESLint plugin version 6.1.5 designed to enforce internationalization (i18n) best practices by preventing the use of untranslated literal strings in code. It features the `no-literal-string` rule, which supports React and Vue frameworks, providing configurable validation modes (jsx-text-only, jsx-only, all, vue-template-only) and extensive filtering options for components, attributes, and function callees.

Tokens
3.2K
Snippets
14
Records
19
Agent score
52%

What's inside eslint-plugin-i18next

  1. Use `include` and `exclude` to filter rule triggers

    main

    For any selector option (like words, callees, etc.), you can use include and exclude to control validation. These fields accept arrays of strings treated as regular expressions.

    Logic Rules:

    1. If both include and exclude are provided, a match must satisfy both conditions to be validated/ignored.
    2. If both are provided, the string will be validated (subject to the regex logic).

    Example: words selector If you add .*foo.* to the exclude list, any string containing 'foo' will be allowed regardless of where it appears:

    method('afoo'); // Allowed
    const message = 'foob'; // Allowed
    <Component value="foo" /> // Allowed
  2. Understand `no-literal-string` validation modes

    main

    The mode option determines the scope of the rule's validation. This is dependent on the framework setting.

    • jsx-text-only (Default for react): Only forbids plain text inside JSX markup (e.g., <div>text</div>).
    • jsx-only (Available for react): Validates both JSX text and JSX attribute values.
    • all (Available for react and vue): Validates all literal strings across the codebase.
    • vue-template-only (Available for vue): Only validates the template part of Vue components.
  3. Enable TypeScript support for no-literal-string

    main

    To prevent the rule from incorrectly flagging TypeScript-specific usages (like type members, enums, or union types), you must provide type information to ESLint by configuring parserOptions.project in your .eslintrc.

    // In your .eslintrc
    "parserOptions": {
      "project": "./tsconfig.json"
    }
  4. Configure eslint-plugin-i18next for ESLint 9 (Flat Config)

    main

    To use the recommended configuration in ESLint 9 using the flat config format, import the plugin and include i18next.configs['flat/recommended'] in your configuration array.

    // eslint.config.mjs
    import i18next from 'eslint-plugin-i18next';
    
    export default [
      // your other configs
      i18next.configs['flat/recommended'],
    ];
  5. Configure eslint-plugin-i18next in ESLint

    main

    To use the plugin, add i18next to your .eslintrc plugins array. You can then either configure specific rules manually or extend the recommended configuration.

    {
      "plugins": ["i18next"],
      "rules": {
        "i18next/no-literal-string": 2
      }
    }

    Or use the recommended preset:

    {
      "extends": ["plugin:i18next/recommended"]
    }
  6. Configure markupOnly for JSX/HTML templates

    main

    When markupOnly is set to true, the rule only validates JSX text content and strings used as JSX attributes. This is useful if you want to ignore literal strings in standard JavaScript logic and only focus on UI templates.

    /*eslint i18next/no-literal-string: ["error", { "markupOnly": true }]*/
    
    // Incorrect (flagged):
    <div>hello world</div>
    <div foo="foo"></div>
  7. Configure ignoreCallee for custom i18n libraries

    main

    If you use an internationalization library other than i18next, you can prevent the rule from flagging its function calls using the ignoreCallee option. This option accepts an array of regexp patterns.

    // Ignore a single function name
    /*eslint i18next/no-literal-string: ["error", { "ignoreCallee": ["yourI18n"] }]*/
    const bar = yourI18n('bar');
    
    // Ignore a specific method on an object
    /*eslint i18next/no-literal-string: ["error", { "ignoreCallee": ["yourI18n.method"] }]*/
    const bar2 = yourI18n.method('bar');
  8. Configure onlyAttribute for specific JSX attributes

    main

    The onlyAttribute option enables markupOnly mode but restricts validation to a specific list of JSX attributes. This is useful when you only want to ensure certain props (like label or placeholder) are translated.

    /*eslint i18next/no-literal-string: ["error", {"onlyAttribute": ["foo"]}]*/
    
    // Incorrect (flagged):
    <div foo="foo"></div>
    
    // Correct (ignored):
    <div other="bar"></div>
  9. Understand `no-literal-string` exclusion logic

    main

    The no-literal-string rule automatically ignores several common coding patterns to reduce false positives:

    • Imports/Exports: Strings used in import, export, or import() statements are ignored.
    • TypeScript Types: String literals used in type definitions (e.g., var a: 'abc') or as members of Enums are ignored.
    • Constants: Variable declarations where the identifier is in UPPER_CASE are ignored (e.g., const API_URL = '...').
    • Logic/Comparisons: String literals used in binary expressions (except for string concatenation +) or within switch case statements are ignored.
    • Object Keys: String literals used as keys in object definitions (e.g., { "foo": 123 }) are ignored.
    • Function Arguments: Strings used as default values in assignment patterns (e.g., function bar(input = 'foo') {}) are ignored.
    • DOM Attributes: Standard DOM attributes are automatically skipped.