### Install ESLint Plugin i18next Source: https://context7.com/edvardchen/eslint-plugin-i18next/llms.txt Install the plugin as a dev dependency using npm or pnpm. ```bash npm install eslint-plugin-i18next --save-dev # or pnpm add -D eslint-plugin-i18next ``` -------------------------------- ### Install eslint-plugin-i18next Source: https://github.com/edvardchen/eslint-plugin-i18next/blob/main/README.md Install the plugin as a development dependency using npm. ```bash npm install eslint-plugin-i18next --save-dev ``` -------------------------------- ### Exclude/Include Regex Examples Source: https://github.com/edvardchen/eslint-plugin-i18next/blob/main/docs/rules/no-literal-string.md Demonstrates how to use regular expressions with `exclude` and `include` options to control which literal strings are validated. Both conditions must be met if both are specified. ```js method('afoo'); const message = 'foob'; ; ``` -------------------------------- ### Callee Exclusion Example Source: https://github.com/edvardchen/eslint-plugin-i18next/blob/main/docs/rules/no-literal-string.md Demonstrates the `callees` option, where excluding specific function calls like `window.open` permits literal strings as arguments for those functions. ```js window.open('http://example.com'); ``` -------------------------------- ### Example of 'words.exclude' Functionality Source: https://context7.com/edvardchen/eslint-plugin-i18next/llms.txt Strings matching patterns in `words.exclude`, such as URLs, are allowed even when `mode: 'all'` is active. ```jsx /*eslint i18next/no-literal-string: ["error", { "mode": "all", "words": { "exclude": ["^https?://"] } }]*/ // ✅ OK — matches the URL exclusion pattern window.open('https://example.com'); // ❌ Error — does not match any exclusion const msg = 'Welcome back'; ``` -------------------------------- ### Use i18next.t for Translations Source: https://github.com/edvardchen/eslint-plugin-i18next/blob/main/README.md This example shows the correct usage where a translated string key is used with i18next.t(). The 'no-literal-string' rule allows this. ```javascript /*eslint i18next/no-literal-string: "error"*/
{i18next.t('HELLO_KEY')}
``` -------------------------------- ### Example: Validating Template Literals Source: https://context7.com/edvardchen/eslint-plugin-i18next/llms.txt Shows how enabling `'should-validate-template'` flags literal strings within template literals, encouraging translation usage instead. ```jsx /*eslint i18next/no-literal-string: ["error", { "mode": "all", "should-validate-template": true }]*/ // ❌ Error — static text in template literal is flagged const msg = `Welcome back, ${username}!`; // ✅ OK — use translation with interpolation const msg = t('welcomeBack', { name: username }); ``` -------------------------------- ### Class Property Exclusion Example Source: https://github.com/edvardchen/eslint-plugin-i18next/blob/main/docs/rules/no-literal-string.md Illustrates the `class-properties` option, where excluding properties like `displayName` by default allows literal strings for those properties. ```js class My extends Component { displayName = 'MyComponent'; } ``` -------------------------------- ### Example: `callees.exclude` in Action Source: https://context7.com/edvardchen/eslint-plugin-i18next/llms.txt Demonstrates how `callees.exclude` prevents linting errors for excluded function calls like `new Error()` and `console.error()`. ```jsx /*eslint i18next/no-literal-string: ["error", { "mode": "all", "callees": { "exclude": ["Error", "console\..*"] } }]*/ // ✅ OK — Error constructor is excluded throw new Error('Something went wrong internally'); // ✅ OK — console is excluded console.error('debug: unexpected state'); // ❌ Error — alert() is not excluded alert('Please fill in the form'); ``` -------------------------------- ### Example of 'mode: all' Validation Source: https://context7.com/edvardchen/eslint-plugin-i18next/llms.txt When `mode: 'all'` is enabled, strings in both JS expressions and JSX attributes are flagged unless translated. ```jsx /*eslint i18next/no-literal-string: ["error", { "mode": "all" }]*/ // ❌ Error — string in JS expression const greeting = 'Hello'; // ❌ Error — string in JSX attribute const El = () => ; // ✅ OK — translated const El2 = () => ( ); ``` -------------------------------- ### Avoid Literal Strings in JSX Source: https://github.com/edvardchen/eslint-plugin-i18next/blob/main/README.md This example shows incorrect usage where a literal string is displayed directly. The 'no-literal-string' rule flags this. ```javascript /*eslint i18next/no-literal-string: "error"*/
hello world
``` -------------------------------- ### Object Property Exclusion Example Source: https://github.com/edvardchen/eslint-plugin-i18next/blob/main/docs/rules/no-literal-string.md Explains the `object-properties` option, showing how excluding certain property keys (e.g., `fieldName`) allows literal strings for those keys while validating others (e.g., `label`). ```js const fieldConfig = { fieldName: 'currency_code', label: 'Currency', }; ``` -------------------------------- ### JSX Component Exclusion Example Source: https://github.com/edvardchen/eslint-plugin-i18next/blob/main/docs/rules/no-literal-string.md Shows how the `jsx-components` option, with `Trans` excluded by default, allows literal strings as children within specific components like `Trans`. ```jsx Hello World ``` -------------------------------- ### JSX Attribute Exclusion Example Source: https://github.com/edvardchen/eslint-plugin-i18next/blob/main/docs/rules/no-literal-string.md Illustrates the `jsx-attributes` option, where excluding attributes like `data-testid` allows literal strings in those specific attributes. ```jsx ``` -------------------------------- ### Disallow Literal String in JSX Source: https://github.com/edvardchen/eslint-plugin-i18next/blob/main/docs/rules/no-literal-string.md This example shows how to configure the rule to disallow literal strings directly within JSX elements. Use this to ensure all user-facing text is translatable. ```jsx /*eslint i18next/no-literal-string: "error"*/
hello world
``` ```jsx /*eslint i18next/no-literal-string: "error"*/
{i18next.t('HELLO_KEY')}
``` -------------------------------- ### Configure ESLint 8 and Below (Legacy .eslintrc) Source: https://context7.com/edvardchen/eslint-plugin-i18next/llms.txt Use the `plugin:i18next/recommended` shareable config for older ESLint versions. ```json // .eslintrc { "extends": ["plugin:i18next/recommended"], "rules": { "i18next/no-literal-string": ["error", { "mode": "all" }] } } ``` -------------------------------- ### Configure ESLint 9 Flat Configuration Source: https://github.com/edvardchen/eslint-plugin-i18next/blob/main/README.md Configure ESLint using the flat configuration system by importing the plugin and extending its recommended configuration. ```javascript // eslint.config.mjs import i18next from 'eslint-plugin-i18next'; export default [ // your other configs i18next.configs['flat/recommended'], ]; ``` -------------------------------- ### Configure ESLint 8 and Below Source: https://github.com/edvardchen/eslint-plugin-i18next/blob/main/README.md Configure ESLint for versions 8 and below by extending the plugin's recommended configuration in your .eslintrc file. ```json // .eslintrc { "extends": ["plugin:i18next/recommended"] } ``` -------------------------------- ### Configure Vue Framework Support Source: https://context7.com/edvardchen/eslint-plugin-i18next/llms.txt Enable Vue framework support by setting `framework: 'vue'` and using `vue-eslint-parser`. The `vue-template-only` mode restricts linting to template content. ```javascript // eslint.config.mjs import vueParser from 'vue-eslint-parser'; import i18next from 'eslint-plugin-i18next'; export default [ { files: ['**/*.vue'], languageOptions: { parser: vueParser }, plugins: { i18next: i18next }, rules: { 'i18next/no-literal-string': [ 'error', { framework: 'vue', mode: 'vue-template-only', // only lint inside