|
| 1 | +# Unused i18n |
| 2 | + |
| 3 | +Simplifies managing and cleaning up unused translation keys in localization files |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Analyzes source files to identify used and unused translation keys. |
| 8 | +- Supports multiple scoped translation functions. |
| 9 | +- Can display or remove unused translation keys. |
| 10 | +- Configurable through a JSON, CJS, or JS config file. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +You can install `unused-i18n` via npm: |
| 15 | + |
| 16 | +```sh |
| 17 | +npm install -g unused-i18n |
| 18 | + |
| 19 | +or |
| 20 | + |
| 21 | +npm install -D unused-i18n |
| 22 | +``` |
| 23 | + |
| 24 | +## Configuration |
| 25 | + |
| 26 | +Create a unused-i18n.config.json or unused-i18n.config.js file in the root of your project. Here's an example configuration: |
| 27 | + |
| 28 | +```cjs |
| 29 | +module.exports = { |
| 30 | + paths: [ |
| 31 | + { |
| 32 | + srcPath: ['src/pages/products'], |
| 33 | + localPath: 'src/pages/products/locales', |
| 34 | + }, |
| 35 | + ], |
| 36 | + localesExtensions: 'ts', |
| 37 | + localesNames: 'en', |
| 38 | + scopedNames: ['scopedT', 'scopedTOne'], |
| 39 | + ignorePaths: ['src/pages/products/ignoreThisFolder'], |
| 40 | + excludeKey: ['someKey'], |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +| Option | Type | Default | Required | Description | |
| 45 | +| ------------------- | ---------------- | ------- | -------- | ---------------------------------------------------------------------------- | |
| 46 | +| `paths` | Array of Objects | `[]` | Yes | An array of objects defining the source paths and local paths to be checked. | |
| 47 | +| `paths.srcPath` | Array of Strings | `[]` | Yes | Source paths to search for translations. | |
| 48 | +| `paths.localPath` | Strings | `""` | Yes | Path to the translation files. | |
| 49 | +| `localesExtensions` | String | `js` | No | Extension of the locale files. | |
| 50 | +| `localesNames` | String | `en` | No | Name of the locale files without the extension. | |
| 51 | +| `scopedNames` | Array of Strings | `[]` | No | Names of the scoped translation functions used in your project. | |
| 52 | +| `ignorePaths` | Array of Strings | `[]` | No | Paths to be ignored during the search. | |
| 53 | +| `excludeKey` | Array of Strings | `[]` | No | Specific translation keys to be excluded from the removal process. | |
| 54 | + |
| 55 | +## Usage |
| 56 | + |
| 57 | +### Using with Config File |
| 58 | + |
| 59 | +To use unused-i18n with your config file, simply run: |
| 60 | + |
| 61 | +```sh |
| 62 | +npx unused-i18n display |
| 63 | +``` |
| 64 | + |
| 65 | +### Using with Command Line Options |
| 66 | + |
| 67 | +You can also specify the source and local paths directly in the command line: |
| 68 | + |
| 69 | +##### Display Unused Translations |
| 70 | + |
| 71 | +```sh |
| 72 | +npx unused-i18n display --srcPath="src/folders/bla" --localPath="src/folders/bla/locales" |
| 73 | +``` |
| 74 | + |
| 75 | +##### Remove Unused Translations |
| 76 | + |
| 77 | +```sh |
| 78 | +npx unused-i18n remove --srcPath="src/folders/bla" --localPath="src/folders/bla/locales" |
| 79 | +``` |
| 80 | + |
| 81 | +## API |
| 82 | + |
| 83 | +`processTranslations(paths, action)` |
| 84 | +Processes translations based on the specified paths and action. |
| 85 | + |
| 86 | +- paths: Array of objects containing srcPath and localPath. |
| 87 | +- action: Action to perform, either 'display' or 'remove'. |
| 88 | + |
| 89 | +## Development |
| 90 | + |
| 91 | +### Building the Project |
| 92 | + |
| 93 | +To build the project, run: |
| 94 | + |
| 95 | +```sh |
| 96 | +npm run build |
| 97 | +``` |
| 98 | + |
| 99 | +#### Running Tests |
| 100 | + |
| 101 | +To run the tests, use: |
| 102 | + |
| 103 | +```sh |
| 104 | +npm run test |
| 105 | +``` |
| 106 | + |
| 107 | +## License |
| 108 | + |
| 109 | +This project is licensed under the MIT License - see the [LICENSE](https://github.com/Lawndlwd/unused-i18n/blob/HEAD/LICENSE) file for details. |
| 110 | + |
| 111 | +## Contributing |
| 112 | + |
| 113 | +Contributions are welcome! Please open an issue or submit a pull request if you have any improvements or suggestions. |
| 114 | + |
| 115 | +Acknowledgements |
| 116 | + |
| 117 | +- [Vite](https://vitejs.dev/) - Next Generation Frontend Tooling. |
| 118 | +- [TypeScript](https://www.typescriptlang.org/) - Typed JavaScript used in this project. |
| 119 | +- [Vitest](https://vitest.dev/guide/cli) - Testing framework used in this project. |
| 120 | +- [Commander](https://github.com/tj/commander.js#readme) - Node.js command-line interfaces. |
0 commit comments