@flare-engine/localization 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 +30 -0
- package/dist/index.d.mts +74 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.js +252 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +222 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Grzegorz Otto
|
|
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,30 @@
|
|
|
1
|
+
# @flare-engine/localization
|
|
2
|
+
|
|
3
|
+
String tables + locale negotiation + ICU-style plural / interpolation + hot-reload. `LocalizationManager.t(key, values?)` renders messages with active → fallback → raw-key fallthrough. `formatMessage` is stateless; supports `{var}` interpolation and `{count, plural, =0 {…} one {# x} other {# xs}}`. CLDR-inspired `pluralCategory` covers `en`, `pl`, `ru`, `fr`, `ar`, `ja/ko/zh`, etc.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
bun add @flare-engine/localization
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { LocalizationManager } from '@flare-engine/localization';
|
|
15
|
+
|
|
16
|
+
const lm = new LocalizationManager({
|
|
17
|
+
fallback: 'en',
|
|
18
|
+
tables: { en: enJson, pl: plJson },
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
lm.setLocale('pl');
|
|
22
|
+
lm.t('shop.bought', { count: 3 }); // -> "Kupiono 3 przedmioty"
|
|
23
|
+
lm.onChange.add((locale) => console.log('locale ->', locale));
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
See [API spec](../../docs/design/api-specs.md#flare-enginelocalization--7kb-raw) and [skill reference](../../.github/skills/flare-localization/SKILL.md).
|
|
27
|
+
|
|
28
|
+
## License
|
|
29
|
+
|
|
30
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Signal } from '@flare-engine/events';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* String table → `{ key: message }` per locale.
|
|
5
|
+
*
|
|
6
|
+
* Messages support `{var}` interpolation and ICU-style plural selectors:
|
|
7
|
+
* "You have {count, plural, =0 {no items} one {# item} other {# items}}."
|
|
8
|
+
*
|
|
9
|
+
* Keys may be dotted for namespacing; there's no nesting — the table is flat.
|
|
10
|
+
*/
|
|
11
|
+
type LocaleCode = string;
|
|
12
|
+
type MessageValue = string | number;
|
|
13
|
+
type MessageValues = Record<string, MessageValue>;
|
|
14
|
+
type StringTable = Record<string, string>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Minimal CLDR-inspired plural classifier. Supports a handful of locales
|
|
18
|
+
* natively — fall back to English rules if unknown. This is not a full ICU
|
|
19
|
+
* implementation; it covers the categories used by our string tables.
|
|
20
|
+
*/
|
|
21
|
+
type PluralCategory = 'zero' | 'one' | 'two' | 'few' | 'many' | 'other';
|
|
22
|
+
declare function pluralCategory(locale: LocaleCode, n: number): PluralCategory;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Minimal ICU MessageFormat-ish parser.
|
|
26
|
+
*
|
|
27
|
+
* Grammar:
|
|
28
|
+
* text := (chunk)*
|
|
29
|
+
* chunk := literal | '{' var '}' | '{' var ',' 'plural' ',' branches '}'
|
|
30
|
+
* branches := (selector '{' text '}')+
|
|
31
|
+
* selector := '=' number | identifier // one | few | many | other | zero | two
|
|
32
|
+
* number/ident literals inside a branch may reference '#' → the variable's number value.
|
|
33
|
+
*
|
|
34
|
+
* Nested plural blocks are not supported (not needed for our tables).
|
|
35
|
+
*/
|
|
36
|
+
declare function formatMessage(template: string, values: MessageValues | undefined, locale: LocaleCode): string;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Negotiate the best locale match from a prioritized list against a set of
|
|
40
|
+
* available locales. Matching is case-insensitive and considers language +
|
|
41
|
+
* region. Returns the fallback when nothing matches.
|
|
42
|
+
*/
|
|
43
|
+
declare function resolveLocale(requested: readonly LocaleCode[], available: readonly LocaleCode[], fallback: LocaleCode): LocaleCode;
|
|
44
|
+
|
|
45
|
+
interface LocalizationManagerOptions {
|
|
46
|
+
tables: Record<LocaleCode, StringTable>;
|
|
47
|
+
initialLocale: LocaleCode;
|
|
48
|
+
fallbackLocale: LocaleCode;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Owns the active locale + string tables. `t(key, values?)` renders a message
|
|
52
|
+
* using the active locale, falling back to the fallback locale, then the raw
|
|
53
|
+
* key. `onChange` fires whenever the active locale is swapped. `setTable`
|
|
54
|
+
* replaces a locale's table in place — enables hot-reload in dev.
|
|
55
|
+
*/
|
|
56
|
+
declare class LocalizationManager {
|
|
57
|
+
private _tables;
|
|
58
|
+
private _locale;
|
|
59
|
+
private readonly _fallback;
|
|
60
|
+
readonly onChange: Signal<string>;
|
|
61
|
+
constructor(options: LocalizationManagerOptions);
|
|
62
|
+
get locale(): LocaleCode;
|
|
63
|
+
get fallback(): LocaleCode;
|
|
64
|
+
availableLocales(): LocaleCode[];
|
|
65
|
+
setLocale(locale: LocaleCode): void;
|
|
66
|
+
/** Pick the best-matching locale from the given requests and make it active. */
|
|
67
|
+
negotiateLocale(requested: readonly LocaleCode[]): LocaleCode;
|
|
68
|
+
/** Replace (or install) a locale's table. Emits `onChange` if it targets the active locale. */
|
|
69
|
+
setTable(locale: LocaleCode, table: StringTable): void;
|
|
70
|
+
has(key: string): boolean;
|
|
71
|
+
t(key: string, values?: MessageValues): string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export { type LocaleCode, LocalizationManager, type LocalizationManagerOptions, type MessageValue, type MessageValues, type PluralCategory, type StringTable, formatMessage, pluralCategory, resolveLocale };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Signal } from '@flare-engine/events';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* String table → `{ key: message }` per locale.
|
|
5
|
+
*
|
|
6
|
+
* Messages support `{var}` interpolation and ICU-style plural selectors:
|
|
7
|
+
* "You have {count, plural, =0 {no items} one {# item} other {# items}}."
|
|
8
|
+
*
|
|
9
|
+
* Keys may be dotted for namespacing; there's no nesting — the table is flat.
|
|
10
|
+
*/
|
|
11
|
+
type LocaleCode = string;
|
|
12
|
+
type MessageValue = string | number;
|
|
13
|
+
type MessageValues = Record<string, MessageValue>;
|
|
14
|
+
type StringTable = Record<string, string>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Minimal CLDR-inspired plural classifier. Supports a handful of locales
|
|
18
|
+
* natively — fall back to English rules if unknown. This is not a full ICU
|
|
19
|
+
* implementation; it covers the categories used by our string tables.
|
|
20
|
+
*/
|
|
21
|
+
type PluralCategory = 'zero' | 'one' | 'two' | 'few' | 'many' | 'other';
|
|
22
|
+
declare function pluralCategory(locale: LocaleCode, n: number): PluralCategory;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Minimal ICU MessageFormat-ish parser.
|
|
26
|
+
*
|
|
27
|
+
* Grammar:
|
|
28
|
+
* text := (chunk)*
|
|
29
|
+
* chunk := literal | '{' var '}' | '{' var ',' 'plural' ',' branches '}'
|
|
30
|
+
* branches := (selector '{' text '}')+
|
|
31
|
+
* selector := '=' number | identifier // one | few | many | other | zero | two
|
|
32
|
+
* number/ident literals inside a branch may reference '#' → the variable's number value.
|
|
33
|
+
*
|
|
34
|
+
* Nested plural blocks are not supported (not needed for our tables).
|
|
35
|
+
*/
|
|
36
|
+
declare function formatMessage(template: string, values: MessageValues | undefined, locale: LocaleCode): string;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Negotiate the best locale match from a prioritized list against a set of
|
|
40
|
+
* available locales. Matching is case-insensitive and considers language +
|
|
41
|
+
* region. Returns the fallback when nothing matches.
|
|
42
|
+
*/
|
|
43
|
+
declare function resolveLocale(requested: readonly LocaleCode[], available: readonly LocaleCode[], fallback: LocaleCode): LocaleCode;
|
|
44
|
+
|
|
45
|
+
interface LocalizationManagerOptions {
|
|
46
|
+
tables: Record<LocaleCode, StringTable>;
|
|
47
|
+
initialLocale: LocaleCode;
|
|
48
|
+
fallbackLocale: LocaleCode;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Owns the active locale + string tables. `t(key, values?)` renders a message
|
|
52
|
+
* using the active locale, falling back to the fallback locale, then the raw
|
|
53
|
+
* key. `onChange` fires whenever the active locale is swapped. `setTable`
|
|
54
|
+
* replaces a locale's table in place — enables hot-reload in dev.
|
|
55
|
+
*/
|
|
56
|
+
declare class LocalizationManager {
|
|
57
|
+
private _tables;
|
|
58
|
+
private _locale;
|
|
59
|
+
private readonly _fallback;
|
|
60
|
+
readonly onChange: Signal<string>;
|
|
61
|
+
constructor(options: LocalizationManagerOptions);
|
|
62
|
+
get locale(): LocaleCode;
|
|
63
|
+
get fallback(): LocaleCode;
|
|
64
|
+
availableLocales(): LocaleCode[];
|
|
65
|
+
setLocale(locale: LocaleCode): void;
|
|
66
|
+
/** Pick the best-matching locale from the given requests and make it active. */
|
|
67
|
+
negotiateLocale(requested: readonly LocaleCode[]): LocaleCode;
|
|
68
|
+
/** Replace (or install) a locale's table. Emits `onChange` if it targets the active locale. */
|
|
69
|
+
setTable(locale: LocaleCode, table: StringTable): void;
|
|
70
|
+
has(key: string): boolean;
|
|
71
|
+
t(key: string, values?: MessageValues): string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export { type LocaleCode, LocalizationManager, type LocalizationManagerOptions, type MessageValue, type MessageValues, type PluralCategory, type StringTable, formatMessage, pluralCategory, resolveLocale };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
LocalizationManager: () => LocalizationManager,
|
|
24
|
+
formatMessage: () => formatMessage,
|
|
25
|
+
pluralCategory: () => pluralCategory,
|
|
26
|
+
resolveLocale: () => resolveLocale
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
|
|
30
|
+
// src/plurals.ts
|
|
31
|
+
var ENGLISH = (n) => n === 1 ? "one" : "other";
|
|
32
|
+
var POLISH = (n) => {
|
|
33
|
+
if (n === 1) return "one";
|
|
34
|
+
const mod10 = n % 10;
|
|
35
|
+
const mod100 = n % 100;
|
|
36
|
+
if (mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) return "few";
|
|
37
|
+
return "many";
|
|
38
|
+
};
|
|
39
|
+
var RUSSIAN = (n) => {
|
|
40
|
+
const mod10 = n % 10;
|
|
41
|
+
const mod100 = n % 100;
|
|
42
|
+
if (mod10 === 1 && mod100 !== 11) return "one";
|
|
43
|
+
if (mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) return "few";
|
|
44
|
+
if (mod10 === 0 || mod10 >= 5 && mod10 <= 9 || mod100 >= 11 && mod100 <= 14) return "many";
|
|
45
|
+
return "other";
|
|
46
|
+
};
|
|
47
|
+
var ARABIC = (n) => {
|
|
48
|
+
if (n === 0) return "zero";
|
|
49
|
+
if (n === 1) return "one";
|
|
50
|
+
if (n === 2) return "two";
|
|
51
|
+
const mod100 = n % 100;
|
|
52
|
+
if (mod100 >= 3 && mod100 <= 10) return "few";
|
|
53
|
+
if (mod100 >= 11 && mod100 <= 99) return "many";
|
|
54
|
+
return "other";
|
|
55
|
+
};
|
|
56
|
+
var JAPANESE = () => "other";
|
|
57
|
+
var RULES = {
|
|
58
|
+
en: ENGLISH,
|
|
59
|
+
de: ENGLISH,
|
|
60
|
+
es: ENGLISH,
|
|
61
|
+
fr: (n) => n === 0 || n === 1 ? "one" : "other",
|
|
62
|
+
it: ENGLISH,
|
|
63
|
+
pt: ENGLISH,
|
|
64
|
+
nl: ENGLISH,
|
|
65
|
+
pl: POLISH,
|
|
66
|
+
ru: RUSSIAN,
|
|
67
|
+
uk: RUSSIAN,
|
|
68
|
+
ar: ARABIC,
|
|
69
|
+
ja: JAPANESE,
|
|
70
|
+
ko: JAPANESE,
|
|
71
|
+
zh: JAPANESE
|
|
72
|
+
};
|
|
73
|
+
function pluralCategory(locale, n) {
|
|
74
|
+
const lang = locale.toLowerCase().split(/[-_]/)[0] ?? "en";
|
|
75
|
+
const rule = RULES[lang] ?? ENGLISH;
|
|
76
|
+
return rule(n);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/format.ts
|
|
80
|
+
function formatMessage(template, values, locale) {
|
|
81
|
+
const vals = values ?? {};
|
|
82
|
+
let out = "";
|
|
83
|
+
let i = 0;
|
|
84
|
+
const len = template.length;
|
|
85
|
+
while (i < len) {
|
|
86
|
+
const ch = template[i];
|
|
87
|
+
if (ch !== "{") {
|
|
88
|
+
out += ch;
|
|
89
|
+
i++;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
const endIdx = findMatchingClose(template, i);
|
|
93
|
+
if (endIdx === -1) {
|
|
94
|
+
out += template.slice(i);
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
const inner = template.slice(i + 1, endIdx);
|
|
98
|
+
out += renderPlaceholder(inner, vals, locale);
|
|
99
|
+
i = endIdx + 1;
|
|
100
|
+
}
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
function findMatchingClose(src, startBrace) {
|
|
104
|
+
let depth = 0;
|
|
105
|
+
for (let i = startBrace; i < src.length; i++) {
|
|
106
|
+
const c = src[i];
|
|
107
|
+
if (c === "{") depth++;
|
|
108
|
+
else if (c === "}") {
|
|
109
|
+
depth--;
|
|
110
|
+
if (depth === 0) return i;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return -1;
|
|
114
|
+
}
|
|
115
|
+
function renderPlaceholder(inner, values, locale) {
|
|
116
|
+
const commaIdx = topLevelComma(inner);
|
|
117
|
+
if (commaIdx === -1) {
|
|
118
|
+
return stringifyValue(values[inner.trim()]);
|
|
119
|
+
}
|
|
120
|
+
const name = inner.slice(0, commaIdx).trim();
|
|
121
|
+
const rest = inner.slice(commaIdx + 1).trim();
|
|
122
|
+
const secondComma = topLevelComma(rest);
|
|
123
|
+
if (secondComma === -1) return stringifyValue(values[name]);
|
|
124
|
+
const type = rest.slice(0, secondComma).trim();
|
|
125
|
+
const body = rest.slice(secondComma + 1).trim();
|
|
126
|
+
if (type !== "plural") {
|
|
127
|
+
return stringifyValue(values[name]);
|
|
128
|
+
}
|
|
129
|
+
const raw = values[name];
|
|
130
|
+
const n = typeof raw === "number" ? raw : Number(raw);
|
|
131
|
+
const category = Number.isFinite(n) ? pluralCategory(locale, n) : "other";
|
|
132
|
+
const branches = parsePluralBranches(body);
|
|
133
|
+
const branchText = branches[`=${Number.isFinite(n) ? n : ""}`] ?? branches[category] ?? branches.other ?? "";
|
|
134
|
+
const replaced = branchText.replace(/#/g, Number.isFinite(n) ? String(n) : "");
|
|
135
|
+
return formatMessage(replaced, values, locale);
|
|
136
|
+
}
|
|
137
|
+
function topLevelComma(src) {
|
|
138
|
+
let depth = 0;
|
|
139
|
+
for (let i = 0; i < src.length; i++) {
|
|
140
|
+
const c = src[i];
|
|
141
|
+
if (c === "{") depth++;
|
|
142
|
+
else if (c === "}") depth--;
|
|
143
|
+
else if (c === "," && depth === 0) return i;
|
|
144
|
+
}
|
|
145
|
+
return -1;
|
|
146
|
+
}
|
|
147
|
+
function parsePluralBranches(body) {
|
|
148
|
+
const out = {};
|
|
149
|
+
let i = 0;
|
|
150
|
+
while (i < body.length) {
|
|
151
|
+
while (i < body.length) {
|
|
152
|
+
const ch = body[i];
|
|
153
|
+
if (ch === " " || ch === " " || ch === "\n" || ch === "\r") i++;
|
|
154
|
+
else break;
|
|
155
|
+
}
|
|
156
|
+
if (i >= body.length) break;
|
|
157
|
+
const selStart = i;
|
|
158
|
+
while (i < body.length) {
|
|
159
|
+
const ch = body[i];
|
|
160
|
+
if (ch === " " || ch === " " || ch === "\n" || ch === "\r" || ch === "{") break;
|
|
161
|
+
i++;
|
|
162
|
+
}
|
|
163
|
+
const selector = body.slice(selStart, i);
|
|
164
|
+
while (i < body.length) {
|
|
165
|
+
const ch = body[i];
|
|
166
|
+
if (ch === " " || ch === " " || ch === "\n" || ch === "\r") i++;
|
|
167
|
+
else break;
|
|
168
|
+
}
|
|
169
|
+
if (body[i] !== "{") break;
|
|
170
|
+
const end = findMatchingClose(body, i);
|
|
171
|
+
if (end === -1) break;
|
|
172
|
+
out[selector] = body.slice(i + 1, end);
|
|
173
|
+
i = end + 1;
|
|
174
|
+
}
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
function stringifyValue(v) {
|
|
178
|
+
if (v === void 0 || v === null) return "";
|
|
179
|
+
return String(v);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// src/resolve.ts
|
|
183
|
+
function resolveLocale(requested, available, fallback) {
|
|
184
|
+
if (available.length === 0) return fallback;
|
|
185
|
+
const norm = (s) => s.toLowerCase().replace(/_/g, "-");
|
|
186
|
+
const avail = available.map(norm);
|
|
187
|
+
for (const rawReq of requested) {
|
|
188
|
+
const req = norm(rawReq);
|
|
189
|
+
const exactIdx = avail.indexOf(req);
|
|
190
|
+
if (exactIdx !== -1) return available[exactIdx];
|
|
191
|
+
const lang = req.split("-")[0] ?? req;
|
|
192
|
+
const langIdx = avail.findIndex((a) => (a.split("-")[0] ?? a) === lang);
|
|
193
|
+
if (langIdx !== -1) return available[langIdx];
|
|
194
|
+
}
|
|
195
|
+
const fbIdx = avail.indexOf(norm(fallback));
|
|
196
|
+
if (fbIdx !== -1) return available[fbIdx];
|
|
197
|
+
return available[0];
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// src/manager.ts
|
|
201
|
+
var import_events = require("@flare-engine/events");
|
|
202
|
+
var LocalizationManager = class {
|
|
203
|
+
_tables;
|
|
204
|
+
_locale;
|
|
205
|
+
_fallback;
|
|
206
|
+
onChange = new import_events.Signal();
|
|
207
|
+
constructor(options) {
|
|
208
|
+
this._tables = { ...options.tables };
|
|
209
|
+
this._fallback = options.fallbackLocale;
|
|
210
|
+
this._locale = options.initialLocale;
|
|
211
|
+
}
|
|
212
|
+
get locale() {
|
|
213
|
+
return this._locale;
|
|
214
|
+
}
|
|
215
|
+
get fallback() {
|
|
216
|
+
return this._fallback;
|
|
217
|
+
}
|
|
218
|
+
availableLocales() {
|
|
219
|
+
return Object.keys(this._tables);
|
|
220
|
+
}
|
|
221
|
+
setLocale(locale) {
|
|
222
|
+
if (this._locale === locale) return;
|
|
223
|
+
this._locale = locale;
|
|
224
|
+
this.onChange.emit(locale);
|
|
225
|
+
}
|
|
226
|
+
/** Pick the best-matching locale from the given requests and make it active. */
|
|
227
|
+
negotiateLocale(requested) {
|
|
228
|
+
const chosen = resolveLocale(requested, this.availableLocales(), this._fallback);
|
|
229
|
+
this.setLocale(chosen);
|
|
230
|
+
return chosen;
|
|
231
|
+
}
|
|
232
|
+
/** Replace (or install) a locale's table. Emits `onChange` if it targets the active locale. */
|
|
233
|
+
setTable(locale, table) {
|
|
234
|
+
this._tables[locale] = table;
|
|
235
|
+
if (locale === this._locale) this.onChange.emit(locale);
|
|
236
|
+
}
|
|
237
|
+
has(key) {
|
|
238
|
+
return this._tables[this._locale]?.[key] !== void 0 || this._tables[this._fallback]?.[key] !== void 0;
|
|
239
|
+
}
|
|
240
|
+
t(key, values) {
|
|
241
|
+
const template = this._tables[this._locale]?.[key] ?? this._tables[this._fallback]?.[key] ?? key;
|
|
242
|
+
return formatMessage(template, values, this._locale);
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
246
|
+
0 && (module.exports = {
|
|
247
|
+
LocalizationManager,
|
|
248
|
+
formatMessage,
|
|
249
|
+
pluralCategory,
|
|
250
|
+
resolveLocale
|
|
251
|
+
});
|
|
252
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/plurals.ts","../src/format.ts","../src/resolve.ts","../src/manager.ts"],"sourcesContent":["export type { LocaleCode, MessageValue, MessageValues, StringTable } from './types.js';\nexport { type PluralCategory, pluralCategory } from './plurals.js';\nexport { formatMessage } from './format.js';\nexport { resolveLocale } from './resolve.js';\nexport { LocalizationManager, type LocalizationManagerOptions } from './manager.js';\n","import type { LocaleCode } from './types.js';\n\n/**\n * Minimal CLDR-inspired plural classifier. Supports a handful of locales\n * natively — fall back to English rules if unknown. This is not a full ICU\n * implementation; it covers the categories used by our string tables.\n */\n\nexport type PluralCategory = 'zero' | 'one' | 'two' | 'few' | 'many' | 'other';\n\ntype PluralRule = (n: number) => PluralCategory;\n\nconst ENGLISH: PluralRule = (n) => (n === 1 ? 'one' : 'other');\n\nconst POLISH: PluralRule = (n) => {\n if (n === 1) return 'one';\n const mod10 = n % 10;\n const mod100 = n % 100;\n if (mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) return 'few';\n return 'many';\n};\n\nconst RUSSIAN: PluralRule = (n) => {\n const mod10 = n % 10;\n const mod100 = n % 100;\n if (mod10 === 1 && mod100 !== 11) return 'one';\n if (mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) return 'few';\n if (mod10 === 0 || (mod10 >= 5 && mod10 <= 9) || (mod100 >= 11 && mod100 <= 14)) return 'many';\n return 'other';\n};\n\nconst ARABIC: PluralRule = (n) => {\n if (n === 0) return 'zero';\n if (n === 1) return 'one';\n if (n === 2) return 'two';\n const mod100 = n % 100;\n if (mod100 >= 3 && mod100 <= 10) return 'few';\n if (mod100 >= 11 && mod100 <= 99) return 'many';\n return 'other';\n};\n\nconst JAPANESE: PluralRule = () => 'other';\n\nconst RULES: Record<string, PluralRule> = {\n en: ENGLISH,\n de: ENGLISH,\n es: ENGLISH,\n fr: (n) => (n === 0 || n === 1 ? 'one' : 'other'),\n it: ENGLISH,\n pt: ENGLISH,\n nl: ENGLISH,\n pl: POLISH,\n ru: RUSSIAN,\n uk: RUSSIAN,\n ar: ARABIC,\n ja: JAPANESE,\n ko: JAPANESE,\n zh: JAPANESE,\n};\n\nexport function pluralCategory(locale: LocaleCode, n: number): PluralCategory {\n const lang = locale.toLowerCase().split(/[-_]/)[0] ?? 'en';\n const rule = RULES[lang] ?? ENGLISH;\n return rule(n);\n}\n","import { pluralCategory } from './plurals.js';\nimport type { LocaleCode, MessageValues } from './types.js';\n\n/**\n * Minimal ICU MessageFormat-ish parser.\n *\n * Grammar:\n * text := (chunk)*\n * chunk := literal | '{' var '}' | '{' var ',' 'plural' ',' branches '}'\n * branches := (selector '{' text '}')+\n * selector := '=' number | identifier // one | few | many | other | zero | two\n * number/ident literals inside a branch may reference '#' → the variable's number value.\n *\n * Nested plural blocks are not supported (not needed for our tables).\n */\n\nexport function formatMessage(\n template: string,\n values: MessageValues | undefined,\n locale: LocaleCode,\n): string {\n const vals = values ?? {};\n let out = '';\n let i = 0;\n const len = template.length;\n while (i < len) {\n const ch = template[i];\n if (ch !== '{') {\n out += ch;\n i++;\n continue;\n }\n // Find the matching closing brace, respecting nested braces.\n const endIdx = findMatchingClose(template, i);\n if (endIdx === -1) {\n // Malformed — emit remainder literally.\n out += template.slice(i);\n break;\n }\n const inner = template.slice(i + 1, endIdx);\n out += renderPlaceholder(inner, vals, locale);\n i = endIdx + 1;\n }\n return out;\n}\n\nfunction findMatchingClose(src: string, startBrace: number): number {\n let depth = 0;\n for (let i = startBrace; i < src.length; i++) {\n const c = src[i];\n if (c === '{') depth++;\n else if (c === '}') {\n depth--;\n if (depth === 0) return i;\n }\n }\n return -1;\n}\n\nfunction renderPlaceholder(inner: string, values: MessageValues, locale: LocaleCode): string {\n // Split only on the top-level commas into at most 3 parts: name, type, body.\n const commaIdx = topLevelComma(inner);\n if (commaIdx === -1) {\n return stringifyValue(values[inner.trim()]);\n }\n const name = inner.slice(0, commaIdx).trim();\n const rest = inner.slice(commaIdx + 1).trim();\n const secondComma = topLevelComma(rest);\n if (secondComma === -1) return stringifyValue(values[name]);\n const type = rest.slice(0, secondComma).trim();\n const body = rest.slice(secondComma + 1).trim();\n if (type !== 'plural') {\n // Unknown formatter — fall back to raw substitution.\n return stringifyValue(values[name]);\n }\n const raw = values[name];\n const n = typeof raw === 'number' ? raw : Number(raw);\n const category = Number.isFinite(n) ? pluralCategory(locale, n) : 'other';\n const branches = parsePluralBranches(body);\n const branchText =\n branches[`=${Number.isFinite(n) ? n : ''}`] ?? branches[category] ?? branches.other ?? '';\n // '#' inside the branch refers to the numeric value.\n const replaced = branchText.replace(/#/g, Number.isFinite(n) ? String(n) : '');\n // Recurse so nested `{var}` inside the branch is resolved.\n return formatMessage(replaced, values, locale);\n}\n\nfunction topLevelComma(src: string): number {\n let depth = 0;\n for (let i = 0; i < src.length; i++) {\n const c = src[i];\n if (c === '{') depth++;\n else if (c === '}') depth--;\n else if (c === ',' && depth === 0) return i;\n }\n return -1;\n}\n\nfunction parsePluralBranches(body: string): Record<string, string> {\n const out: Record<string, string> = {};\n let i = 0;\n while (i < body.length) {\n // Skip whitespace.\n while (i < body.length) {\n const ch = body[i];\n if (ch === ' ' || ch === '\\t' || ch === '\\n' || ch === '\\r') i++;\n else break;\n }\n if (i >= body.length) break;\n // Read selector until whitespace or '{'.\n const selStart = i;\n while (i < body.length) {\n const ch = body[i];\n if (ch === ' ' || ch === '\\t' || ch === '\\n' || ch === '\\r' || ch === '{') break;\n i++;\n }\n const selector = body.slice(selStart, i);\n while (i < body.length) {\n const ch = body[i];\n if (ch === ' ' || ch === '\\t' || ch === '\\n' || ch === '\\r') i++;\n else break;\n }\n if (body[i] !== '{') break;\n const end = findMatchingClose(body, i);\n if (end === -1) break;\n out[selector] = body.slice(i + 1, end);\n i = end + 1;\n }\n return out;\n}\n\nfunction stringifyValue(v: unknown): string {\n if (v === undefined || v === null) return '';\n return String(v);\n}\n","import type { LocaleCode } from './types.js';\n\n/**\n * Negotiate the best locale match from a prioritized list against a set of\n * available locales. Matching is case-insensitive and considers language +\n * region. Returns the fallback when nothing matches.\n */\nexport function resolveLocale(\n requested: readonly LocaleCode[],\n available: readonly LocaleCode[],\n fallback: LocaleCode,\n): LocaleCode {\n if (available.length === 0) return fallback;\n const norm = (s: string): string => s.toLowerCase().replace(/_/g, '-');\n const avail = available.map(norm);\n\n for (const rawReq of requested) {\n const req = norm(rawReq);\n // Exact match.\n const exactIdx = avail.indexOf(req);\n if (exactIdx !== -1) return available[exactIdx] as LocaleCode;\n // Language-only match against any region variant.\n const lang = req.split('-')[0] ?? req;\n const langIdx = avail.findIndex((a) => (a.split('-')[0] ?? a) === lang);\n if (langIdx !== -1) return available[langIdx] as LocaleCode;\n }\n // Fallback — ensure it is actually present; otherwise first available.\n const fbIdx = avail.indexOf(norm(fallback));\n if (fbIdx !== -1) return available[fbIdx] as LocaleCode;\n return available[0] as LocaleCode;\n}\n","import { Signal } from '@flare-engine/events';\nimport { formatMessage } from './format.js';\nimport { resolveLocale } from './resolve.js';\nimport type { LocaleCode, MessageValues, StringTable } from './types.js';\n\nexport interface LocalizationManagerOptions {\n tables: Record<LocaleCode, StringTable>;\n initialLocale: LocaleCode;\n fallbackLocale: LocaleCode;\n}\n\n/**\n * Owns the active locale + string tables. `t(key, values?)` renders a message\n * using the active locale, falling back to the fallback locale, then the raw\n * key. `onChange` fires whenever the active locale is swapped. `setTable`\n * replaces a locale's table in place — enables hot-reload in dev.\n */\nexport class LocalizationManager {\n private _tables: Record<LocaleCode, StringTable>;\n private _locale: LocaleCode;\n private readonly _fallback: LocaleCode;\n readonly onChange = new Signal<LocaleCode>();\n\n constructor(options: LocalizationManagerOptions) {\n this._tables = { ...options.tables };\n this._fallback = options.fallbackLocale;\n this._locale = options.initialLocale;\n }\n\n get locale(): LocaleCode {\n return this._locale;\n }\n\n get fallback(): LocaleCode {\n return this._fallback;\n }\n\n availableLocales(): LocaleCode[] {\n return Object.keys(this._tables);\n }\n\n setLocale(locale: LocaleCode): void {\n if (this._locale === locale) return;\n this._locale = locale;\n this.onChange.emit(locale);\n }\n\n /** Pick the best-matching locale from the given requests and make it active. */\n negotiateLocale(requested: readonly LocaleCode[]): LocaleCode {\n const chosen = resolveLocale(requested, this.availableLocales(), this._fallback);\n this.setLocale(chosen);\n return chosen;\n }\n\n /** Replace (or install) a locale's table. Emits `onChange` if it targets the active locale. */\n setTable(locale: LocaleCode, table: StringTable): void {\n this._tables[locale] = table;\n if (locale === this._locale) this.onChange.emit(locale);\n }\n\n has(key: string): boolean {\n return (\n this._tables[this._locale]?.[key] !== undefined ||\n this._tables[this._fallback]?.[key] !== undefined\n );\n }\n\n t(key: string, values?: MessageValues): string {\n const template =\n this._tables[this._locale]?.[key] ?? this._tables[this._fallback]?.[key] ?? key;\n return formatMessage(template, values, this._locale);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACYA,IAAM,UAAsB,CAAC,MAAO,MAAM,IAAI,QAAQ;AAEtD,IAAM,SAAqB,CAAC,MAAM;AAChC,MAAI,MAAM,EAAG,QAAO;AACpB,QAAM,QAAQ,IAAI;AAClB,QAAM,SAAS,IAAI;AACnB,MAAI,SAAS,KAAK,SAAS,MAAM,SAAS,MAAM,SAAS,IAAK,QAAO;AACrE,SAAO;AACT;AAEA,IAAM,UAAsB,CAAC,MAAM;AACjC,QAAM,QAAQ,IAAI;AAClB,QAAM,SAAS,IAAI;AACnB,MAAI,UAAU,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YAAY,OAAQ;AAC7B,SAAK,UAAU;AACf,SAAK,SAAS,KAAK,MAAM;AAAA,EAC3B;AAAA;AAAA,EAGA,gBAAgB,WAA8C;AAC5D,UAAM,SAAS,cAAc,WAAW,KAAK,iBAAiB,GAAG,KAAK,SAAS;AAC/E,SAAK,UAAU,MAAM;AACrB,WAAO;AAAA,EACT;AAAA;AAAA,EAGA,SAAS,QAAoB,OAA0B;AACrD,SAAK,QAAQ,MAAM,IAAI;AACvB,QAAI,WAAW,KAAK,QAAS,MAAK,SAAS,KAAK,MAAM;AAAA,EACxD;AAAA,EAEA,IAAI,KAAsB;AACxB,WACE,KAAK,QAAQ,KAAK,OAAO,IAAI,GAAG,MAAM,UACtC,KAAK,QAAQ,KAAK,SAAS,IAAI,GAAG,MAAM;AAAA,EAE5C;AAAA,EAEA,EAAE,KAAa,QAAgC;AAC7C,UAAM,WACJ,KAAK,QAAQ,KAAK,OAAO,IAAI,GAAG,KAAK,KAAK,QAAQ,KAAK,SAAS,IAAI,GAAG,KAAK;AAC9E,WAAO,cAAc,UAAU,QAAQ,KAAK,OAAO;AAAA,EACrD;AACF;","names":[]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
// src/plurals.ts
|
|
2
|
+
var ENGLISH = (n) => n === 1 ? "one" : "other";
|
|
3
|
+
var POLISH = (n) => {
|
|
4
|
+
if (n === 1) return "one";
|
|
5
|
+
const mod10 = n % 10;
|
|
6
|
+
const mod100 = n % 100;
|
|
7
|
+
if (mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) return "few";
|
|
8
|
+
return "many";
|
|
9
|
+
};
|
|
10
|
+
var RUSSIAN = (n) => {
|
|
11
|
+
const mod10 = n % 10;
|
|
12
|
+
const mod100 = n % 100;
|
|
13
|
+
if (mod10 === 1 && mod100 !== 11) return "one";
|
|
14
|
+
if (mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) return "few";
|
|
15
|
+
if (mod10 === 0 || mod10 >= 5 && mod10 <= 9 || mod100 >= 11 && mod100 <= 14) return "many";
|
|
16
|
+
return "other";
|
|
17
|
+
};
|
|
18
|
+
var ARABIC = (n) => {
|
|
19
|
+
if (n === 0) return "zero";
|
|
20
|
+
if (n === 1) return "one";
|
|
21
|
+
if (n === 2) return "two";
|
|
22
|
+
const mod100 = n % 100;
|
|
23
|
+
if (mod100 >= 3 && mod100 <= 10) return "few";
|
|
24
|
+
if (mod100 >= 11 && mod100 <= 99) return "many";
|
|
25
|
+
return "other";
|
|
26
|
+
};
|
|
27
|
+
var JAPANESE = () => "other";
|
|
28
|
+
var RULES = {
|
|
29
|
+
en: ENGLISH,
|
|
30
|
+
de: ENGLISH,
|
|
31
|
+
es: ENGLISH,
|
|
32
|
+
fr: (n) => n === 0 || n === 1 ? "one" : "other",
|
|
33
|
+
it: ENGLISH,
|
|
34
|
+
pt: ENGLISH,
|
|
35
|
+
nl: ENGLISH,
|
|
36
|
+
pl: POLISH,
|
|
37
|
+
ru: RUSSIAN,
|
|
38
|
+
uk: RUSSIAN,
|
|
39
|
+
ar: ARABIC,
|
|
40
|
+
ja: JAPANESE,
|
|
41
|
+
ko: JAPANESE,
|
|
42
|
+
zh: JAPANESE
|
|
43
|
+
};
|
|
44
|
+
function pluralCategory(locale, n) {
|
|
45
|
+
const lang = locale.toLowerCase().split(/[-_]/)[0] ?? "en";
|
|
46
|
+
const rule = RULES[lang] ?? ENGLISH;
|
|
47
|
+
return rule(n);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/format.ts
|
|
51
|
+
function formatMessage(template, values, locale) {
|
|
52
|
+
const vals = values ?? {};
|
|
53
|
+
let out = "";
|
|
54
|
+
let i = 0;
|
|
55
|
+
const len = template.length;
|
|
56
|
+
while (i < len) {
|
|
57
|
+
const ch = template[i];
|
|
58
|
+
if (ch !== "{") {
|
|
59
|
+
out += ch;
|
|
60
|
+
i++;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const endIdx = findMatchingClose(template, i);
|
|
64
|
+
if (endIdx === -1) {
|
|
65
|
+
out += template.slice(i);
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
const inner = template.slice(i + 1, endIdx);
|
|
69
|
+
out += renderPlaceholder(inner, vals, locale);
|
|
70
|
+
i = endIdx + 1;
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
function findMatchingClose(src, startBrace) {
|
|
75
|
+
let depth = 0;
|
|
76
|
+
for (let i = startBrace; i < src.length; i++) {
|
|
77
|
+
const c = src[i];
|
|
78
|
+
if (c === "{") depth++;
|
|
79
|
+
else if (c === "}") {
|
|
80
|
+
depth--;
|
|
81
|
+
if (depth === 0) return i;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return -1;
|
|
85
|
+
}
|
|
86
|
+
function renderPlaceholder(inner, values, locale) {
|
|
87
|
+
const commaIdx = topLevelComma(inner);
|
|
88
|
+
if (commaIdx === -1) {
|
|
89
|
+
return stringifyValue(values[inner.trim()]);
|
|
90
|
+
}
|
|
91
|
+
const name = inner.slice(0, commaIdx).trim();
|
|
92
|
+
const rest = inner.slice(commaIdx + 1).trim();
|
|
93
|
+
const secondComma = topLevelComma(rest);
|
|
94
|
+
if (secondComma === -1) return stringifyValue(values[name]);
|
|
95
|
+
const type = rest.slice(0, secondComma).trim();
|
|
96
|
+
const body = rest.slice(secondComma + 1).trim();
|
|
97
|
+
if (type !== "plural") {
|
|
98
|
+
return stringifyValue(values[name]);
|
|
99
|
+
}
|
|
100
|
+
const raw = values[name];
|
|
101
|
+
const n = typeof raw === "number" ? raw : Number(raw);
|
|
102
|
+
const category = Number.isFinite(n) ? pluralCategory(locale, n) : "other";
|
|
103
|
+
const branches = parsePluralBranches(body);
|
|
104
|
+
const branchText = branches[`=${Number.isFinite(n) ? n : ""}`] ?? branches[category] ?? branches.other ?? "";
|
|
105
|
+
const replaced = branchText.replace(/#/g, Number.isFinite(n) ? String(n) : "");
|
|
106
|
+
return formatMessage(replaced, values, locale);
|
|
107
|
+
}
|
|
108
|
+
function topLevelComma(src) {
|
|
109
|
+
let depth = 0;
|
|
110
|
+
for (let i = 0; i < src.length; i++) {
|
|
111
|
+
const c = src[i];
|
|
112
|
+
if (c === "{") depth++;
|
|
113
|
+
else if (c === "}") depth--;
|
|
114
|
+
else if (c === "," && depth === 0) return i;
|
|
115
|
+
}
|
|
116
|
+
return -1;
|
|
117
|
+
}
|
|
118
|
+
function parsePluralBranches(body) {
|
|
119
|
+
const out = {};
|
|
120
|
+
let i = 0;
|
|
121
|
+
while (i < body.length) {
|
|
122
|
+
while (i < body.length) {
|
|
123
|
+
const ch = body[i];
|
|
124
|
+
if (ch === " " || ch === " " || ch === "\n" || ch === "\r") i++;
|
|
125
|
+
else break;
|
|
126
|
+
}
|
|
127
|
+
if (i >= body.length) break;
|
|
128
|
+
const selStart = i;
|
|
129
|
+
while (i < body.length) {
|
|
130
|
+
const ch = body[i];
|
|
131
|
+
if (ch === " " || ch === " " || ch === "\n" || ch === "\r" || ch === "{") break;
|
|
132
|
+
i++;
|
|
133
|
+
}
|
|
134
|
+
const selector = body.slice(selStart, i);
|
|
135
|
+
while (i < body.length) {
|
|
136
|
+
const ch = body[i];
|
|
137
|
+
if (ch === " " || ch === " " || ch === "\n" || ch === "\r") i++;
|
|
138
|
+
else break;
|
|
139
|
+
}
|
|
140
|
+
if (body[i] !== "{") break;
|
|
141
|
+
const end = findMatchingClose(body, i);
|
|
142
|
+
if (end === -1) break;
|
|
143
|
+
out[selector] = body.slice(i + 1, end);
|
|
144
|
+
i = end + 1;
|
|
145
|
+
}
|
|
146
|
+
return out;
|
|
147
|
+
}
|
|
148
|
+
function stringifyValue(v) {
|
|
149
|
+
if (v === void 0 || v === null) return "";
|
|
150
|
+
return String(v);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// src/resolve.ts
|
|
154
|
+
function resolveLocale(requested, available, fallback) {
|
|
155
|
+
if (available.length === 0) return fallback;
|
|
156
|
+
const norm = (s) => s.toLowerCase().replace(/_/g, "-");
|
|
157
|
+
const avail = available.map(norm);
|
|
158
|
+
for (const rawReq of requested) {
|
|
159
|
+
const req = norm(rawReq);
|
|
160
|
+
const exactIdx = avail.indexOf(req);
|
|
161
|
+
if (exactIdx !== -1) return available[exactIdx];
|
|
162
|
+
const lang = req.split("-")[0] ?? req;
|
|
163
|
+
const langIdx = avail.findIndex((a) => (a.split("-")[0] ?? a) === lang);
|
|
164
|
+
if (langIdx !== -1) return available[langIdx];
|
|
165
|
+
}
|
|
166
|
+
const fbIdx = avail.indexOf(norm(fallback));
|
|
167
|
+
if (fbIdx !== -1) return available[fbIdx];
|
|
168
|
+
return available[0];
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// src/manager.ts
|
|
172
|
+
import { Signal } from "@flare-engine/events";
|
|
173
|
+
var LocalizationManager = class {
|
|
174
|
+
_tables;
|
|
175
|
+
_locale;
|
|
176
|
+
_fallback;
|
|
177
|
+
onChange = new Signal();
|
|
178
|
+
constructor(options) {
|
|
179
|
+
this._tables = { ...options.tables };
|
|
180
|
+
this._fallback = options.fallbackLocale;
|
|
181
|
+
this._locale = options.initialLocale;
|
|
182
|
+
}
|
|
183
|
+
get locale() {
|
|
184
|
+
return this._locale;
|
|
185
|
+
}
|
|
186
|
+
get fallback() {
|
|
187
|
+
return this._fallback;
|
|
188
|
+
}
|
|
189
|
+
availableLocales() {
|
|
190
|
+
return Object.keys(this._tables);
|
|
191
|
+
}
|
|
192
|
+
setLocale(locale) {
|
|
193
|
+
if (this._locale === locale) return;
|
|
194
|
+
this._locale = locale;
|
|
195
|
+
this.onChange.emit(locale);
|
|
196
|
+
}
|
|
197
|
+
/** Pick the best-matching locale from the given requests and make it active. */
|
|
198
|
+
negotiateLocale(requested) {
|
|
199
|
+
const chosen = resolveLocale(requested, this.availableLocales(), this._fallback);
|
|
200
|
+
this.setLocale(chosen);
|
|
201
|
+
return chosen;
|
|
202
|
+
}
|
|
203
|
+
/** Replace (or install) a locale's table. Emits `onChange` if it targets the active locale. */
|
|
204
|
+
setTable(locale, table) {
|
|
205
|
+
this._tables[locale] = table;
|
|
206
|
+
if (locale === this._locale) this.onChange.emit(locale);
|
|
207
|
+
}
|
|
208
|
+
has(key) {
|
|
209
|
+
return this._tables[this._locale]?.[key] !== void 0 || this._tables[this._fallback]?.[key] !== void 0;
|
|
210
|
+
}
|
|
211
|
+
t(key, values) {
|
|
212
|
+
const template = this._tables[this._locale]?.[key] ?? this._tables[this._fallback]?.[key] ?? key;
|
|
213
|
+
return formatMessage(template, values, this._locale);
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
export {
|
|
217
|
+
LocalizationManager,
|
|
218
|
+
formatMessage,
|
|
219
|
+
pluralCategory,
|
|
220
|
+
resolveLocale
|
|
221
|
+
};
|
|
222
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/plurals.ts","../src/format.ts","../src/resolve.ts","../src/manager.ts"],"sourcesContent":["import type { LocaleCode } from './types.js';\n\n/**\n * Minimal CLDR-inspired plural classifier. Supports a handful of locales\n * natively — fall back to English rules if unknown. This is not a full ICU\n * implementation; it covers the categories used by our string tables.\n */\n\nexport type PluralCategory = 'zero' | 'one' | 'two' | 'few' | 'many' | 'other';\n\ntype PluralRule = (n: number) => PluralCategory;\n\nconst ENGLISH: PluralRule = (n) => (n === 1 ? 'one' : 'other');\n\nconst POLISH: PluralRule = (n) => {\n if (n === 1) return 'one';\n const mod10 = n % 10;\n const mod100 = n % 100;\n if (mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) return 'few';\n return 'many';\n};\n\nconst RUSSIAN: PluralRule = (n) => {\n const mod10 = n % 10;\n const mod100 = n % 100;\n if (mod10 === 1 && mod100 !== 11) return 'one';\n if (mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) return 'few';\n if (mod10 === 0 || (mod10 >= 5 && mod10 <= 9) || (mod100 >= 11 && mod100 <= 14)) return 'many';\n return 'other';\n};\n\nconst ARABIC: PluralRule = (n) => {\n if (n === 0) return 'zero';\n if (n === 1) return 'one';\n if (n === 2) return 'two';\n const mod100 = n % 100;\n if (mod100 >= 3 && mod100 <= 10) return 'few';\n if (mod100 >= 11 && mod100 <= 99) return 'many';\n return 'other';\n};\n\nconst JAPANESE: PluralRule = () => 'other';\n\nconst RULES: Record<string, PluralRule> = {\n en: ENGLISH,\n de: ENGLISH,\n es: ENGLISH,\n fr: (n) => (n === 0 || n === 1 ? 'one' : 'other'),\n it: ENGLISH,\n pt: ENGLISH,\n nl: ENGLISH,\n pl: POLISH,\n ru: RUSSIAN,\n uk: RUSSIAN,\n ar: ARABIC,\n ja: JAPANESE,\n ko: JAPANESE,\n zh: JAPANESE,\n};\n\nexport function pluralCategory(locale: LocaleCode, n: number): PluralCategory {\n const lang = locale.toLowerCase().split(/[-_]/)[0] ?? 'en';\n const rule = RULES[lang] ?? ENGLISH;\n return rule(n);\n}\n","import { pluralCategory } from './plurals.js';\nimport type { LocaleCode, MessageValues } from './types.js';\n\n/**\n * Minimal ICU MessageFormat-ish parser.\n *\n * Grammar:\n * text := (chunk)*\n * chunk := literal | '{' var '}' | '{' var ',' 'plural' ',' branches '}'\n * branches := (selector '{' text '}')+\n * selector := '=' number | identifier // one | few | many | other | zero | two\n * number/ident literals inside a branch may reference '#' → the variable's number value.\n *\n * Nested plural blocks are not supported (not needed for our tables).\n */\n\nexport function formatMessage(\n template: string,\n values: MessageValues | undefined,\n locale: LocaleCode,\n): string {\n const vals = values ?? {};\n let out = '';\n let i = 0;\n const len = template.length;\n while (i < len) {\n const ch = template[i];\n if (ch !== '{') {\n out += ch;\n i++;\n continue;\n }\n // Find the matching closing brace, respecting nested braces.\n const endIdx = findMatchingClose(template, i);\n if (endIdx === -1) {\n // Malformed — emit remainder literally.\n out += template.slice(i);\n break;\n }\n const inner = template.slice(i + 1, endIdx);\n out += renderPlaceholder(inner, vals, locale);\n i = endIdx + 1;\n }\n return out;\n}\n\nfunction findMatchingClose(src: string, startBrace: number): number {\n let depth = 0;\n for (let i = startBrace; i < src.length; i++) {\n const c = src[i];\n if (c === '{') depth++;\n else if (c === '}') {\n depth--;\n if (depth === 0) return i;\n }\n }\n return -1;\n}\n\nfunction renderPlaceholder(inner: string, values: MessageValues, locale: LocaleCode): string {\n // Split only on the top-level commas into at most 3 parts: name, type, body.\n const commaIdx = topLevelComma(inner);\n if (commaIdx === -1) {\n return stringifyValue(values[inner.trim()]);\n }\n const name = inner.slice(0, commaIdx).trim();\n const rest = inner.slice(commaIdx + 1).trim();\n const secondComma = topLevelComma(rest);\n if (secondComma === -1) return stringifyValue(values[name]);\n const type = rest.slice(0, secondComma).trim();\n const body = rest.slice(secondComma + 1).trim();\n if (type !== 'plural') {\n // Unknown formatter — fall back to raw substitution.\n return stringifyValue(values[name]);\n }\n const raw = values[name];\n const n = typeof raw === 'number' ? raw : Number(raw);\n const category = Number.isFinite(n) ? pluralCategory(locale, n) : 'other';\n const branches = parsePluralBranches(body);\n const branchText =\n branches[`=${Number.isFinite(n) ? n : ''}`] ?? branches[category] ?? branches.other ?? '';\n // '#' inside the branch refers to the numeric value.\n const replaced = branchText.replace(/#/g, Number.isFinite(n) ? String(n) : '');\n // Recurse so nested `{var}` inside the branch is resolved.\n return formatMessage(replaced, values, locale);\n}\n\nfunction topLevelComma(src: string): number {\n let depth = 0;\n for (let i = 0; i < src.length; i++) {\n const c = src[i];\n if (c === '{') depth++;\n else if (c === '}') depth--;\n else if (c === ',' && depth === 0) return i;\n }\n return -1;\n}\n\nfunction parsePluralBranches(body: string): Record<string, string> {\n const out: Record<string, string> = {};\n let i = 0;\n while (i < body.length) {\n // Skip whitespace.\n while (i < body.length) {\n const ch = body[i];\n if (ch === ' ' || ch === '\\t' || ch === '\\n' || ch === '\\r') i++;\n else break;\n }\n if (i >= body.length) break;\n // Read selector until whitespace or '{'.\n const selStart = i;\n while (i < body.length) {\n const ch = body[i];\n if (ch === ' ' || ch === '\\t' || ch === '\\n' || ch === '\\r' || ch === '{') break;\n i++;\n }\n const selector = body.slice(selStart, i);\n while (i < body.length) {\n const ch = body[i];\n if (ch === ' ' || ch === '\\t' || ch === '\\n' || ch === '\\r') i++;\n else break;\n }\n if (body[i] !== '{') break;\n const end = findMatchingClose(body, i);\n if (end === -1) break;\n out[selector] = body.slice(i + 1, end);\n i = end + 1;\n }\n return out;\n}\n\nfunction stringifyValue(v: unknown): string {\n if (v === undefined || v === null) return '';\n return String(v);\n}\n","import type { LocaleCode } from './types.js';\n\n/**\n * Negotiate the best locale match from a prioritized list against a set of\n * available locales. Matching is case-insensitive and considers language +\n * region. Returns the fallback when nothing matches.\n */\nexport function resolveLocale(\n requested: readonly LocaleCode[],\n available: readonly LocaleCode[],\n fallback: LocaleCode,\n): LocaleCode {\n if (available.length === 0) return fallback;\n const norm = (s: string): string => s.toLowerCase().replace(/_/g, '-');\n const avail = available.map(norm);\n\n for (const rawReq of requested) {\n const req = norm(rawReq);\n // Exact match.\n const exactIdx = avail.indexOf(req);\n if (exactIdx !== -1) return available[exactIdx] as LocaleCode;\n // Language-only match against any region variant.\n const lang = req.split('-')[0] ?? req;\n const langIdx = avail.findIndex((a) => (a.split('-')[0] ?? a) === lang);\n if (langIdx !== -1) return available[langIdx] as LocaleCode;\n }\n // Fallback — ensure it is actually present; otherwise first available.\n const fbIdx = avail.indexOf(norm(fallback));\n if (fbIdx !== -1) return available[fbIdx] as LocaleCode;\n return available[0] as LocaleCode;\n}\n","import { Signal } from '@flare-engine/events';\nimport { formatMessage } from './format.js';\nimport { resolveLocale } from './resolve.js';\nimport type { LocaleCode, MessageValues, StringTable } from './types.js';\n\nexport interface LocalizationManagerOptions {\n tables: Record<LocaleCode, StringTable>;\n initialLocale: LocaleCode;\n fallbackLocale: LocaleCode;\n}\n\n/**\n * Owns the active locale + string tables. `t(key, values?)` renders a message\n * using the active locale, falling back to the fallback locale, then the raw\n * key. `onChange` fires whenever the active locale is swapped. `setTable`\n * replaces a locale's table in place — enables hot-reload in dev.\n */\nexport class LocalizationManager {\n private _tables: Record<LocaleCode, StringTable>;\n private _locale: LocaleCode;\n private readonly _fallback: LocaleCode;\n readonly onChange = new Signal<LocaleCode>();\n\n constructor(options: LocalizationManagerOptions) {\n this._tables = { ...options.tables };\n this._fallback = options.fallbackLocale;\n this._locale = options.initialLocale;\n }\n\n get locale(): LocaleCode {\n return this._locale;\n }\n\n get fallback(): LocaleCode {\n return this._fallback;\n }\n\n availableLocales(): LocaleCode[] {\n return Object.keys(this._tables);\n }\n\n setLocale(locale: LocaleCode): void {\n if (this._locale === locale) return;\n this._locale = locale;\n this.onChange.emit(locale);\n }\n\n /** Pick the best-matching locale from the given requests and make it active. */\n negotiateLocale(requested: readonly LocaleCode[]): LocaleCode {\n const chosen = resolveLocale(requested, this.availableLocales(), this._fallback);\n this.setLocale(chosen);\n return chosen;\n }\n\n /** Replace (or install) a locale's table. Emits `onChange` if it targets the active locale. */\n setTable(locale: LocaleCode, table: StringTable): void {\n this._tables[locale] = table;\n if (locale === this._locale) this.onChange.emit(locale);\n }\n\n has(key: string): boolean {\n return (\n this._tables[this._locale]?.[key] !== undefined ||\n this._tables[this._fallback]?.[key] !== undefined\n );\n }\n\n t(key: string, values?: MessageValues): string {\n const template =\n this._tables[this._locale]?.[key] ?? this._tables[this._fallback]?.[key] ?? key;\n return formatMessage(template, values, this._locale);\n }\n}\n"],"mappings":";AAYA,IAAM,UAAsB,CAAC,MAAO,MAAM,IAAI,QAAQ;AAEtD,IAAM,SAAqB,CAAC,MAAM;AAChC,MAAI,MAAM,EAAG,QAAO;AACpB,QAAM,QAAQ,IAAI;AAClB,QAAM,SAAS,IAAI;AACnB,MAAI,SAAS,KAAK,SAAS,MAAM,SAAS,MAAM,SAAS,IAAK,QAAO;AACrE,SAAO;AACT;AAEA,IAAM,UAAsB,CAAC,MAAM;AACjC,QAAM,QAAQ,IAAI;AAClB,QAAM,SAA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AU,QAA0B;AAClC,QAAI,KAAK,YAAY,OAAQ;AAC7B,SAAK,UAAU;AACf,SAAK,SAAS,KAAK,MAAM;AAAA,EAC3B;AAAA;AAAA,EAGA,gBAAgB,WAA8C;AAC5D,UAAM,SAAS,cAAc,WAAW,KAAK,iBAAiB,GAAG,KAAK,SAAS;AAC/E,SAAK,UAAU,MAAM;AACrB,WAAO;AAAA,EACT;AAAA;AAAA,EAGA,SAAS,QAAoB,OAA0B;AACrD,SAAK,QAAQ,MAAM,IAAI;AACvB,QAAI,WAAW,KAAK,QAAS,MAAK,SAAS,KAAK,MAAM;AAAA,EACxD;AAAA,EAEA,IAAI,KAAsB;AACxB,WACE,KAAK,QAAQ,KAAK,OAAO,IAAI,GAAG,MAAM,UACtC,KAAK,QAAQ,KAAK,SAAS,IAAI,GAAG,MAAM;AAAA,EAE5C;AAAA,EAEA,EAAE,KAAa,QAAgC;AAC7C,UAAM,WACJ,KAAK,QAAQ,KAAK,OAAO,IAAI,GAAG,KAAK,KAAK,QAAQ,KAAK,SAAS,IAAI,GAAG,KAAK;AAC9E,WAAO,cAAc,UAAU,QAAQ,KAAK,OAAO;AAAA,EACrD;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@flare-engine/localization",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "String tables, locale resolution, ICU-style plurals + interpolation",
|
|
5
|
+
"keywords": ["flare-engine", "localization", "i18n", "icu", "plurals"],
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Grzegorz Otto",
|
|
8
|
+
"homepage": "https://github.com/grzott/flare-engine#readme",
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/grzott/flare-engine/issues"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/grzott/flare-engine.git",
|
|
15
|
+
"directory": "packages/localization"
|
|
16
|
+
},
|
|
17
|
+
"main": "dist/index.js",
|
|
18
|
+
"module": "dist/index.mjs",
|
|
19
|
+
"types": "dist/index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"import": "./dist/index.mjs",
|
|
24
|
+
"require": "./dist/index.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"files": ["dist", "README.md", "LICENSE"],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsup",
|
|
30
|
+
"test": "bun test",
|
|
31
|
+
"lint": "biome check src/",
|
|
32
|
+
"typecheck": "tsc --noEmit"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@flare-engine/events": "^0.1.0"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "restricted",
|
|
39
|
+
"registry": "https://registry.npmjs.org/"
|
|
40
|
+
},
|
|
41
|
+
"sideEffects": false
|
|
42
|
+
}
|