@eouia/intl-msg 0.1.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/LICENSE +21 -0
- package/README.md +615 -0
- package/dist/browser/intl-msg.js +591 -0
- package/dist/cjs/compose.cjs +65 -0
- package/dist/cjs/loaders.cjs +73 -0
- package/dist/cjs/main.cjs +588 -0
- package/dist/esm/compose.js +60 -0
- package/dist/esm/loaders.js +66 -0
- package/dist/esm/main.js +586 -0
- package/dist/esm/package.json +3 -0
- package/package.json +67 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Seongnoh Sean Yi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,615 @@
|
|
|
1
|
+
# intl-msg
|
|
2
|
+
|
|
3
|
+
Native `Intl`-based i18n message formatting for modern Node.js, browsers, and Electron, with no runtime dependencies.
|
|
4
|
+
|
|
5
|
+
## Status
|
|
6
|
+
|
|
7
|
+
The package now builds from a single source file and publishes both CommonJS and ESM outputs:
|
|
8
|
+
|
|
9
|
+
- CommonJS: `dist/cjs/main.cjs`
|
|
10
|
+
- ESM: `dist/esm/main.js`
|
|
11
|
+
- Source of truth: `src/main.js`
|
|
12
|
+
|
|
13
|
+
Legacy files such as `commonjs/main.js`, `esm/main.js`, and the root `main.js` are now thin compatibility shims. Package consumers should rely on the published package entry points.
|
|
14
|
+
|
|
15
|
+
## Runtime requirements
|
|
16
|
+
|
|
17
|
+
This library is designed for modern JavaScript runtimes with full `Intl` support. It is not a legacy-browser compatibility build.
|
|
18
|
+
|
|
19
|
+
Minimum practical requirements:
|
|
20
|
+
|
|
21
|
+
- Node.js: 16+ recommended
|
|
22
|
+
- Browsers: native ESM support and modern class features, including private fields
|
|
23
|
+
- Electron: a modern Electron release whose bundled Chromium/Node versions satisfy the browser and Node requirements above
|
|
24
|
+
|
|
25
|
+
Required built-in `Intl` APIs:
|
|
26
|
+
|
|
27
|
+
- `Intl.getCanonicalLocales`
|
|
28
|
+
- `Intl.PluralRules`
|
|
29
|
+
- `Intl.DateTimeFormat`
|
|
30
|
+
- `Intl.RelativeTimeFormat`
|
|
31
|
+
- `Intl.ListFormat`
|
|
32
|
+
- `Intl.NumberFormat`
|
|
33
|
+
|
|
34
|
+
Required JavaScript features in the runtime:
|
|
35
|
+
|
|
36
|
+
- ES modules or a bundler that can consume them
|
|
37
|
+
- private class fields
|
|
38
|
+
- optional chaining
|
|
39
|
+
- nullish coalescing
|
|
40
|
+
|
|
41
|
+
If your target runtime does not provide the required `Intl` APIs, you must inject a compatible `intlPolyfill` when constructing `IntlMsg`.
|
|
42
|
+
|
|
43
|
+
Locale input remains compatibility-friendly:
|
|
44
|
+
|
|
45
|
+
- common non-BCP47 separators such as `en_US` are normalized to `en-US`
|
|
46
|
+
- fallback lookup checks the full canonical locale first, then falls back through the locale base name chain such as `en-US` and `en`
|
|
47
|
+
|
|
48
|
+
## Environment support
|
|
49
|
+
|
|
50
|
+
Supported in practice means:
|
|
51
|
+
|
|
52
|
+
- Node.js: works via the published CommonJS and ESM package entry points
|
|
53
|
+
- Browsers: works in modern browsers through native ESM, a bundler, or the browser global build
|
|
54
|
+
- Electron: works when the embedded Node/Chromium runtime provides the required `Intl` APIs and language features
|
|
55
|
+
|
|
56
|
+
Not currently provided:
|
|
57
|
+
|
|
58
|
+
- a legacy ES5 build
|
|
59
|
+
- a UMD or IIFE browser bundle
|
|
60
|
+
- automatic polyfills for missing `Intl` features
|
|
61
|
+
|
|
62
|
+
## Install
|
|
63
|
+
|
|
64
|
+
```sh
|
|
65
|
+
npm install @eouia/intl-msg
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
If you publish under a different package name, replace `@eouia/intl-msg` accordingly.
|
|
69
|
+
|
|
70
|
+
## Usage
|
|
71
|
+
|
|
72
|
+
### ESM
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
import IntlMsg from '@eouia/intl-msg'
|
|
76
|
+
|
|
77
|
+
const msg = IntlMsg.factory({
|
|
78
|
+
locales: ['en-US', 'en'],
|
|
79
|
+
dictionaries: {
|
|
80
|
+
en: {
|
|
81
|
+
translations: {
|
|
82
|
+
HELLO: 'Hello, {{name}}.',
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
console.log(msg.message('HELLO', { name: 'Taylor' }))
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### CommonJS
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
const IntlMsg = require('@eouia/intl-msg')
|
|
95
|
+
|
|
96
|
+
const msg = new IntlMsg()
|
|
97
|
+
msg.addLocale(['en-US', 'en'])
|
|
98
|
+
msg.addDictionary({
|
|
99
|
+
en: {
|
|
100
|
+
translations: {
|
|
101
|
+
HELLO: 'Hello, {{name}}.',
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
console.log(msg.message('HELLO', { name: 'Taylor' }))
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Browser `<script>`
|
|
110
|
+
|
|
111
|
+
For modern browsers, the package also ships a browser global build at `dist/browser/intl-msg.js`.
|
|
112
|
+
|
|
113
|
+
```html
|
|
114
|
+
<script src="./dist/browser/intl-msg.js"></script>
|
|
115
|
+
<script>
|
|
116
|
+
const msg = IntlMsg.factory({
|
|
117
|
+
locales: ['en-US', 'en'],
|
|
118
|
+
dictionaries: {
|
|
119
|
+
en: {
|
|
120
|
+
translations: {
|
|
121
|
+
HELLO: 'Hello, {{name}}.',
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
console.log(msg.message('HELLO', { name: 'Taylor' }))
|
|
128
|
+
</script>
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
This build is intended for modern browsers. It is not a transpiled legacy-browser build.
|
|
132
|
+
|
|
133
|
+
## Dictionary format
|
|
134
|
+
|
|
135
|
+
```json
|
|
136
|
+
{
|
|
137
|
+
"en": {
|
|
138
|
+
"translations": {
|
|
139
|
+
"HELLO": "Hello, {{name}}."
|
|
140
|
+
},
|
|
141
|
+
"formatters": {
|
|
142
|
+
"currency": {
|
|
143
|
+
"format": "number",
|
|
144
|
+
"options": {
|
|
145
|
+
"style": "currency",
|
|
146
|
+
"currency": "USD"
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
- `translations` maps message keys to template strings
|
|
155
|
+
- `formatters` maps formatter names to formatter config objects
|
|
156
|
+
|
|
157
|
+
## Message syntax
|
|
158
|
+
|
|
159
|
+
### Plain substitution
|
|
160
|
+
|
|
161
|
+
```txt
|
|
162
|
+
{{name}}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
```js
|
|
166
|
+
msg.message('HELLO', { name: 'Taylor' })
|
|
167
|
+
// => 'Hello, Taylor.'
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Formatted substitution
|
|
171
|
+
|
|
172
|
+
```txt
|
|
173
|
+
{{amount:currency}}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
```js
|
|
177
|
+
msg.addDictionary({
|
|
178
|
+
en: {
|
|
179
|
+
translations: {
|
|
180
|
+
TOTAL: 'Total: {{amount:currency}}',
|
|
181
|
+
},
|
|
182
|
+
formatters: {
|
|
183
|
+
currency: {
|
|
184
|
+
format: 'number',
|
|
185
|
+
options: { style: 'currency', currency: 'USD' },
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
msg.message('TOTAL', { amount: 1234.5 })
|
|
192
|
+
// => 'Total: $1,234.50'
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## Locale fallback
|
|
196
|
+
|
|
197
|
+
Locales are resolved using a fallback chain. For example:
|
|
198
|
+
|
|
199
|
+
- `en-US` tries `en-US`, then `en`
|
|
200
|
+
- `zh-Hant-TW` tries `zh-Hant-TW`, then `zh-Hant`, then `zh`
|
|
201
|
+
|
|
202
|
+
You can also provide multiple preferred locales:
|
|
203
|
+
|
|
204
|
+
```js
|
|
205
|
+
msg.setLocale(['fr-CA', 'fr', 'en'])
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
Message lookup will try each locale in order, including each locale's fallback chain, until it finds a matching translation.
|
|
209
|
+
|
|
210
|
+
## Built-in formatters
|
|
211
|
+
|
|
212
|
+
The library includes these built-in formatters:
|
|
213
|
+
|
|
214
|
+
- `pluralRules`
|
|
215
|
+
- `pluralRange`
|
|
216
|
+
- `list`
|
|
217
|
+
- `number`
|
|
218
|
+
- `numberRange`
|
|
219
|
+
- `select`
|
|
220
|
+
- `dateTime`
|
|
221
|
+
- `dateTimeRange`
|
|
222
|
+
- `relativeTime`
|
|
223
|
+
- `duration`
|
|
224
|
+
- `humanizedRelativeTime`
|
|
225
|
+
|
|
226
|
+
### Example
|
|
227
|
+
|
|
228
|
+
```js
|
|
229
|
+
msg.addDictionary({
|
|
230
|
+
en: {
|
|
231
|
+
translations: {
|
|
232
|
+
SUMMARY: 'Today is {{today:dateLabel}}. Total: {{amount:currency}}.',
|
|
233
|
+
},
|
|
234
|
+
formatters: {
|
|
235
|
+
dateLabel: {
|
|
236
|
+
format: 'dateTime',
|
|
237
|
+
options: { weekday: 'long', month: 'long', day: 'numeric' },
|
|
238
|
+
},
|
|
239
|
+
currency: {
|
|
240
|
+
format: 'number',
|
|
241
|
+
options: { style: 'currency', currency: 'USD' },
|
|
242
|
+
},
|
|
243
|
+
},
|
|
244
|
+
},
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
msg.message('SUMMARY', {
|
|
248
|
+
today: '2026-03-23',
|
|
249
|
+
amount: 1234.5,
|
|
250
|
+
})
|
|
251
|
+
// => 'Today is Monday, March 23. Total: $1,234.50.'
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### Duration example
|
|
255
|
+
|
|
256
|
+
```js
|
|
257
|
+
msg.addDictionary({
|
|
258
|
+
en: {
|
|
259
|
+
translations: {
|
|
260
|
+
ELAPSED: 'Elapsed: {{time:elapsed}}',
|
|
261
|
+
},
|
|
262
|
+
formatters: {
|
|
263
|
+
elapsed: {
|
|
264
|
+
format: 'duration',
|
|
265
|
+
options: { style: 'short' },
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
msg.message('ELAPSED', {
|
|
272
|
+
time: { hours: 1, minutes: 30, seconds: 5 },
|
|
273
|
+
})
|
|
274
|
+
// => 'Elapsed: 1 hr, 30 min, 5 sec'
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
The `duration` formatter follows `Intl.DurationFormat` and expects a duration record object such as `{ hours: 1, minutes: 30 }`.
|
|
278
|
+
|
|
279
|
+
### Range examples
|
|
280
|
+
|
|
281
|
+
```js
|
|
282
|
+
msg.addDictionary({
|
|
283
|
+
en: {
|
|
284
|
+
translations: {
|
|
285
|
+
BUDGET: 'Budget: {{amount:budget}}',
|
|
286
|
+
EVENT: 'Event: {{period:schedule}}',
|
|
287
|
+
},
|
|
288
|
+
formatters: {
|
|
289
|
+
budget: {
|
|
290
|
+
format: 'numberRange',
|
|
291
|
+
options: { style: 'currency', currency: 'USD' },
|
|
292
|
+
},
|
|
293
|
+
schedule: {
|
|
294
|
+
format: 'dateTimeRange',
|
|
295
|
+
options: { month: 'short', day: 'numeric' },
|
|
296
|
+
},
|
|
297
|
+
},
|
|
298
|
+
},
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
msg.message('BUDGET', {
|
|
302
|
+
amount: { start: 1200, end: 3400 },
|
|
303
|
+
})
|
|
304
|
+
// => 'Budget: $1,200.00 - $3,400.00'
|
|
305
|
+
|
|
306
|
+
msg.message('EVENT', {
|
|
307
|
+
period: { start: '2026-03-23', end: '2026-03-25' },
|
|
308
|
+
})
|
|
309
|
+
// => 'Event: Mar 23-25'
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
The `numberRange` and `dateTimeRange` formatters expect an object with `{ start, end }`.
|
|
313
|
+
|
|
314
|
+
### Plural range example
|
|
315
|
+
|
|
316
|
+
```js
|
|
317
|
+
msg.addDictionary({
|
|
318
|
+
en: {
|
|
319
|
+
translations: {
|
|
320
|
+
LABEL: 'Recommended for {{countText}} {{count:ticketLabel}}',
|
|
321
|
+
},
|
|
322
|
+
formatters: {
|
|
323
|
+
ticketLabel: {
|
|
324
|
+
format: 'pluralRange',
|
|
325
|
+
rules: {
|
|
326
|
+
one: 'ticket',
|
|
327
|
+
other: 'tickets',
|
|
328
|
+
},
|
|
329
|
+
},
|
|
330
|
+
},
|
|
331
|
+
},
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
msg.message('LABEL', {
|
|
335
|
+
countText: '1-3',
|
|
336
|
+
count: { start: 1, end: 3 },
|
|
337
|
+
})
|
|
338
|
+
// => 'Recommended for 1-3 tickets'
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
The `pluralRange` formatter expects `{ start, end }` and uses `Intl.PluralRules.prototype.selectRange()`.
|
|
342
|
+
|
|
343
|
+
## Option validation
|
|
344
|
+
|
|
345
|
+
When the runtime supports `Intl.supportedValuesOf()`, the library validates commonly used Intl options before constructing formatters.
|
|
346
|
+
|
|
347
|
+
Currently validated where applicable:
|
|
348
|
+
|
|
349
|
+
- `currency`
|
|
350
|
+
- `unit`
|
|
351
|
+
- `calendar`
|
|
352
|
+
- `numberingSystem`
|
|
353
|
+
|
|
354
|
+
If an option is invalid, the formatter warns through the configured logger and falls back gracefully instead of relying only on a constructor exception.
|
|
355
|
+
|
|
356
|
+
## Parts-aware post-processing
|
|
357
|
+
|
|
358
|
+
Any formatter can optionally pass its result through one registered post-formatter as a second stage.
|
|
359
|
+
There is no recursive formatter pipeline: `format` runs first, then `postFormat` may run once.
|
|
360
|
+
|
|
361
|
+
Supported built-in formatters currently provide `parts` when available:
|
|
362
|
+
|
|
363
|
+
- `list`
|
|
364
|
+
- `number`
|
|
365
|
+
- `numberRange`
|
|
366
|
+
- `dateTime`
|
|
367
|
+
- `dateTimeRange`
|
|
368
|
+
- `relativeTime`
|
|
369
|
+
|
|
370
|
+
The post-formatter receives a context object including:
|
|
371
|
+
|
|
372
|
+
- `value`: the built-in formatter's default string result
|
|
373
|
+
- `parts`: the result of `formatToParts()` or `formatRangeToParts()` when supported
|
|
374
|
+
- `rawValue`: the original unformatted input value
|
|
375
|
+
- `format`: the built-in formatter name that ran first
|
|
376
|
+
|
|
377
|
+
For custom primary formatters, `postFormat` still works, but `parts` is only populated when the first stage formatter collected them.
|
|
378
|
+
|
|
379
|
+
Example:
|
|
380
|
+
|
|
381
|
+
```js
|
|
382
|
+
msg.registerFormatter('markCurrency', ({ value, parts }) => {
|
|
383
|
+
const currency = parts.find((part) => part.type === 'currency')?.value ?? ''
|
|
384
|
+
return `${value} [${currency}]`
|
|
385
|
+
})
|
|
386
|
+
|
|
387
|
+
msg.addDictionary({
|
|
388
|
+
en: {
|
|
389
|
+
translations: {
|
|
390
|
+
TOTAL: 'Total: {{amount:price}}',
|
|
391
|
+
},
|
|
392
|
+
formatters: {
|
|
393
|
+
price: {
|
|
394
|
+
format: 'number',
|
|
395
|
+
options: { style: 'currency', currency: 'USD' },
|
|
396
|
+
postFormat: 'markCurrency',
|
|
397
|
+
},
|
|
398
|
+
},
|
|
399
|
+
},
|
|
400
|
+
})
|
|
401
|
+
|
|
402
|
+
msg.message('TOTAL', { amount: 1234.5 })
|
|
403
|
+
// => 'Total: $1,234.50 [$]'
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
## Custom formatters
|
|
407
|
+
|
|
408
|
+
Register a formatter by name, then reference it from dictionary formatter definitions:
|
|
409
|
+
|
|
410
|
+
```js
|
|
411
|
+
msg.registerFormatter('capitalize', ({ value }) => {
|
|
412
|
+
const text = value == null ? '' : String(value)
|
|
413
|
+
return text ? text[0].toUpperCase() + text.slice(1).toLowerCase() : text
|
|
414
|
+
})
|
|
415
|
+
|
|
416
|
+
msg.addDictionary({
|
|
417
|
+
en: {
|
|
418
|
+
translations: {
|
|
419
|
+
TITLE: 'Welcome, {{name:titleCase}}.',
|
|
420
|
+
},
|
|
421
|
+
formatters: {
|
|
422
|
+
titleCase: {
|
|
423
|
+
format: 'capitalize',
|
|
424
|
+
},
|
|
425
|
+
},
|
|
426
|
+
},
|
|
427
|
+
})
|
|
428
|
+
|
|
429
|
+
msg.message('TITLE', { name: 'tAYLOR' })
|
|
430
|
+
// => 'Welcome, Taylor.'
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
Custom formatter callbacks receive a single config object. Common fields include:
|
|
434
|
+
|
|
435
|
+
- `locales`
|
|
436
|
+
- `value`
|
|
437
|
+
- `options`
|
|
438
|
+
- any additional formatter-specific properties from the dictionary config
|
|
439
|
+
|
|
440
|
+
## API
|
|
441
|
+
|
|
442
|
+
### `new IntlMsg(options?)`
|
|
443
|
+
|
|
444
|
+
Creates an instance.
|
|
445
|
+
|
|
446
|
+
Supported options:
|
|
447
|
+
|
|
448
|
+
- `log`
|
|
449
|
+
- `verbose`
|
|
450
|
+
- `intlPolyfill`
|
|
451
|
+
|
|
452
|
+
### `IntlMsg.factory(options?)`
|
|
453
|
+
|
|
454
|
+
Convenience constructor. In addition to the constructor options, it also accepts:
|
|
455
|
+
|
|
456
|
+
- `locales`
|
|
457
|
+
- `dictionaries`
|
|
458
|
+
|
|
459
|
+
### `addLocale(locales)`
|
|
460
|
+
|
|
461
|
+
Adds one locale or an array of locales.
|
|
462
|
+
|
|
463
|
+
### `setLocale(locales)`
|
|
464
|
+
|
|
465
|
+
Replaces the current locale list.
|
|
466
|
+
|
|
467
|
+
### `getLocale()`
|
|
468
|
+
|
|
469
|
+
Returns the current locale list.
|
|
470
|
+
|
|
471
|
+
### `addDictionary(dictionaryJson)`
|
|
472
|
+
|
|
473
|
+
Merges dictionary data into the current instance.
|
|
474
|
+
|
|
475
|
+
### `getDictionary(locale)`
|
|
476
|
+
|
|
477
|
+
Returns the `Dictionary` instance for a locale, or `null`.
|
|
478
|
+
|
|
479
|
+
### `getDictionaryNames()`
|
|
480
|
+
|
|
481
|
+
Returns the registered locale names.
|
|
482
|
+
|
|
483
|
+
### `addTermToDictionary(locale, key, value)`
|
|
484
|
+
|
|
485
|
+
Adds or replaces a translation term for a locale.
|
|
486
|
+
|
|
487
|
+
### `getTermFromDictionary(locale, key)`
|
|
488
|
+
|
|
489
|
+
Returns a term value, or `undefined`.
|
|
490
|
+
|
|
491
|
+
### `getRawMessage(key, locales?)`
|
|
492
|
+
|
|
493
|
+
Returns the untranslated template string selected by locale lookup.
|
|
494
|
+
|
|
495
|
+
### `message(key, values?)`
|
|
496
|
+
|
|
497
|
+
Formats and returns the final message string.
|
|
498
|
+
|
|
499
|
+
### `registerFormatter(name, fn)`
|
|
500
|
+
|
|
501
|
+
Registers a custom formatter callback.
|
|
502
|
+
|
|
503
|
+
Dictionary formatter configs may also set `postFormat` to the name of a registered formatter. Only two stages are supported: `format`, then `postFormat`.
|
|
504
|
+
|
|
505
|
+
## Development
|
|
506
|
+
|
|
507
|
+
Install dependencies and run tests:
|
|
508
|
+
|
|
509
|
+
```sh
|
|
510
|
+
npm test
|
|
511
|
+
```
|
|
512
|
+
|
|
513
|
+
Tests currently build the package first, then run Mocha with `nyc` coverage.
|
|
514
|
+
|
|
515
|
+
## Production use
|
|
516
|
+
|
|
517
|
+
`intl-msg` is usable in real applications today, especially when you want:
|
|
518
|
+
|
|
519
|
+
- partial dictionaries
|
|
520
|
+
- locale-aware formatting driven by translation data
|
|
521
|
+
- explicit fallback behavior
|
|
522
|
+
- application-controlled dictionary loading
|
|
523
|
+
|
|
524
|
+
It is a good fit when:
|
|
525
|
+
|
|
526
|
+
- your app can decide where dictionaries live
|
|
527
|
+
- you want to merge default dictionaries, language packs, and user overrides
|
|
528
|
+
- you want to stay close to native `Intl` behavior
|
|
529
|
+
|
|
530
|
+
Things to keep in mind:
|
|
531
|
+
|
|
532
|
+
- dictionary discovery is application-owned
|
|
533
|
+
- the optional `compose` and `loaders` helpers are intentionally small
|
|
534
|
+
- modern runtimes are assumed
|
|
535
|
+
- this is not an ICU MessageFormat replacement
|
|
536
|
+
|
|
537
|
+
For runtime language switching, prefer composing a fresh dictionary set and creating a fresh `IntlMsg` instance instead of mutating one long-lived instance in place.
|
|
538
|
+
|
|
539
|
+
## Optional composition helper
|
|
540
|
+
|
|
541
|
+
The package also provides an optional composition helper for building one merged dictionary from a priority plan:
|
|
542
|
+
|
|
543
|
+
```js
|
|
544
|
+
import composeDictionaries from '@eouia/intl-msg/compose'
|
|
545
|
+
|
|
546
|
+
const dictionaries = await composeDictionaries(
|
|
547
|
+
[
|
|
548
|
+
{ locale: 'en', source: 'default' },
|
|
549
|
+
{ locale: 'en-US', source: 'langpack' },
|
|
550
|
+
{ locale: 'en-CA', source: 'user' },
|
|
551
|
+
],
|
|
552
|
+
async ({ locale, source }) => {
|
|
553
|
+
// Application-specific loading logic goes here.
|
|
554
|
+
// Return a partial dictionary object or null.
|
|
555
|
+
}
|
|
556
|
+
)
|
|
557
|
+
```
|
|
558
|
+
|
|
559
|
+
This helper is intentionally small and optional. It does not replace the core `IntlMsg` API.
|
|
560
|
+
|
|
561
|
+
## Optional loader helpers
|
|
562
|
+
|
|
563
|
+
The package also provides optional strict helpers via `intl-msg/loaders`:
|
|
564
|
+
|
|
565
|
+
```js
|
|
566
|
+
import { createMemoryLoader, createFetchLoader, createPathLoader } from '@eouia/intl-msg/loaders'
|
|
567
|
+
```
|
|
568
|
+
|
|
569
|
+
These helpers are intentionally narrow:
|
|
570
|
+
|
|
571
|
+
- they work well for simple, conventional layouts
|
|
572
|
+
- they do not try to discover arbitrary custom dictionary locations
|
|
573
|
+
- applications can override URL/path resolution through callbacks
|
|
574
|
+
|
|
575
|
+
Examples:
|
|
576
|
+
|
|
577
|
+
```js
|
|
578
|
+
const memoryLoader = createMemoryLoader(registry)
|
|
579
|
+
|
|
580
|
+
const fetchLoader = createFetchLoader({
|
|
581
|
+
resolveUrl: ({ locale, source }) => `/dictionaries/${source}/${locale}.json`,
|
|
582
|
+
})
|
|
583
|
+
|
|
584
|
+
const pathLoader = createPathLoader({
|
|
585
|
+
resolvePath: ({ locale, source }) => `./dictionaries/${source}/${locale}.json`,
|
|
586
|
+
readFile: fs.promises.readFile,
|
|
587
|
+
})
|
|
588
|
+
```
|
|
589
|
+
|
|
590
|
+
Node.js path example:
|
|
591
|
+
|
|
592
|
+
```js
|
|
593
|
+
import IntlMsg from 'intl-msg'
|
|
594
|
+
import composeDictionaries from 'intl-msg/compose'
|
|
595
|
+
import { createPathLoader } from 'intl-msg/loaders'
|
|
596
|
+
import { readFile } from 'node:fs/promises'
|
|
597
|
+
|
|
598
|
+
const loader = createPathLoader({
|
|
599
|
+
readFile,
|
|
600
|
+
resolvePath: ({ locale, source }) =>
|
|
601
|
+
`${process.cwd()}/dictionaries/${source}/${locale}.json`,
|
|
602
|
+
})
|
|
603
|
+
|
|
604
|
+
const dictionaries = await composeDictionaries(
|
|
605
|
+
[{ locale: 'en-US', source: 'default' }],
|
|
606
|
+
loader
|
|
607
|
+
)
|
|
608
|
+
|
|
609
|
+
const msg = IntlMsg.factory({
|
|
610
|
+
locales: ['en-US', 'en'],
|
|
611
|
+
dictionaries,
|
|
612
|
+
})
|
|
613
|
+
|
|
614
|
+
console.log(msg.message('HELLO'))
|
|
615
|
+
```
|