locl-js 1.0.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/readme.md DELETED
@@ -1,321 +0,0 @@
1
- # LocL.js 🌐
2
-
3
- ![Coverage](https://img.shields.io/badge/coverage-98%25-brightgreen)
4
-
5
- **LocL** is a powerful, lightweight, and fully-featured TypeScript internationalization (i18n) library. It is designed to be flexible, easy to use, and highly extensible, providing a seamless experience for adding multiple languages to your projects. With built-in support for plurals, formatting, and modularization, `LocL` is the perfect tool for developers looking for a modern and robust i18n solution.
6
-
7
- It is inspired by libraries like `i18next` and `react-i18next`, but with a focus on type-safety, simplicity, performance and backend usability like discord bot and other apps.
8
-
9
- ## Features ✨
10
-
11
- - **💪 Type-Safe**: Leverages TypeScript to provide compile-time safety for your translation keys, ensuring that you never miss a translation or use an incorrect key.
12
- - **- 📦 Lightweight**: With zero dependencies, `LocL` is a small library that won't bloat your project.
13
- - **- 🚀 Powerful**: Supports plurals, interpolation, formatting, and modularization out of the box.
14
- - **- 🔧 Extensible**: Easily extend the library with custom formatters to fit your needs.
15
- - **- 🌍 Modular**: Organize your translations into modules to keep your codebase clean and maintainable.
16
- - **- ⚡ Fast**: Uses caching to provide fast and efficient translations.
17
- - **- 💻 Dev-Friendly**: Provides helpful warnings in development mode to help you catch missing translations.
18
-
19
- ## Installation
20
-
21
- ```bash
22
- npm install locl-js
23
- ```
24
-
25
- ## Quick Start
26
-
27
- ```typescript
28
- import { initLocL, LangWithPlurals } from "locl-js";
29
-
30
- const resources = {
31
- en: {
32
- hello: "Hello, {name}!",
33
- messages: {
34
- one: "You have one message.",
35
- other: "You have {count} messages.",
36
- },
37
- },
38
- fr: {
39
- hello: "Bonjour, {name}!",
40
- messages: {
41
- one: "Vous avez un message.",
42
- other: "Vous avez {count} messages.",
43
- },
44
- },
45
- };
46
-
47
- const translator = initLocL({
48
- resources,
49
- fallbackLanguage: "en",
50
- });
51
-
52
- console.log(translator.t("hello", { name: "John" })); // "Hello, John!"
53
- console.log(translator.plural("messages", { count: 1 })); // "You have one message."
54
- console.log(translator.plural("messages", { count: 5 })); // "You have 5 messages."
55
-
56
- translator.changeLanguage("fr");
57
-
58
- console.log(translator.t("hello", { name: "John" })); // "Bonjour, John!"
59
- console.log(translator.plural("messages", { count: 1 })); // "Vous avez un message."
60
- console.log(translator.plural("messages", { count: 5 })); // "Vous avez 5 messages."
61
- ```
62
-
63
- ## API
64
-
65
- ### `initLocL(config)`
66
-
67
- Initializes a new LocL instance.
68
-
69
- - `config`: An object with the following properties:
70
- - `resources`: An object where keys are language codes and values are translation objects.
71
- - `fallbackLanguage`: The language to use when a translation is not available in the current language.
72
- - `language` (optional): The initial language to use.
73
- - `scope` (optional): The scope to load translations from.
74
- - `formatters` (optional): An object with custom formatters.
75
- - `useDefaultFormatters` (optional): Whether to use the default formatters. Defaults to `true`.
76
- - `devMode` (optional): Whether to enable development mode. Defaults to `false`.
77
- - `useCache` (optional): Whether to use caching. Defaults to `true`.
78
-
79
- ### `translator.t(key, values, format)`
80
-
81
- Translates a key.
82
-
83
- - `key`: The key to translate.
84
- - `values` (optional): An object with values to interpolate into the translation.
85
- - `format` (optional): An object with formatting options.
86
-
87
- ### `translator.plural(key, values, format)`
88
-
89
- Translates a key with pluralization.
90
-
91
- - `key`: The key to translate.
92
- - `values`: An object with a `count` property and other values to interpolate.
93
- - `format` (optional): An object with formatting options.
94
-
95
- ### `translator.changeLanguage(lang)`
96
-
97
- Changes the current language.
98
-
99
- - `lang`: The language to switch to.
100
-
101
- ### `translator.withConfig(config)`
102
-
103
- Creates a new proxy translator instance with a different configuration. This is useful for creating a translator with a different scope or language without creating a new instance.
104
-
105
- - `config`: An object with the following properties:
106
- - `scope` (optional): The scope to load translations from.
107
- - `language` (optional): The language to use.
108
-
109
- ### `translator.clone(language, scope)`
110
-
111
- Creates a new LocL instance with a different language or scope.
112
-
113
- - `language` (optional): The language to use.
114
- - `scope` (optional): The scope to load translations from.
115
-
116
- ### `translator.get(key)`
117
-
118
- Gets a translation object or a specific translation value.
119
-
120
- - `key` (optional): The key of the translation to get. If not provided, it returns the entire translation object for the current language and scope.
121
-
122
- ### `translator.format(value, formatter, args)`
123
-
124
- Formats a value using a specific formatter.
125
-
126
- - `value`: The value to format.
127
- - `formatter`: The name of the formatter to use.
128
- - `args` (optional): An array of arguments to pass to the formatter.
129
-
130
- ## Advanced Usage
131
-
132
- ### Interpolation
133
-
134
- You can interpolate values into your translations using curly braces:
135
-
136
- ```typescript
137
- const en = {
138
- hello: "Hello, {name}!",
139
- };
140
- ```
141
-
142
- Then, pass the values to the `t` function:
143
-
144
- ```typescript
145
- translator.t("hello", { name: "John" }); // "Hello, John!"
146
- ```
147
-
148
- ### Pluralization
149
-
150
- `LocL` supports two ways of handling plurals:
151
-
152
- #### 1. Object Pluralization
153
-
154
- You can define plurals as an object with different forms:
155
-
156
- ```typescript
157
- const en = {
158
- messages: {
159
- one: "You have one message.",
160
- other: "You have {count} messages.",
161
- },
162
- };
163
- ```
164
-
165
- Then, use the `plural` function:
166
-
167
- ```typescript
168
- translator.plural("messages", { count: 1 }); // "You have one message."
169
- translator.plural("messages", { count: 5 }); // "You have 5 messages."
170
- ```
171
-
172
- #### 2. Suffix Pluralization
173
-
174
- You can also define plurals using suffixes:
175
-
176
- ```typescript
177
- const en = {
178
- message_one: "You have one message.",
179
- message_other: "You have {count} messages.",
180
- };
181
- ```
182
-
183
- Then, use the `plural` function with the base key:
184
-
185
- ```typescript
186
- translator.plural("message", { count: 1 }); // "You have one message."
187
- translator.plural("message", { count: 5 }); // "You have 5 messages."
188
- ```
189
-
190
- ### Formatting
191
-
192
- `LocL` comes with a set of built-in formatters that you can use to format your translations.
193
-
194
- #### Default Formatters
195
-
196
- - `upper`: Converts the value to uppercase.
197
- - `lower`: Converts the value to lowercase.
198
- - `capitalize`: Capitalizes the first letter of the value.
199
- - `trim`: Trims the value.
200
- - `truncate`: Truncates the value to a specific length.
201
- - `number`: Formats a number using `Intl.NumberFormat`.
202
- - `currency`: Formats a currency value using `Intl.NumberFormat`.
203
- - `date`: Formats a date using `Intl.DateTimeFormat`.
204
- - `relativeDate`: Formats a date as a relative time (e.g., "2 hours ago").
205
- - `json`: Converts a value to a JSON string.
206
- - `yesNo`: Returns "Yes" or "No" based on the value.
207
- - `boolean`: Returns "true" or "false" based on the value.
208
- - `padStart`: Pads the start of the value with a string.
209
- - `padEnd`: Pads the end of the value with a string.
210
-
211
- You can use formatters in your translations like this:
212
-
213
- ```typescript
214
- const en = {
215
- greeting: "Hello, {name | upper}!",
216
- };
217
-
218
- translator.t("greeting", { name: "John" }); // "Hello, JOHN!"
219
- ```
220
-
221
- You can also pass arguments to formatters:
222
-
223
- ```typescript
224
- const en = {
225
- price: "The price is {price | currency:USD}",
226
- };
227
-
228
- translator.t("price", { price: 123 }); // "The price is $123.00"
229
- ```
230
-
231
- #### Custom Formatters
232
-
233
- You can also define your own custom formatters:
234
-
235
- ```typescript
236
- const translator = initLocL({
237
- resources,
238
- fallbackLanguage: "en",
239
- formatters: {
240
- reverse: (value) => String(value).split("").reverse().join(""),
241
- },
242
- });
243
-
244
- const en = {
245
- greeting: "Hello, {name | reverse}!",
246
- };
247
-
248
- translator.t("greeting", { name: "John" }); // "Hello, nhoJ!"
249
- ```
250
-
251
- ### Modules
252
-
253
- You can organize your translations into scopes to keep your codebase clean and maintainable.
254
-
255
- ```typescript
256
- const resources = {
257
- en: {
258
- common: {
259
- hello: "Hello",
260
- },
261
- home: {
262
- title: "Welcome to the home page",
263
- },
264
- },
265
- };
266
- ```
267
-
268
- Then, you can create a translator for a specific scope:
269
-
270
- ```typescript
271
- const homeTranslator = translator.withConfig({ scope: "home" });
272
-
273
- console.log(homeTranslator.t("title")); // "Welcome to the home page"
274
- ```
275
-
276
- You can also load multiple scopes at once:
277
-
278
- ```typescript
279
- const multiScopeTranslator = translator.withConfig({ scope: ["common", "home"] });
280
-
281
- console.log(multiScopeTranslator.t("common.hello")); // "Hello"
282
- console.log(multiScopeTranslator.t("home.title")); // "Welcome to the home page"
283
- ```
284
-
285
- ### Type-Safe Translations
286
-
287
- `LocL` provides type-safety for your translation keys. To enable this, you can use the `LangWithPlurals` type to define your translation resources:
288
-
289
- ```typescript
290
- import { LangWithPlurals } from "locl-js";
291
-
292
- const en: LangWithPlurals<typeof en> = {
293
- hello: "Hello, {name}!",
294
- messages: {
295
- one: "You have one message.",
296
- other: "You have {count} messages.",
297
- },
298
- };
299
- const fr: LangWithPlurals<typeof en> = {
300
- hello: "Bonjour, {name}!",
301
- messages: {
302
- one: "Vous avez un message.",
303
- few: "Vous avez quelques messages.",
304
- // Typescript error for missing "other" plural key
305
- },
306
- };
307
- ```
308
-
309
- Now, you will get autocompletion and type-checking for your translation keys:
310
-
311
- ```typescript
312
- translator.t("hell"); // TypeScript error: Argument of type '"hell"' is not assignable to parameter of type '"hello" | "messages"'.
313
- ```
314
-
315
- ## Contributing
316
-
317
- Contributions are welcome! Please open an issue or submit a pull request if you have any ideas or suggestions.
318
-
319
- ## License
320
-
321
- This project is licensed under the MIT License.