Skip to content

Commit 7542c37

Browse files
committed
doc: README.md
1 parent 1fb50ce commit 7542c37

File tree

2 files changed

+123
-6
lines changed

2 files changed

+123
-6
lines changed

README.md

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# intlit <a href="https://github.com/denostack"><img src="https://raw.githubusercontent.com/denostack/images/main/logo.svg" width="160" align="right" /></a>
1+
# Intlit <a href="https://github.com/denostack"><img src="https://raw.githubusercontent.com/denostack/images/main/logo.svg" width="160" align="right" /></a>
22

33
<p>
44
<a href="https://github.com/denostack/intlit/actions"><img alt="Build" src="https://img.shields.io/github/actions/workflow/status/denostack/intlit/ci.yml?branch=main&logo=github&style=flat-square" /></a>
@@ -7,7 +7,128 @@
77
<img alt="Language Typescript" src="https://img.shields.io/badge/language-Typescript-007acc.svg?style=flat-square" />
88
<br />
99
<a href="https://jsr.io/@denostack/intlit"><img alt="JSR version" src="https://jsr.io/badges/@denostack/intlit?style=flat-square" /></a>
10-
<a href="https://deno.land/x/intlit"><img alt="Deno version" src="https://deno.land/badge/intlit/version?style=flat-square" /></a>
1110
<a href="https://www.npmjs.com/package/intlit"><img alt="NPM Version" src="https://img.shields.io/npm/v/intlit.svg?style=flat-square&logo=npm" /></a>
1211
<a href="https://npmcharts.com/compare/intlit?minimal=true"><img alt="Downloads" src="https://img.shields.io/npm/dt/intlit.svg?style=flat-square" /></a>
1312
</p>
13+
14+
Elevate your internationalization (i18n) workflow with Intlit:
15+
gettext-compatible formatting, simplified plural handling, and first-class
16+
TypeScript support.
17+
18+
## Features
19+
20+
- Simple and intuitive API
21+
- Full TypeScript support for type safety
22+
- Unified key for singular and plural forms (unlike gettext which often requires
23+
separate `msgid` and `msgid_plural`)
24+
- Includes pluralization support (plugins can be optionally added for more
25+
features)
26+
- Easy to integrate with existing projects
27+
28+
## Installation
29+
30+
```bash
31+
npm install intlit
32+
```
33+
34+
```typescript
35+
import { Formatter } from "intlit";
36+
```
37+
38+
## Usage
39+
40+
### Basic Usage
41+
42+
```typescript
43+
const formatter = new Formatter({
44+
locale: "en",
45+
});
46+
47+
const text = formatter.format("Hello World");
48+
49+
console.log(text); // Output: Hello World
50+
```
51+
52+
### Handling Plurals
53+
54+
Intlit provides support for handling plural forms in different languages, making
55+
it easy to create grammatically correct translations.
56+
57+
First, let's see how to set up `Formatter` instances for English, Korean, and
58+
Arabic, including specific message translations for Korean and Arabic. The
59+
pluralization capability is available by default.
60+
61+
```typescript
62+
const formatter = new Formatter({
63+
locale: "en",
64+
});
65+
66+
// With count = 1 (singular)
67+
console.log(
68+
formatter.format("You have {count} file{count.other:}s{/}.", { count: 1 }),
69+
); // Output: You have 1 file.
70+
71+
// With count = 2 (plural)
72+
console.log(
73+
formatter.format("You have {count} file{count.other:}s{/}.", { count: 2 }),
74+
); // Output: You have 2 files.
75+
```
76+
77+
```typescript
78+
const messages = {
79+
"You have {count} file{count.other:}s{/}.": "{count}개의 파일이 있습니다.",
80+
};
81+
82+
const formatter = new Formatter({
83+
locale: "ko",
84+
messages,
85+
});
86+
87+
// With count = 1
88+
console.log(
89+
formatter.format("You have {count} file{count.other:}s{/}.", { count: 1 }),
90+
); // Output: 1개의 파일이 있습니다.
91+
```
92+
93+
```typescript
94+
const messages = {
95+
"You have {count} file{count.other:}s{/}.":
96+
"{count.zero:}لا يوجد لديك ملفات.{.one:}لديك ملف واحد.{.two:}لديك ملفان.{.few:}لديك {count} ملفات قليلة.{.many:}لديك {count} ملفات كثيرة.{.other:}لديك {count} ملفات.{/}",
97+
};
98+
99+
const formatter = new Formatter({
100+
locale: "ar",
101+
messages: messages,
102+
});
103+
104+
// Arabic has multiple plural forms (zero, one, two, few, many, other). 🫢
105+
106+
console.log(
107+
formatter.format("You have {count} file{count.other:}s{/}.", { count: 0 }),
108+
); // Output: لا يوجد لديك ملفات.
109+
110+
console.log(
111+
formatter.format("You have {count} file{count.other:}s{/}.", { count: 1 }),
112+
); // Output: لديك ملف واحد.
113+
114+
console.log(
115+
formatter.format("You have {count} file{count.other:}s{/}.", { count: 2 }),
116+
); // Output: لديك ملفان.
117+
118+
console.log(
119+
formatter.format("You have {count} file{count.other:}s{/}.", { count: 3 }),
120+
); // Output: لديك 3 ملفات قليلة.
121+
122+
console.log(
123+
formatter.format("You have {count} file{count.other:}s{/}.", { count: 11 }),
124+
); // Output: لديك 11 ملفات كثيرة.
125+
126+
console.log(
127+
formatter.format("You have {count} file{count.other:}s{/}.", { count: 100 }),
128+
); // Output: لديك 100 ملفات.
129+
```
130+
131+
### Message Format for Plurals
132+
133+
The library uses a specific format for pluralization within the message string
134+
itself, which simplifies key management. Unlike traditional `gettext`

formatter.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { assertEquals } from "@std/assert/assert-equals";
22
import { Formatter } from "./formatter.ts";
3-
import { defaultHooks } from "./plugins/default.ts";
43

54
Deno.test("formatter, plural", () => {
65
const messages = {
@@ -17,7 +16,6 @@ Deno.test("formatter, plural", () => {
1716
{
1817
const formatter = new Formatter({
1918
locale: "en",
20-
hooks: defaultHooks,
2119
});
2220

2321
assertEquals(
@@ -37,7 +35,6 @@ Deno.test("formatter, plural", () => {
3735
{
3836
const formatter = new Formatter({
3937
locale: "ko",
40-
hooks: defaultHooks,
4138
messages: messages.ko,
4239
});
4340

@@ -51,7 +48,6 @@ Deno.test("formatter, plural", () => {
5148
{
5249
const formatter = new Formatter({
5350
locale: "ar",
54-
hooks: defaultHooks,
5551
messages: messages.ar,
5652
});
5753

0 commit comments

Comments
 (0)