Skip to content

Commit b7c09e4

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

File tree

2 files changed

+117
-6
lines changed

2 files changed

+117
-6
lines changed

README.md

Lines changed: 117 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,122 @@
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: gettext-compatible formatting, simplified plural handling, and first-class TypeScript support.
15+
16+
## Features
17+
18+
- Simple and intuitive API
19+
- Full TypeScript support for type safety
20+
- Unified key for singular and plural forms (unlike gettext which often requires separate `msgid` and `msgid_plural`)
21+
- Includes pluralization support (plugins can be optionally added for more features)
22+
- Easy to integrate with existing projects
23+
24+
## Installation
25+
26+
```bash
27+
npm install intlit
28+
```
29+
30+
```typescript
31+
import { Formatter } from "intlit";
32+
```
33+
34+
## Usage
35+
36+
### Basic Usage
37+
38+
```typescript
39+
const formatter = new Formatter({
40+
locale: "en",
41+
});
42+
43+
const text = formatter.format("Hello World");
44+
45+
console.log(text); // Output: Hello World
46+
```
47+
48+
### Handling Plurals
49+
50+
Intlit provides support for handling plural forms in different languages, making it easy to create grammatically correct translations.
51+
52+
First, let's see how to set up `Formatter` instances for English, Korean, and Arabic, including specific message translations for Korean and Arabic. The pluralization capability is available by default.
53+
54+
```typescript
55+
const formatter = new Formatter({
56+
locale: "en",
57+
});
58+
59+
// With count = 1 (singular)
60+
console.log(
61+
formatter.format("You have {count} file{count.other:}s{/}.", { count: 1 })
62+
); // Output: You have 1 file.
63+
64+
// With count = 2 (plural)
65+
console.log(
66+
formatter.format("You have {count} file{count.other:}s{/}.", { count: 2 })
67+
); // Output: You have 2 files.
68+
```
69+
70+
```typescript
71+
const messages = {
72+
"You have {count} file{count.other:}s{/}.": "{count}개의 파일이 있습니다.",
73+
};
74+
75+
const formatter = new Formatter({
76+
locale: "ko",
77+
messages,
78+
});
79+
80+
// With count = 1
81+
console.log(
82+
formatter.format("You have {count} file{count.other:}s{/}.", { count: 1 })
83+
); // Output: 1개의 파일이 있습니다.
84+
```
85+
86+
87+
```typescript
88+
const messages = {
89+
"You have {count} file{count.other:}s{/}.":
90+
"{count.zero:}لا يوجد لديك ملفات.{.one:}لديك ملف واحد.{.two:}لديك ملفان.{.few:}لديك {count} ملفات قليلة.{.many:}لديك {count} ملفات كثيرة.{.other:}لديك {count} ملفات.{/}",
91+
};
92+
93+
const formatter = new Formatter({
94+
locale: "ar",
95+
messages: messages,
96+
});
97+
98+
99+
// Arabic has multiple plural forms (zero, one, two, few, many, other). 🫢
100+
101+
console.log(
102+
formatter.format("You have {count} file{count.other:}s{/}.", { count: 0 })
103+
); // Output: لا يوجد لديك ملفات.
104+
105+
console.log(
106+
formatter.format("You have {count} file{count.other:}s{/}.", { count: 1 })
107+
); // Output: لديك ملف واحد.
108+
109+
console.log(
110+
formatter.format("You have {count} file{count.other:}s{/}.", { count: 2 })
111+
); // Output: لديك ملفان.
112+
113+
console.log(
114+
formatter.format("You have {count} file{count.other:}s{/}.", { count: 3 })
115+
); // Output: لديك 3 ملفات قليلة.
116+
117+
console.log(
118+
formatter.format("You have {count} file{count.other:}s{/}.", { count: 11 })
119+
); // Output: لديك 11 ملفات كثيرة.
120+
121+
console.log(
122+
formatter.format("You have {count} file{count.other:}s{/}.", { count: 100 })
123+
); // Output: لديك 100 ملفات.
124+
```
125+
126+
### Message Format for Plurals
127+
128+
The library uses a specific format for pluralization within the message string 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)