An open API service indexing awesome lists of open source software.

https://github.com/christianivicevic/intl-watcher

Automated translation key extraction and dictionary management plugin for Next.js
https://github.com/christianivicevic/intl-watcher

i18n internationalization intl localization next nextjs react translate translation

Last synced: about 1 month ago
JSON representation

Automated translation key extraction and dictionary management plugin for Next.js

Awesome Lists containing this project

README

        

๐ŸŒ intl-watcher plugin for Next.js




๐Ÿ‘ช All Contributors: 1


๐Ÿค Code of Conduct: Kept
๐Ÿงช Coverage
๐Ÿ“ License: MIT
๐Ÿ“ฆ npm version
๐Ÿ’ช TypeScript: Strict

![Showcase](./assets/showcase.gif)

Automatically scans your Next.js project's source files to manage internationalization (i18n) translation keys.
It keeps your translation dictionaries up-to-date by extracting new keys, removing unused ones, and optionally partitioning keys into separate client and server dictionaries.

## Features

- **Automatic Extraction**: Scans your project's source code for i18n keys.
- **Dictionary Syncing**: Automatically updates JSON dictionaries with new translation keys.
- **Unused Keys Handling**: Warns or removes unused keys.
- **Client/Server Partitioning**: Separates translation keys into client-side and server-side bundles.
- **Debounced Scanning**: Efficiently handles rapid file changes.

## Installation

Install the package via npm, yarn or pnpm:
```bash
npm install intl-watcher
# or
yarn add intl-watcher
# or
pnpm add intl-watcher
```

## Usage

Wrap your Next.js configuration with the provided `createIntlWatcher` function:

```ts
// next.config.ts
import { createIntlWatcher } from 'intl-watcher'

const withIntlWatcher = createIntlWatcher({
i18nDictionaryPaths: ['./locales/en.json'],
applyPartitioning: true,
debounceDelay: 500,
defaultTranslationGeneratorFn: (key) => `[NYT: ${key}]`,
partitioningOptions: {
clientFunction: 'useTranslations',
serverFunction: 'getTranslations',
},
removeUnusedKeys: true,
sourceDirectory: './src'
})

export default withIntlWatcher({
reactStrictMode: true,
// other Next.js config options...
})
```

## Configuration Options

### Required Options

#### `i18nDictionaryPaths`

- **Type:** `string[]`
- **Description:** Paths to JSON dictionary files to manage.

### Optional Options

#### `applyPartitioning`

- **Type**: `boolean`
- **Default**: `false`
- **Description**: Enables splitting of translation keys into separate client/server dictionaries.

#### `debounceDelay`

- **Type:** `number`
- **Default:** `500`
- **Description:** Delay (ms) for debouncing scans after file changes.

#### `defaultTranslationGeneratorFn`

- **Type**: `(key: string) => string`
- **Default**:
```js
(key) => `[NYT: ${key}]`
```
- **Description**: Function to generate default values for new translation keys.

#### `partitioningOptions`

- **Type:** `{ clientFunction?: string; serverFunction?: string }`
- **Default:**
```json5
{
clientFunction: 'useTranslations',
serverFunction: 'getTranslations'
}
```
- **Description**: Identifiers to distinguish client/server translation functions.

#### `removeUnusedKeys`

- **Type**: `boolean`
- **Default**: `false`
- **Description**: Removes unused translation keys if true; otherwise, logs a warning.

#### `sourceDirectory`

- **Type:** `string`
- **Default:** `'./src'`
- **Description:** Directory to scan for translation keys.

#### `tsConfigFilePath`

- **Type**: `string`
- **Default**: `tsconfig.json`
- **Description**: Path to the tsconfig file to resolve file scanning criteria.

## Dictionary Partitioning

When `applyPartitioning` is enabled, the plugin generates separate dictionaries for client and server bundles.
For example:

```
locales/
โ”œโ”€โ”€ en.json
โ”œโ”€โ”€ en.client.json
โ””โ”€โ”€ en.server.json
```

This enables optimized bundle sizes and clearer separation of translations by environment.

## Development

See [`.github/CONTRIBUTING.md`](./.github/CONTRIBUTING.md), then [`.github/DEVELOPMENT.md`](./.github/DEVELOPMENT.md).
Thanks! ๐Ÿ’–

## Contributors



Christian Ivicevic
Christian Ivicevic
๐Ÿ’ป ๐Ÿ–‹ ๐Ÿ“– ๐Ÿค” ๐Ÿš‡ ๐Ÿšง ๐Ÿ“† ๐Ÿ”ง