npm package: @iroco/eslint-svelte-i18n
Description: ESLint plugin to extract Svelte text into i18n files
Author: Iroco
Latest version: 0.1.4

# @iroco/eslint-svelte-i18n [![status-badge](https://ci.codeberg.org/api/badges/16308/status.svg)](https://ci.codeberg.org/repos/16308)

## svelte-i18n extractor for ESLint

An ESLint plugin that automatically extracts hardcoded text from `.svelte` files and moves it into your default `svelte-i18n` translation file.

---

## Motivation

Internationalizing an existing Svelte project can be tedious when text is already hardcoded in components.

You typically need to:

- Create new translation keys
- Replace hardcoded text with `$_('key')`
- Manually copy the original text into your translation files

This plugin automates that process.

It scans your `.svelte` files, generates translation keys, replaces the text in your components, and updates your translation file automatically.

The goal is simple: **save time and make internationalization painless.**

It can also change your workflow by allowing you to work on your Svelte files without thinking about keys and internationalization and focus on semantics and readability. Then, when your changes are finished, you only have to execute the command line and your changes are ready to be translated.

---

## Installation

```bash
npm install -D eslint @iroco/eslint-svelte-i18n
```

---

## Usage

### ESLint Flat Config (`eslint.config.js`)

This plugin currently provides one rule:

```
extract-i18n
```

Example configuration:

```js
import svelteI18nPlugin from '@iroco/eslint-svelte-i18n';

export default [
	{
		plugins: {
			'svelte-i18n': svelteI18nPlugin
		}
	},
	{
		files: ['**/*.svelte'],
		rules: {
			'svelte-i18n/extract-i18n': 'warn'
		}
	},
	{
		ignores: ['build/', '.svelte-kit/', 'dist/']
	}
];
```

> 💡 It is recommended to use `"warn"` instead of `"error"` so your CI/CD pipelines do not fail unexpectedly.

---

## Rule Options

The rule accepts a single options object with the following properties:

| Option                      | Type       | Default                                                             | Description                                                                |
| --------------------------- | ---------- | ------------------------------------------------------------------- | -------------------------------------------------------------------------- |
| `translationsFile`          | `string`   | `'src/lib/i18n/locales/en.json'`                                    | Path to your default translation file                                      |
| `maxWordsInIntermediaryKey` | `number`   | `5`                                                                 | Maximum number of words used when generating translation keys              |
| `maxCharsInDirectKey`       | `number`   | `50`                                                                | Maximum number of characters allowed before generating an intermediary key |
| `escapedTexts`              | `string[]` | `['']`                                                              | Strings that will not be processed if alone                                |
| `excludedWordsInKey`        | `string[]` | `['and','or','the','a','an','of','in','on','with','to','for','by']` | Words that will not be in generated keys                                   |

### Example with Options

```js
import svelteI18nPlugin from '@iroco/eslint-svelte-i18n';

export default [
	{
		plugins: {
			'svelte-i18n': svelteI18nPlugin
		}
	},
	{
		files: ['**/*.svelte'],
		rules: {
			'svelte-i18n/extract-i18n': [
				'warn',
				{
					translationsFile: './src/lib/i18n/locales/fr.json',
					maxWordsInIntermediaryKey: 6,
					maxCharsInDirectKey: 20,
					escapedTexts: ['Google']
				}
			]
		}
	}
];
```

---

## License

MIT — see the [LICENSE](LICENSE) file for details.