|
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> |
2 | 2 |
|
3 | 3 | <p>
|
4 | 4 | <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 | <img alt="Language Typescript" src="https://img.shields.io/badge/language-Typescript-007acc.svg?style=flat-square" />
|
8 | 8 | <br />
|
9 | 9 | <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> |
11 | 10 | <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>
|
12 | 11 | <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>
|
13 | 12 | </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` |
0 commit comments