@chelone/core 0.6.0 → 0.9.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/dist/assistant/assistant-session.d.ts +6 -0
- package/dist/assistant/assistant-session.js +204 -181
- package/dist/assistant/echo-transport.js +26 -29
- package/dist/assistant/http-transport.d.ts +10 -0
- package/dist/assistant/http-transport.js +98 -0
- package/dist/assistant/types.d.ts +1 -7
- package/dist/auth/access-control.d.ts +13 -5
- package/dist/auth/access-control.js +25 -21
- package/dist/auth/auth-options.d.ts +23 -17
- package/dist/auth/auth-options.js +82 -48
- package/dist/auth/permission-matrix.d.ts +12 -0
- package/dist/auth/permission-matrix.js +67 -0
- package/dist/auth/types.d.ts +14 -4
- package/dist/block-editor/block-document.d.ts +64 -0
- package/dist/block-editor/block-document.js +147 -0
- package/dist/block-editor/data-blocks.d.ts +63 -0
- package/dist/block-editor/data-blocks.js +117 -0
- package/dist/chart/chart-data.js +46 -44
- package/dist/command-menu/hotkey.js +58 -47
- package/dist/command-menu/recents.d.ts +4 -0
- package/dist/command-menu/recents.js +12 -11
- package/dist/command-menu/registry.d.ts +9 -5
- package/dist/command-menu/registry.js +36 -46
- package/dist/controller/calendar-dates.d.ts +12 -0
- package/dist/controller/calendar-dates.js +94 -113
- package/dist/controller/calendar-events.js +93 -88
- package/dist/controller/client-list-controller.d.ts +2 -2
- package/dist/controller/client-list-controller.js +251 -254
- package/dist/controller/data-table-columns.d.ts +2 -0
- package/dist/controller/data-table-columns.js +50 -56
- package/dist/controller/deep-equal.d.ts +1 -0
- package/dist/controller/deep-equal.js +14 -0
- package/dist/controller/filter-toggle.js +36 -44
- package/dist/controller/in-place-editor.d.ts +43 -0
- package/dist/controller/in-place-editor.js +43 -0
- package/dist/controller/infinite-list-controller.js +219 -189
- package/dist/controller/inspector.d.ts +55 -0
- package/dist/controller/inspector.js +70 -0
- package/dist/controller/job-monitor.d.ts +10 -4
- package/dist/controller/job-monitor.js +46 -47
- package/dist/controller/kanban-board.js +45 -24
- package/dist/controller/list-controller.d.ts +8 -5
- package/dist/controller/list-controller.js +447 -400
- package/dist/controller/list-params.js +96 -87
- package/dist/controller/pivot-grid.d.ts +4 -4
- package/dist/controller/pivot-grid.js +89 -95
- package/dist/controller/prev-next.d.ts +2 -2
- package/dist/controller/prev-next.js +74 -54
- package/dist/controller/record-controllers.d.ts +14 -6
- package/dist/controller/record-controllers.js +288 -257
- package/dist/controller/record-diff.d.ts +21 -0
- package/dist/controller/record-diff.js +50 -0
- package/dist/controller/record-timeline.d.ts +5 -5
- package/dist/controller/record-timeline.js +42 -30
- package/dist/controller/record-tree.js +57 -47
- package/dist/controller/record-workflow.d.ts +38 -16
- package/dist/controller/record-workflow.js +85 -64
- package/dist/controller/reference-many-controller.d.ts +2 -0
- package/dist/controller/reference-many-controller.js +199 -192
- package/dist/controller/reference-one-controller.d.ts +1 -1
- package/dist/controller/reference-one-controller.js +63 -56
- package/dist/controller/saved-queries.js +104 -91
- package/dist/controller/translate-fallback.js +10 -6
- package/dist/csv/export.d.ts +4 -3
- package/dist/csv/export.js +43 -40
- package/dist/csv/import-controller.d.ts +4 -0
- package/dist/csv/import-controller.js +189 -169
- package/dist/csv/match-headers.js +29 -22
- package/dist/csv/parse-with.js +60 -54
- package/dist/csv/run-import.js +69 -72
- package/dist/csv/validate-rows.d.ts +1 -1
- package/dist/csv/validate-rows.js +65 -52
- package/dist/data/cache-ops.d.ts +5 -4
- package/dist/data/cache-ops.js +208 -188
- package/dist/data/combine-data-providers.js +25 -23
- package/dist/data/data-provider-log.d.ts +45 -0
- package/dist/data/data-provider-log.js +93 -0
- package/dist/data/errors.js +14 -10
- package/dist/data/fake-provider.d.ts +6 -3
- package/dist/data/fake-provider.js +135 -114
- package/dist/data/fetch-utils.js +61 -44
- package/dist/data/filter-branch.d.ts +5 -0
- package/dist/data/filter-branch.js +10 -0
- package/dist/data/get-many-aggregate.js +84 -76
- package/dist/data/http-error.js +17 -12
- package/dist/data/keys.js +50 -36
- package/dist/data/mutation-options.d.ts +14 -9
- package/dist/data/mutation-options.js +93 -109
- package/dist/data/query-options.js +73 -68
- package/dist/data/remove-empty.d.ts +3 -0
- package/dist/data/remove-empty.js +11 -0
- package/dist/data/undoable.d.ts +2 -2
- package/dist/data/undoable.js +190 -174
- package/dist/data/with-lifecycle-callbacks.d.ts +45 -17
- package/dist/data/with-lifecycle-callbacks.js +66 -52
- package/dist/environment/application-update-store.js +58 -55
- package/dist/environment/offline-store.js +21 -21
- package/dist/form/array-binding.d.ts +30 -11
- package/dist/form/array-binding.js +184 -166
- package/dist/form/choice-ids.d.ts +7 -0
- package/dist/form/choice-ids.js +11 -0
- package/dist/form/choices-controller.d.ts +22 -2
- package/dist/form/choices-controller.js +315 -275
- package/dist/form/date-format.d.ts +38 -0
- package/dist/form/date-format.js +89 -0
- package/dist/form/dirty-values.d.ts +2 -2
- package/dist/form/dirty-values.js +22 -11
- package/dist/form/duration.d.ts +22 -0
- package/dist/form/duration.js +49 -0
- package/dist/form/edit-create-form.d.ts +12 -5
- package/dist/form/edit-create-form.js +243 -188
- package/dist/form/event-value.d.ts +6 -0
- package/dist/form/event-value.js +17 -0
- package/dist/form/field-error.d.ts +14 -0
- package/dist/form/field-error.js +28 -0
- package/dist/form/field-name.d.ts +20 -0
- package/dist/form/field-name.js +6 -0
- package/dist/form/filter-form-values.js +63 -49
- package/dist/form/form-controller.d.ts +9 -1
- package/dist/form/form-controller.js +165 -160
- package/dist/form/form-groups.d.ts +4 -3
- package/dist/form/form-groups.js +79 -79
- package/dist/form/form-handle.d.ts +175 -0
- package/dist/form/form-handle.js +68 -0
- package/dist/form/input-binding.d.ts +56 -31
- package/dist/form/input-binding.js +171 -118
- package/dist/form/input-values.d.ts +140 -0
- package/dist/form/input-values.js +206 -0
- package/dist/form/json-text.d.ts +11 -0
- package/dist/form/json-text.js +62 -37
- package/dist/form/save-pipeline.d.ts +6 -8
- package/dist/form/save-pipeline.js +91 -82
- package/dist/form/set-form-values.d.ts +6 -0
- package/dist/form/set-form-values.js +10 -0
- package/dist/form/source.js +97 -65
- package/dist/form/text-of.d.ts +2 -0
- package/dist/form/text-of.js +7 -0
- package/dist/form/validators.d.ts +58 -19
- package/dist/form/validators.js +97 -71
- package/dist/geo/bbox.d.ts +5 -4
- package/dist/geo/bbox.js +69 -48
- package/dist/geo/bounds.d.ts +14 -3
- package/dist/geo/bounds.js +98 -68
- package/dist/geo/constraints.js +88 -93
- package/dist/geo/coordinates.d.ts +10 -0
- package/dist/geo/coordinates.js +44 -23
- package/dist/geo/editor.d.ts +13 -13
- package/dist/geo/editor.js +176 -153
- package/dist/geo/geocoding.d.ts +4 -4
- package/dist/geo/geocoding.js +38 -41
- package/dist/geo/geojson.d.ts +12 -74
- package/dist/geo/geojson.js +52 -40
- package/dist/geo/geometry-math.d.ts +4 -4
- package/dist/geo/geometry-math.js +100 -93
- package/dist/geo/index.d.ts +9 -8
- package/dist/geo/map-layers.d.ts +42 -7
- package/dist/geo/map-layers.js +88 -62
- package/dist/geo/map-style.d.ts +12 -0
- package/dist/geo/map-style.js +43 -32
- package/dist/geo/messages.js +48 -45
- package/dist/geo/nominatim.js +88 -80
- package/dist/geo/osm-presets.d.ts +4 -3
- package/dist/geo/osm-presets.js +108 -86
- package/dist/geo/osm-tags.js +14 -12
- package/dist/geo/overpass.d.ts +14 -2
- package/dist/geo/overpass.js +83 -90
- package/dist/geo/shape-value.d.ts +9 -5
- package/dist/geo/shape-value.js +169 -146
- package/dist/geo/snap-to-roads.d.ts +3 -3
- package/dist/geo/snap-to-roads.js +55 -50
- package/dist/geo/throttle.js +36 -39
- package/dist/guards.d.ts +44 -0
- package/dist/guards.js +77 -0
- package/dist/i18n/default-messages.js +34 -29
- package/dist/i18n/english-messages.d.ts +172 -0
- package/dist/i18n/english-messages.js +359 -189
- package/dist/i18n/i18n.d.ts +2 -2
- package/dist/i18n/i18n.js +47 -60
- package/dist/i18n/inflection.d.ts +34 -0
- package/dist/i18n/inflection.js +82 -0
- package/dist/i18n/messages.d.ts +10 -2
- package/dist/i18n/messages.js +17 -4
- package/dist/i18n/missing-keys.d.ts +4 -0
- package/dist/i18n/missing-keys.js +70 -62
- package/dist/i18n/translatable.d.ts +16 -1
- package/dist/i18n/translatable.js +40 -47
- package/dist/index.d.ts +87 -39
- package/dist/index.js +54 -301
- package/dist/json/deep-equal.d.ts +8 -0
- package/dist/json/deep-equal.js +24 -0
- package/dist/json/strip-json-comments.js +38 -42
- package/dist/noop.d.ts +2 -0
- package/dist/noop.js +4 -0
- package/dist/notification/error-message.d.ts +2 -0
- package/dist/notification/error-message.js +10 -0
- package/dist/notification/notification-store.d.ts +3 -2
- package/dist/notification/notification-store.js +41 -45
- package/dist/onboarding-tour/placement.d.ts +2 -0
- package/dist/onboarding-tour/placement.js +65 -78
- package/dist/onboarding-tour/tour-controller.js +46 -54
- package/dist/realtime/add-events-for-mutations.js +75 -81
- package/dist/realtime/announce-lock.js +16 -13
- package/dist/realtime/broadcast-channel-transport.js +65 -59
- package/dist/realtime/copy-members.js +22 -14
- package/dist/realtime/in-memory-lock-provider.js +40 -48
- package/dist/realtime/live.js +95 -101
- package/dist/realtime/lock-options.js +16 -16
- package/dist/realtime/lock-session.js +91 -78
- package/dist/realtime/memory-transport.js +25 -25
- package/dist/realtime/presence.js +120 -122
- package/dist/realtime/realtime-data-provider.d.ts +10 -0
- package/dist/realtime/realtime-data-provider.js +45 -42
- package/dist/realtime/reconnect-backoff.js +17 -0
- package/dist/realtime/sse-transport.d.ts +31 -0
- package/dist/realtime/sse-transport.js +235 -0
- package/dist/realtime/status-store.js +23 -28
- package/dist/realtime/topic-hub.js +62 -71
- package/dist/realtime/topics.js +10 -12
- package/dist/realtime/types.js +10 -12
- package/dist/realtime/unread-count.js +21 -21
- package/dist/realtime/websocket-transport.d.ts +32 -0
- package/dist/realtime/websocket-transport.js +306 -0
- package/dist/resource/describe-resource.d.ts +2 -0
- package/dist/resource/describe-resource.js +247 -219
- package/dist/resource/get-at-path.js +29 -14
- package/dist/resource/identifier.d.ts +18 -0
- package/dist/resource/identifier.js +27 -0
- package/dist/resource/record.d.ts +4 -0
- package/dist/resource/record.js +10 -0
- package/dist/resource/register.d.ts +7 -0
- package/dist/resource/registry-store.d.ts +45 -15
- package/dist/resource/registry-store.js +67 -60
- package/dist/resource/registry.d.ts +10 -0
- package/dist/resource/registry.js +8 -4
- package/dist/resource/source-path.d.ts +23 -2
- package/dist/rich-text/rich-text-extras.d.ts +134 -0
- package/dist/rich-text/rich-text-extras.js +181 -0
- package/dist/rich-text/rich-text-image.d.ts +124 -0
- package/dist/rich-text/rich-text-image.js +163 -0
- package/dist/rich-text/rich-text.d.ts +6 -0
- package/dist/rich-text/rich-text.js +125 -88
- package/dist/router/location.d.ts +24 -4
- package/dist/router/location.js +43 -18
- package/dist/router/match.js +47 -39
- package/dist/router/paths.js +40 -34
- package/dist/router/record-from-location.d.ts +1 -1
- package/dist/router/record-from-location.js +25 -17
- package/dist/store/adapters.js +51 -55
- package/dist/store/micro-store.js +57 -59
- package/dist/store/shallow-equal.d.ts +12 -0
- package/dist/store/shallow-equal.js +20 -0
- package/dist/store/store.js +54 -57
- package/dist/ui-defaults.d.ts +15 -0
- package/dist/ui-defaults.js +17 -0
- package/package.json +13 -11
package/dist/i18n/i18n.js
CHANGED
|
@@ -1,68 +1,55 @@
|
|
|
1
|
+
// src/i18n/i18n.ts
|
|
1
2
|
function interpolate(template, params) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
/\{(\w+)\}/g,
|
|
5
|
-
(match, name) => name in params ? String(params[name]) : match
|
|
6
|
-
);
|
|
3
|
+
if (!params) return template;
|
|
4
|
+
return template.replace(/\{(\w+)\}/g, (match, name) => name in params ? String(params[name]) : match);
|
|
7
5
|
}
|
|
8
6
|
function resolve(value, locale, params) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
7
|
+
if (typeof value === "string") return interpolate(value, params);
|
|
8
|
+
const raw = params?.count ?? params?.smart_count;
|
|
9
|
+
if (raw === void 0 || raw === null) return interpolate(value.other, params);
|
|
10
|
+
const count = Number(raw);
|
|
11
|
+
if (Number.isNaN(count)) return interpolate(value.other, params);
|
|
12
|
+
return interpolate(value[new Intl.PluralRules(locale).select(count)] ?? value.other, {
|
|
13
|
+
...params,
|
|
14
|
+
count: params?.count ?? raw,
|
|
15
|
+
smart_count: params?.smart_count ?? raw
|
|
16
|
+
});
|
|
19
17
|
}
|
|
18
|
+
/** Own-property lookup: `"constructor"` or `"toString"` is a missing key, not a function. */
|
|
20
19
|
function lookup(catalog, key) {
|
|
21
|
-
|
|
20
|
+
return catalog && Object.hasOwn(catalog, key) ? catalog[key] : void 0;
|
|
22
21
|
}
|
|
23
22
|
function createI18n(options) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if (next === locale) return;
|
|
56
|
-
locale = next;
|
|
57
|
-
for (const cb of subscribers) cb();
|
|
58
|
-
},
|
|
59
|
-
getLocales: () => Object.keys(options.messages),
|
|
60
|
-
subscribe(callback) {
|
|
61
|
-
subscribers.add(callback);
|
|
62
|
-
return () => void subscribers.delete(callback);
|
|
63
|
-
}
|
|
64
|
-
};
|
|
23
|
+
let locale = options.locale;
|
|
24
|
+
const subscribers = /* @__PURE__ */ new Set();
|
|
25
|
+
return {
|
|
26
|
+
translate(key, ...args) {
|
|
27
|
+
const params = args[0];
|
|
28
|
+
const own = lookup(options.messages[locale], key);
|
|
29
|
+
if (own === void 0 && options.onMissingKey) try {
|
|
30
|
+
options.onMissingKey(key, locale, typeof params?._ === "string" ? params._ : void 0);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
console.error(error);
|
|
33
|
+
}
|
|
34
|
+
const value = own ?? (options.fallbackLocale ? lookup(options.messages[options.fallbackLocale], key) : void 0);
|
|
35
|
+
if (value === void 0) {
|
|
36
|
+
if (typeof params?._ === "string") return interpolate(params._, params);
|
|
37
|
+
return key;
|
|
38
|
+
}
|
|
39
|
+
return resolve(value, locale, params);
|
|
40
|
+
},
|
|
41
|
+
getLocale: () => locale,
|
|
42
|
+
setLocale(next) {
|
|
43
|
+
if (!Object.hasOwn(options.messages, next)) throw new Error(`createI18n: unknown locale "${next}" — available: ${Object.keys(options.messages).join(", ")}`);
|
|
44
|
+
if (next === locale) return;
|
|
45
|
+
locale = next;
|
|
46
|
+
for (const cb of subscribers) cb();
|
|
47
|
+
},
|
|
48
|
+
getLocales: () => Object.keys(options.messages),
|
|
49
|
+
subscribe(callback) {
|
|
50
|
+
subscribers.add(callback);
|
|
51
|
+
return () => void subscribers.delete(callback);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
65
54
|
}
|
|
66
|
-
export {
|
|
67
|
-
createI18n
|
|
68
|
-
};
|
|
55
|
+
export { createI18n };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Clean-room reimplementation of the SUBSET of the `inflection` package the
|
|
3
|
+
* ra-core label helpers rely on. Chelone carries no `inflection` dependency, so
|
|
4
|
+
* these three small helpers cover exactly what `useGetResourceLabel` /
|
|
5
|
+
* `useTranslateLabel` need:
|
|
6
|
+
*
|
|
7
|
+
* - `humanize(text)` — ra-core `humanize`: strip a trailing `_id`, replace
|
|
8
|
+
* underscores with spaces, lowercase, and capitalize the first letter. Does
|
|
9
|
+
* NOT split camelCase (parity: `humanize('authorName') === 'Authorname'`).
|
|
10
|
+
* - `humanizeField(source)` — ra-core `transform(['underscore','humanize'])`:
|
|
11
|
+
* camelCase-aware, so `firstName` → `First name`. Used for field labels.
|
|
12
|
+
* - `pluralize(word, count)` — ra-core `inflect(word, count)`: singular for
|
|
13
|
+
* `count === 1`, plural otherwise. Covers the regular English rules plus a
|
|
14
|
+
* small irregular set. Full parity with `inflection`'s complete irregular
|
|
15
|
+
* table is a documented non-goal (the fallback only fires when a translation
|
|
16
|
+
* key AND an `options.label` are both absent).
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* ra-core `humanize`: strip a trailing `_id`, underscores → spaces, lowercase,
|
|
20
|
+
* capitalize the first letter. Deliberately does NOT split camelCase.
|
|
21
|
+
*/
|
|
22
|
+
export declare function humanize(text: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* ra-core `transform(source, ['underscore','humanize'])`: camelCase-aware
|
|
25
|
+
* humanization for field labels. `firstName` → `First name`, `full_name` →
|
|
26
|
+
* `Full name`, `author name` → `Author name`.
|
|
27
|
+
*/
|
|
28
|
+
export declare function humanizeField(source: string): string;
|
|
29
|
+
/**
|
|
30
|
+
* ra-core `inflect(word, count)`: singularize when `count === 1`, pluralize
|
|
31
|
+
* otherwise. Case-preserving only for the first letter (the caller humanizes
|
|
32
|
+
* afterward, so casing beyond that does not matter here).
|
|
33
|
+
*/
|
|
34
|
+
export declare function pluralize(word: string, count?: number): string;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// src/i18n/inflection.ts
|
|
2
|
+
/**
|
|
3
|
+
* Clean-room reimplementation of the SUBSET of the `inflection` package the
|
|
4
|
+
* ra-core label helpers rely on. Chelone carries no `inflection` dependency, so
|
|
5
|
+
* these three small helpers cover exactly what `useGetResourceLabel` /
|
|
6
|
+
* `useTranslateLabel` need:
|
|
7
|
+
*
|
|
8
|
+
* - `humanize(text)` — ra-core `humanize`: strip a trailing `_id`, replace
|
|
9
|
+
* underscores with spaces, lowercase, and capitalize the first letter. Does
|
|
10
|
+
* NOT split camelCase (parity: `humanize('authorName') === 'Authorname'`).
|
|
11
|
+
* - `humanizeField(source)` — ra-core `transform(['underscore','humanize'])`:
|
|
12
|
+
* camelCase-aware, so `firstName` → `First name`. Used for field labels.
|
|
13
|
+
* - `pluralize(word, count)` — ra-core `inflect(word, count)`: singular for
|
|
14
|
+
* `count === 1`, plural otherwise. Covers the regular English rules plus a
|
|
15
|
+
* small irregular set. Full parity with `inflection`'s complete irregular
|
|
16
|
+
* table is a documented non-goal (the fallback only fires when a translation
|
|
17
|
+
* key AND an `options.label` are both absent).
|
|
18
|
+
*/
|
|
19
|
+
/** Capitalize the first character; leave the rest untouched. */
|
|
20
|
+
function capitalizeFirst(text) {
|
|
21
|
+
return text.length === 0 ? text : text.charAt(0).toUpperCase() + text.slice(1);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Convert a camelCase / snake_case identifier to lowercase underscore form:
|
|
25
|
+
* `firstName` → `first_name`, `full_name` → `full_name`.
|
|
26
|
+
*/
|
|
27
|
+
function underscore(text) {
|
|
28
|
+
return text.replace(/([a-z\d])([A-Z])/g, "$1_$2").replace(/[-\s]+/g, "_").toLowerCase();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* ra-core `humanize`: strip a trailing `_id`, underscores → spaces, lowercase,
|
|
32
|
+
* capitalize the first letter. Deliberately does NOT split camelCase.
|
|
33
|
+
*/
|
|
34
|
+
function humanize(text) {
|
|
35
|
+
return capitalizeFirst(text.replace(/_id$/, "").replace(/_+/g, " ").trim().toLowerCase());
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* ra-core `transform(source, ['underscore','humanize'])`: camelCase-aware
|
|
39
|
+
* humanization for field labels. `firstName` → `First name`, `full_name` →
|
|
40
|
+
* `Full name`, `author name` → `Author name`.
|
|
41
|
+
*/
|
|
42
|
+
function humanizeField(source) {
|
|
43
|
+
return humanize(underscore(source));
|
|
44
|
+
}
|
|
45
|
+
var IRREGULARS = [
|
|
46
|
+
["person", "people"],
|
|
47
|
+
["child", "children"],
|
|
48
|
+
["man", "men"],
|
|
49
|
+
["woman", "women"],
|
|
50
|
+
["mouse", "mice"],
|
|
51
|
+
["foot", "feet"],
|
|
52
|
+
["tooth", "teeth"],
|
|
53
|
+
["goose", "geese"]
|
|
54
|
+
];
|
|
55
|
+
/** Regular English plural of a lowercase word (idempotent on plurals). */
|
|
56
|
+
function toPlural(word) {
|
|
57
|
+
for (const [singular, plural] of IRREGULARS) if (word === singular || word === plural) return plural;
|
|
58
|
+
const singular = toSingular(word);
|
|
59
|
+
if (/[^aeiou]y$/.test(singular)) return `${singular.slice(0, -1)}ies`;
|
|
60
|
+
if (/(s|x|z|ch|sh)$/.test(singular)) return `${singular}es`;
|
|
61
|
+
return `${singular}s`;
|
|
62
|
+
}
|
|
63
|
+
/** Regular English singular of a lowercase word. */
|
|
64
|
+
function toSingular(word) {
|
|
65
|
+
for (const [singular, plural] of IRREGULARS) {
|
|
66
|
+
if (word === plural) return singular;
|
|
67
|
+
if (word === singular) return singular;
|
|
68
|
+
}
|
|
69
|
+
if (/ies$/.test(word)) return `${word.slice(0, -3)}y`;
|
|
70
|
+
if (/(ss|x|z|ch|sh)es$/.test(word)) return word.slice(0, -2);
|
|
71
|
+
if (/s$/.test(word) && !/ss$/.test(word)) return word.slice(0, -1);
|
|
72
|
+
return word;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* ra-core `inflect(word, count)`: singularize when `count === 1`, pluralize
|
|
76
|
+
* otherwise. Case-preserving only for the first letter (the caller humanizes
|
|
77
|
+
* afterward, so casing beyond that does not matter here).
|
|
78
|
+
*/
|
|
79
|
+
function pluralize(word, count = 2) {
|
|
80
|
+
return count === 1 ? toSingular(word) : toPlural(word);
|
|
81
|
+
}
|
|
82
|
+
export { humanize, humanizeField, pluralize };
|
package/dist/i18n/messages.d.ts
CHANGED
|
@@ -4,6 +4,12 @@ export type PluralMessage = Partial<Record<Intl.LDMLPluralRule, string>> & {
|
|
|
4
4
|
};
|
|
5
5
|
export type MessageValue = string | PluralMessage;
|
|
6
6
|
export type MessageCatalog = Record<string, MessageValue>;
|
|
7
|
+
/** The values a message placeholder takes; one type for typed catalogs and the erased view. */
|
|
8
|
+
export type TranslateParamValues = Record<string, string | number>;
|
|
9
|
+
/** Boundary narrowing for `unknown` input: keeps the string and number entries of a plain object, `undefined` for anything else. */
|
|
10
|
+
export declare function pickTranslateParamValues(value: unknown): TranslateParamValues | undefined;
|
|
11
|
+
/** Drops absent values: an `undefined` param would otherwise render as the text "undefined". */
|
|
12
|
+
export declare function definedParams(params: Record<string, string | number | undefined>): TranslateParamValues;
|
|
7
13
|
/** Identity helper pinning literal keys AND literal template strings (for param inference). */
|
|
8
14
|
export declare function defineMessages<const T extends MessageCatalog>(catalog: T): T;
|
|
9
15
|
export type MessageKey<TCatalog extends MessageCatalog> = keyof TCatalog & string;
|
|
@@ -16,9 +22,11 @@ type ParamsObject<T extends string> = [ExtractParams<T>] extends [never] ? undef
|
|
|
16
22
|
* Required params for a message value: placeholder names for plain strings;
|
|
17
23
|
* plural messages additionally require `count` (drives Intl.PluralRules).
|
|
18
24
|
*/
|
|
19
|
-
export type TranslateParams<TValue extends MessageValue> = TValue extends string ? ParamsObject<TValue> : TValue extends PluralMessage ?
|
|
25
|
+
export type TranslateParams<TValue extends MessageValue> = TValue extends string ? ParamsObject<TValue> : TValue extends PluralMessage ? [ExtractParams<TValue["other"]>] extends [never] ? {
|
|
26
|
+
count: number;
|
|
27
|
+
} : {
|
|
20
28
|
[K in ExtractParams<TValue["other"]>]: string | number;
|
|
21
|
-
}
|
|
29
|
+
} & {
|
|
22
30
|
count: number;
|
|
23
31
|
} : never;
|
|
24
32
|
export {};
|
package/dist/i18n/messages.js
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
|
+
// src/i18n/messages.ts
|
|
2
|
+
/** Boundary narrowing for `unknown` input: keeps the string and number entries of a plain object, `undefined` for anything else. */
|
|
3
|
+
function pickTranslateParamValues(value) {
|
|
4
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return;
|
|
5
|
+
const picked = {};
|
|
6
|
+
for (const [name, entry] of Object.entries(value)) if (typeof entry === "string" || typeof entry === "number") picked[name] = entry;
|
|
7
|
+
return picked;
|
|
8
|
+
}
|
|
9
|
+
/** Drops absent values: an `undefined` param would otherwise render as the text "undefined". */
|
|
10
|
+
function definedParams(params) {
|
|
11
|
+
const defined = {};
|
|
12
|
+
for (const [name, value] of Object.entries(params)) if (value !== void 0) defined[name] = value;
|
|
13
|
+
return defined;
|
|
14
|
+
}
|
|
15
|
+
/** Identity helper pinning literal keys AND literal template strings (for param inference). */
|
|
1
16
|
function defineMessages(catalog) {
|
|
2
|
-
|
|
17
|
+
return catalog;
|
|
3
18
|
}
|
|
4
|
-
export {
|
|
5
|
-
defineMessages
|
|
6
|
-
};
|
|
19
|
+
export { defineMessages, definedParams, pickTranslateParamValues };
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/** Every key the i18n key editor labels itself with. The panel never lists these. */
|
|
2
|
+
export declare const OWN_KEY_PREFIX = "chelone.i18n_key_editor.";
|
|
3
|
+
/** How long its export button shows `Copied`. */
|
|
4
|
+
export declare const COPIED_MS = 1500;
|
|
1
5
|
export interface MissingKeyEntry {
|
|
2
6
|
key: string;
|
|
3
7
|
fallback: string | undefined;
|
|
@@ -1,64 +1,72 @@
|
|
|
1
|
-
|
|
1
|
+
// src/i18n/missing-keys.ts
|
|
2
|
+
/** Every key the i18n key editor labels itself with. The panel never lists these. */
|
|
3
|
+
var OWN_KEY_PREFIX = "chelone.i18n_key_editor.";
|
|
4
|
+
/** How long its export button shows `Copied`. */
|
|
5
|
+
var COPIED_MS = 1500;
|
|
6
|
+
var NO_ENTRIES = [];
|
|
2
7
|
function createMissingKeyStore() {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
8
|
+
const byLocale = /* @__PURE__ */ new Map();
|
|
9
|
+
const snapshots = /* @__PURE__ */ new Map();
|
|
10
|
+
const subscribers = /* @__PURE__ */ new Set();
|
|
11
|
+
let scheduled = false;
|
|
12
|
+
const changed = (locale) => {
|
|
13
|
+
snapshots.delete(locale);
|
|
14
|
+
if (scheduled) return;
|
|
15
|
+
scheduled = true;
|
|
16
|
+
queueMicrotask(() => {
|
|
17
|
+
scheduled = false;
|
|
18
|
+
for (const cb of [...subscribers]) cb();
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
const getEntries = (locale) => {
|
|
22
|
+
const cached = snapshots.get(locale);
|
|
23
|
+
if (cached) return cached;
|
|
24
|
+
const entries = byLocale.get(locale);
|
|
25
|
+
if (!entries?.size) return NO_ENTRIES;
|
|
26
|
+
const snapshot = [...entries.values()];
|
|
27
|
+
snapshots.set(locale, snapshot);
|
|
28
|
+
return snapshot;
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
record(key, locale, fallback) {
|
|
32
|
+
let entries = byLocale.get(locale);
|
|
33
|
+
if (!entries) {
|
|
34
|
+
entries = /* @__PURE__ */ new Map();
|
|
35
|
+
byLocale.set(locale, entries);
|
|
36
|
+
}
|
|
37
|
+
if (entries.has(key)) return;
|
|
38
|
+
entries.set(key, {
|
|
39
|
+
key,
|
|
40
|
+
fallback,
|
|
41
|
+
translation: ""
|
|
42
|
+
});
|
|
43
|
+
changed(locale);
|
|
44
|
+
},
|
|
45
|
+
getEntries,
|
|
46
|
+
setTranslation(locale, key, text) {
|
|
47
|
+
const entries = byLocale.get(locale);
|
|
48
|
+
const entry = entries?.get(key);
|
|
49
|
+
if (!entries || !entry || entry.translation === text) return;
|
|
50
|
+
entries.set(key, {
|
|
51
|
+
...entry,
|
|
52
|
+
translation: text
|
|
53
|
+
});
|
|
54
|
+
changed(locale);
|
|
55
|
+
},
|
|
56
|
+
clear(locale) {
|
|
57
|
+
if (!byLocale.get(locale)?.size) return;
|
|
58
|
+
byLocale.delete(locale);
|
|
59
|
+
changed(locale);
|
|
60
|
+
},
|
|
61
|
+
toJson(locale) {
|
|
62
|
+
const out = /* @__PURE__ */ new Map();
|
|
63
|
+
for (const { key, translation } of getEntries(locale)) if (translation.trim() !== "") out.set(key, translation);
|
|
64
|
+
return JSON.stringify(Object.fromEntries(out), null, 2);
|
|
65
|
+
},
|
|
66
|
+
subscribe(callback) {
|
|
67
|
+
subscribers.add(callback);
|
|
68
|
+
return () => void subscribers.delete(callback);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
61
71
|
}
|
|
62
|
-
export {
|
|
63
|
-
createMissingKeyStore
|
|
64
|
-
};
|
|
72
|
+
export { COPIED_MS, OWN_KEY_PREFIX, createMissingKeyStore };
|
|
@@ -19,6 +19,20 @@ export interface TranslatableController {
|
|
|
19
19
|
* form values themselves stay in the form; this only tracks the selection.
|
|
20
20
|
*/
|
|
21
21
|
export declare function createTranslatableController(options: TranslatableOptions): TranslatableController;
|
|
22
|
+
type LocalizedField<V> = V extends (...args: never[]) => unknown ? V : V extends Date ? V : V extends readonly (infer Item)[] ? LocalizedField<Item>[] : V extends Record<string, string> ? string : V extends object ? RecordForLocale<V> : V;
|
|
23
|
+
/**
|
|
24
|
+
* What `getRecordForLocale` makes of `T`: a field typed `Record<string, string>`
|
|
25
|
+
* becomes `string`, an array maps its items, any other object maps its fields,
|
|
26
|
+
* and every other type stays. The root is never replaced.
|
|
27
|
+
*
|
|
28
|
+
* A nested `{ a: string; b: string }` field reads as a translation map here,
|
|
29
|
+
* though at run time only `locales` tells the two apart. A map without the
|
|
30
|
+
* selected locale is also left as it is at run time, so its type is the
|
|
31
|
+
* translated one only for a locale the data covers.
|
|
32
|
+
*/
|
|
33
|
+
export type RecordForLocale<T> = {
|
|
34
|
+
[K in keyof T]: LocalizedField<T[K]>;
|
|
35
|
+
};
|
|
22
36
|
/**
|
|
23
37
|
* A copy of `record` where every object holding `locale` as an own key is
|
|
24
38
|
* replaced by its value at that key, at any depth and inside arrays. Nodes
|
|
@@ -29,4 +43,5 @@ export declare function createTranslatableController(options: TranslatableOption
|
|
|
29
43
|
* (`id` is Indonesian), so pass `locales` to also keep objects that have other
|
|
30
44
|
* keys, such as a nested `{ id, name }` reference, from being read as a map.
|
|
31
45
|
*/
|
|
32
|
-
export declare function getRecordForLocale<T extends object | null | undefined>(record: T, locale: string, locales?: readonly string[]): T extends object ?
|
|
46
|
+
export declare function getRecordForLocale<T extends object | null | undefined>(record: T, locale: string, locales?: readonly string[]): T extends object ? RecordForLocale<T> : T;
|
|
47
|
+
export {};
|
|
@@ -1,56 +1,49 @@
|
|
|
1
|
+
import { isPlainObject } from "../guards.js";
|
|
1
2
|
import { createMicroStore } from "../store/micro-store.js";
|
|
3
|
+
// src/i18n/translatable.ts
|
|
4
|
+
/**
|
|
5
|
+
* The state behind a multi-locale editor: which locale is on screen. The
|
|
6
|
+
* form values themselves stay in the form; this only tracks the selection.
|
|
7
|
+
*/
|
|
2
8
|
function createTranslatableController(options) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
`Locale "${locale}" is not one of: ${locales.join(", ")}`
|
|
20
|
-
);
|
|
21
|
-
}
|
|
22
|
-
store.setState({ selectedLocale: locale });
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
function isPlainObject(value) {
|
|
27
|
-
if (typeof value !== "object" || value === null) return false;
|
|
28
|
-
const proto = Object.getPrototypeOf(value);
|
|
29
|
-
return proto === Object.prototype || proto === null;
|
|
9
|
+
const { locales, defaultLocale } = options;
|
|
10
|
+
const first = locales[0];
|
|
11
|
+
if (first === void 0) throw new Error("A translatable controller needs at least one locale");
|
|
12
|
+
const store = createMicroStore({
|
|
13
|
+
locales,
|
|
14
|
+
selectedLocale: defaultLocale !== void 0 && locales.includes(defaultLocale) ? defaultLocale : first
|
|
15
|
+
});
|
|
16
|
+
return {
|
|
17
|
+
getSnapshot: store.getSnapshot,
|
|
18
|
+
subscribe: store.subscribe,
|
|
19
|
+
destroy: store.destroy,
|
|
20
|
+
selectLocale(locale) {
|
|
21
|
+
if (!locales.includes(locale)) throw new Error(`Locale "${locale}" is not one of: ${locales.join(", ")}`);
|
|
22
|
+
store.setState({ selectedLocale: locale });
|
|
23
|
+
}
|
|
24
|
+
};
|
|
30
25
|
}
|
|
31
26
|
function pick(node, locale, locales, isRoot) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (!isRoot && isTranslationMap(node, locale, locales)) {
|
|
37
|
-
return pick(node[locale], locale, locales, false);
|
|
38
|
-
}
|
|
39
|
-
return Object.fromEntries(
|
|
40
|
-
Object.entries(node).map(([key, value]) => [
|
|
41
|
-
key,
|
|
42
|
-
pick(value, locale, locales, false)
|
|
43
|
-
])
|
|
44
|
-
);
|
|
27
|
+
if (Array.isArray(node)) return node.map((item) => pick(item, locale, locales, false));
|
|
28
|
+
if (!isPlainObject(node)) return node;
|
|
29
|
+
if (!isRoot && isTranslationMap(node, locale, locales)) return pick(node[locale], locale, locales, false);
|
|
30
|
+
return Object.fromEntries(Object.entries(node).map(([key, value]) => [key, pick(value, locale, locales, false)]));
|
|
45
31
|
}
|
|
46
32
|
function isTranslationMap(node, locale, locales) {
|
|
47
|
-
|
|
48
|
-
|
|
33
|
+
if (!Object.hasOwn(node, locale)) return false;
|
|
34
|
+
return !locales || Object.keys(node).every((key) => locales.includes(key));
|
|
49
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* A copy of `record` where every object holding `locale` as an own key is
|
|
38
|
+
* replaced by its value at that key, at any depth and inside arrays. Nodes
|
|
39
|
+
* without the key are kept as they are, so a record missing the locale still
|
|
40
|
+
* shows the translatable object.
|
|
41
|
+
*
|
|
42
|
+
* The record itself is never replaced. A locale code can equal a field name
|
|
43
|
+
* (`id` is Indonesian), so pass `locales` to also keep objects that have other
|
|
44
|
+
* keys, such as a nested `{ id, name }` reference, from being read as a map.
|
|
45
|
+
*/
|
|
50
46
|
function getRecordForLocale(record, locale, locales) {
|
|
51
|
-
|
|
47
|
+
return pick(record, locale, locales, true);
|
|
52
48
|
}
|
|
53
|
-
export {
|
|
54
|
-
createTranslatableController,
|
|
55
|
-
getRecordForLocale
|
|
56
|
-
};
|
|
49
|
+
export { createTranslatableController, getRecordForLocale };
|