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