# @karuta/linguist

> Extract messages and manage translations with FormatJS

Latest version **0.8.0** (published 2024-06-30) · AGPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install @karuta/linguist
pnpm add @karuta/linguist
yarn add @karuta/linguist
bun add @karuta/linguist
```

Provides the command `linguist`.

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.8.0 |
| Published | 2024-06-30 |
| First published | 2021-09-05 |
| Weekly downloads | 0 |
| License | AGPL-3.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 6 |
| Unpacked size | 53.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Kazuichi Takashiro |
| Keywords | react, i18n, extract |

## Links

- npm: https://www.npmjs.com/package/@karuta/linguist
- Repository: https://github.com/takashiro/linguist
- Homepage: https://github.com/takashiro/linguist#readme
- Issues: https://github.com/takashiro/linguist/issues
- npm.io page: https://npm.io/package/@karuta/linguist

## Dependencies (6)

- [glob](https://npm.io/package/glob.md) ^10.4.2
- [yargs](https://npm.io/package/yargs.md) ^17.7.2
- [typescript](https://npm.io/package/typescript.md) ^5.5.2
- [cosmiconfig](https://npm.io/package/cosmiconfig.md) ^9.0.0
- [@formatjs/ts-transformer](https://npm.io/package/@formatjs/ts-transformer.md) ^3.13.14
- [@formatjs/icu-messageformat-parser](https://npm.io/package/@formatjs/icu-messageformat-parser.md) ^2.7.8

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.8.0 (latest) — 2024-06-30
- 0.7.2 — 2023-12-16
- 0.7.1 — 2023-12-16
- 0.6.0 — 2022-11-06
- 0.5.0 — 2022-04-09
- 0.4.0 — 2021-09-07
- 0.3.0 — 2021-09-06
- 0.2.1 — 2021-09-05
- 0.2.0 — 2021-09-05

## README

Linguist [![npm version](https://badge.fury.io/js/@karuta%2Flinguist.svg)](https://badge.fury.io/js/@karuta%2Flinguist) [![Node.js CI](https://github.com/takashiro/linguist/actions/workflows/nodejs.yml/badge.svg)](https://github.com/takashiro/linguist/actions/workflows/nodejs.yml)
========

A project inspired by FormatJS and Qt Linguist
- Extract messages from your source codes.
- Automatically generate a unique id for each message.
- Generate strings in JSON format where translators can work on.
- Release string bundles as JavaScript scripts.

## License
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

## Usage

1. Install Linguist as a development dependency.

	```bash
	npm i -D @karuta/linguist
	```

1. For React projects, install `react-intl` and wrap your messages.

	```TypeScript
	import {
		useIntl,
		defineMessages,
		FormattedMessage,
		IntlShape,
	} from 'react-intl';

	// Define a set of messages
	const desc = defineMessages({
		emptyRoomNumber: { defaultMessage: 'Please input a room number.' },
		// ...
	});

	// Use pre-defined messages
	export function YourComponentA(): JSX.Element {
		const intl = useIntl();

		return (
			<input
				type="number"
				inputMode="decimal"
				placeholder={intl.formatMessage(desc.roomNumber)}
			/>
		);
	}

	// Directly use <FormattedMessage>
	export function YourComponentB(): JSX.Element {
		return (
			<FormattedMessage defaultMessage="This is a test." />
		);
	}
	```

1. Add a `.linguistrc.js` to your root folder to configure targeted languages and define an override id function.
	```JavaScript
	/**
	 * @type {import('@karuta/linguist').Config}
	*/
	module.exports = {
		locales: [
			'en-US',
			'en-GB',
			'zh-Hans',
			'zh-Hant',
			'yue',
			'ja-JP',
		],
		overrideIdFn: '[sha512:contenthash:base64:6]',
	};
	```

1. Generate messages.
	```bash
	npx linguist update
	```

1. Add translations to the JSON files (e.g. `message/ja-JP.json`).

1. Release messages.
	```bash
	npx linguist release
	```

1. Prune unused messages whenever you deleted some old strings.
	```bash
	npx linguist prune
	```

## Configuration

```JavaScript
/**
 * @type {import('@karuta/linguist').Config}
*/
export default {
	/**
	 * Supported locales of the application. Each locale has a separate file. Default: en-US, zh-CN.
	 */
	locales: [
		'en-US', // English (United States)
		'en-GB', // English (United Kingdom)
		'zh-Hans', // 中文（简体）
		'zh-Hant', // 中文（繁体）
		'ja-JP', // 日本語
	],

	/**
	 * If no explicit id is defined, generate an automatic id for the message.
	 */
	overrideIdFn: '[sha512:contenthash:base64:6]',

	/**
	 * The directory to look for source files. Default: 'src'.
	 */
	sourceDir: 'src',

	/**
	 * The directory to save messages extracted from source files. Default: 'message'.
	 */
	messageDir: 'message',
};
```

---
_Source: https://npm.io/package/@karuta/linguist · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
