@open-pioneer/runtime 4.7.0-dev.20260624141040 → 4.7.0-dev.20260713101438
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/CHANGELOG.md +14 -1
- package/CustomElement.d.ts +27 -2
- package/CustomElement.js.map +1 -1
- package/_virtual/source-info.js +2 -2
- package/api.d.ts +55 -2
- package/app/AppInstance.d.ts +3 -27
- package/app/AppInstance.js +144 -119
- package/app/AppInstance.js.map +1 -1
- package/app/gatherConfig.js +1 -0
- package/app/gatherConfig.js.map +1 -1
- package/builtin-services/ApiServiceImpl.d.ts +1 -1
- package/builtin-services/ApiServiceImpl.js +3 -3
- package/builtin-services/ApiServiceImpl.js.map +1 -1
- package/builtin-services/ApplicationContextImpl.d.ts +11 -7
- package/builtin-services/ApplicationContextImpl.js +51 -10
- package/builtin-services/ApplicationContextImpl.js.map +1 -1
- package/builtin-services/LocaleServiceImpl.d.ts +18 -0
- package/builtin-services/LocaleServiceImpl.js +29 -0
- package/builtin-services/LocaleServiceImpl.js.map +1 -0
- package/builtin-services/NumberParserServiceImpl.d.ts +8 -3
- package/builtin-services/NumberParserServiceImpl.js +17 -12
- package/builtin-services/NumberParserServiceImpl.js.map +1 -1
- package/builtin-services/index.d.ts +3 -1
- package/builtin-services/index.js +34 -4
- package/builtin-services/index.js.map +1 -1
- package/dom/index.d.ts +1 -1
- package/dom/index.js +1 -4
- package/dom/index.js.map +1 -1
- package/i18n/AppIntl.d.ts +49 -11
- package/i18n/AppIntl.js +205 -87
- package/i18n/AppIntl.js.map +1 -1
- package/i18n/index.d.ts +3 -2
- package/i18n/index.js +2 -1
- package/i18n/index.js.map +1 -1
- package/i18n/intl-locale.d.ts +13 -0
- package/i18n/intl-locale.js +17 -0
- package/i18n/intl-locale.js.map +1 -0
- package/i18n/pick.d.ts +29 -16
- package/i18n/pick.js +78 -51
- package/i18n/pick.js.map +1 -1
- package/index.d.ts +1 -1
- package/index.js +6 -0
- package/index.js.map +1 -1
- package/package.json +6 -5
- package/react-integration/ChakraProvider.d.ts +1 -1
- package/react-integration/ChakraProvider.js +3 -2
- package/react-integration/ChakraProvider.js.map +1 -1
- package/react-integration/PackageContext.d.ts +1 -1
- package/react-integration/PackageContext.js.map +1 -1
- package/react-integration/ReactIntegration.d.ts +2 -10
- package/react-integration/ReactIntegration.js +28 -28
- package/react-integration/ReactIntegration.js.map +1 -1
- package/react-integration/hooks.js +5 -9
- package/react-integration/hooks.js.map +1 -1
- package/service-layer/ServiceLayer.d.ts +1 -20
- package/service-layer/ServiceLayer.js +39 -39
- package/service-layer/ServiceLayer.js.map +1 -1
- package/service-layer/ServiceLookup.d.ts +1 -3
- package/service-layer/ServiceLookup.js +10 -10
- package/service-layer/ServiceLookup.js.map +1 -1
- package/service-layer/ServiceRepr.d.ts +2 -11
- package/service-layer/ServiceRepr.js +68 -50
- package/service-layer/ServiceRepr.js.map +1 -1
- package/service-layer/verifyDependencies.js +33 -33
- package/service-layer/verifyDependencies.js.map +1 -1
- package/test-support/index.js +2 -1
- package/test-support/index.js.map +1 -1
package/i18n/AppIntl.js
CHANGED
|
@@ -1,105 +1,223 @@
|
|
|
1
|
-
import { reactive, watchValue, computed, constant } from '@conterra/reactivity-core';
|
|
2
|
-
import { createLogger,
|
|
3
|
-
import { sourceId
|
|
1
|
+
import { reactive, watchValue, batch, computed, constant } from '@conterra/reactivity-core';
|
|
2
|
+
import { createLogger, destroyResource, throwAbortError, shallowEqual, Error } from '@open-pioneer/core';
|
|
3
|
+
import { sourceId } from '../_virtual/source-info.js';
|
|
4
4
|
import { ErrorId } from '../errors.js';
|
|
5
5
|
import { unwrapBox } from '../metadata/ObservableBox.js';
|
|
6
|
+
import { tryParseLocale } from './intl-locale.js';
|
|
6
7
|
import { createPackageIntl } from './PackageIntl.js';
|
|
7
|
-
import { getBrowserLocales,
|
|
8
|
+
import { getBrowserLocales, LocalePicker } from './pick.js';
|
|
8
9
|
|
|
9
10
|
const LOG = createLogger(sourceId);
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
", "
|
|
19
|
-
)}) [forcedLocale=${forcedLocale}].`
|
|
20
|
-
);
|
|
11
|
+
class AppIntl {
|
|
12
|
+
/**
|
|
13
|
+
* Initializes the application's locale and fetches the appropriate i18n messages.
|
|
14
|
+
*/
|
|
15
|
+
static async create(options) {
|
|
16
|
+
const appIntl = new AppIntl(options);
|
|
17
|
+
await appIntl.#loadInitialMessages();
|
|
18
|
+
return appIntl;
|
|
21
19
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
20
|
+
#appMetadata;
|
|
21
|
+
#effectiveSupportedLocales;
|
|
22
|
+
#userLocales;
|
|
23
|
+
#localePicker;
|
|
24
|
+
#supportsLiveChanges;
|
|
25
|
+
#restartWithLocale;
|
|
26
|
+
// Messages are reactive: they can change if the locale changes at runtime
|
|
27
|
+
// or if the developer edits i18n files on disk during development.
|
|
28
|
+
#messages;
|
|
29
|
+
// Locale and messageLocale support reactive changes if `supportsLiveChanges` is enabled.
|
|
30
|
+
#locale;
|
|
31
|
+
#messageLocale;
|
|
32
|
+
/** During dev: watch for changes of the loadMessage function. */
|
|
33
|
+
#hmrWatch;
|
|
34
|
+
/** Monotonic counter to discard outdated changeLocale results. */
|
|
35
|
+
#changeLocaleSeq = 0;
|
|
36
|
+
constructor(options) {
|
|
37
|
+
const {
|
|
38
|
+
appMetadata,
|
|
39
|
+
forcedLocale,
|
|
40
|
+
restrictSupportedLocales,
|
|
41
|
+
supportsLiveChanges = false,
|
|
42
|
+
restartWithLocale
|
|
43
|
+
} = options;
|
|
44
|
+
const messageLocaleStrings = appMetadata?.locales ?? [];
|
|
45
|
+
const effectiveSupportedLocales = filterAvailableLocales(
|
|
46
|
+
messageLocaleStrings,
|
|
47
|
+
restrictSupportedLocales
|
|
48
|
+
);
|
|
49
|
+
const userLocales = getBrowserLocales();
|
|
50
|
+
const preferredLocale = tryParseLocale(forcedLocale);
|
|
51
|
+
if (LOG.isDebug()) {
|
|
52
|
+
const userLocalesList = userLocales.map((l) => l.baseName).join(", ");
|
|
53
|
+
const appLocalesList = messageLocaleStrings.join(", ");
|
|
54
|
+
const effectiveLocalesList = effectiveSupportedLocales.map((l) => l.baseName).join(", ");
|
|
55
|
+
LOG.debug(
|
|
56
|
+
`Attempting to pick locale for user (locales: ${userLocalesList}) from app (locales: ${appLocalesList}; restricted to ${effectiveLocalesList}) [forcedLocale=${preferredLocale?.baseName}].`
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
const localePicker = new LocalePicker(effectiveSupportedLocales);
|
|
60
|
+
const { locale: initialLocale, messageLocale: initialMessageLocale } = localePicker.pickSupportedLocale(preferredLocale, userLocales);
|
|
61
|
+
if (LOG.isDebug()) {
|
|
62
|
+
LOG.debug(
|
|
63
|
+
`Using locale '${initialLocale.baseName}' with messages from locale '${initialMessageLocale.baseName}'.`
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
this.#appMetadata = appMetadata;
|
|
67
|
+
this.#effectiveSupportedLocales = effectiveSupportedLocales;
|
|
68
|
+
this.#userLocales = userLocales;
|
|
69
|
+
this.#localePicker = localePicker;
|
|
70
|
+
this.#supportsLiveChanges = supportsLiveChanges;
|
|
71
|
+
this.#restartWithLocale = restartWithLocale;
|
|
72
|
+
this.#messages = reactive({});
|
|
73
|
+
this.#locale = reactive(initialLocale);
|
|
74
|
+
this.#messageLocale = reactive(initialMessageLocale);
|
|
75
|
+
if (import.meta.hot) {
|
|
76
|
+
this.#hmrWatch = watchValue(
|
|
77
|
+
() => this.#appMetadata?.loadMessages ? unwrapBox(this.#appMetadata.loadMessages) : void 0,
|
|
78
|
+
(loader) => {
|
|
79
|
+
if (!loader) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
this.#applyHotUpdate(loader).catch((e) => {
|
|
83
|
+
LOG.error(`Failed to load messages after hot reload`, e);
|
|
84
|
+
});
|
|
49
85
|
}
|
|
50
86
|
);
|
|
51
87
|
}
|
|
52
88
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
89
|
+
destroy() {
|
|
90
|
+
this.#hmrWatch = destroyResource(this.#hmrWatch);
|
|
91
|
+
}
|
|
92
|
+
/** Locale for Intl formatting. */
|
|
93
|
+
get locale() {
|
|
94
|
+
return this.#locale.value;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The locale of the currently loaded message bundle.
|
|
98
|
+
* Always one of {@link supportedMessageLocales}.
|
|
99
|
+
*/
|
|
100
|
+
get messageLocale() {
|
|
101
|
+
return this.#messageLocale.value;
|
|
102
|
+
}
|
|
103
|
+
/** Supported locales from app metadata. */
|
|
104
|
+
get supportedMessageLocales() {
|
|
105
|
+
return this.#effectiveSupportedLocales;
|
|
106
|
+
}
|
|
107
|
+
/** True if reactive locale switching is enabled. */
|
|
108
|
+
get supportsLiveChanges() {
|
|
109
|
+
return this.#supportsLiveChanges;
|
|
110
|
+
}
|
|
111
|
+
/** True iff `locale` best-fits a supported bundle. */
|
|
112
|
+
supportsLocale(locale) {
|
|
113
|
+
return this.#localePicker.supportsLocale(locale);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Switches to `locale`. Best-fit match; throws `UNSUPPORTED_LOCALE` on no match.
|
|
117
|
+
*/
|
|
118
|
+
async changeLocale(targetLocale) {
|
|
119
|
+
const { locale: nextLocale, messageLocale: nextMessageLocale } = this.#localePicker.pickSupportedLocale(targetLocale, this.#userLocales);
|
|
120
|
+
if (!this.#supportsLiveChanges) {
|
|
121
|
+
this.#restartWithLocale(targetLocale);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const seq = ++this.#changeLocaleSeq;
|
|
125
|
+
const newMessages = await loadMessagesSafely(this.#appMetadata, nextMessageLocale);
|
|
126
|
+
if (seq !== this.#changeLocaleSeq) {
|
|
127
|
+
throwAbortError();
|
|
128
|
+
}
|
|
129
|
+
batch(() => {
|
|
130
|
+
this.#messages.value = newMessages;
|
|
131
|
+
this.#locale.value = nextLocale;
|
|
132
|
+
this.#messageLocale.value = nextMessageLocale;
|
|
133
|
+
});
|
|
134
|
+
if (LOG.isDebug()) {
|
|
135
|
+
LOG.debug(
|
|
136
|
+
`Locale switched: locale='${nextLocale.baseName}', messageLocale='${nextMessageLocale.baseName}'.`
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/** Given the package name, constructs a package i18n instance. */
|
|
141
|
+
createPackageI18n(packageName) {
|
|
142
|
+
const makeIntl = (packageMessages) => createPackageIntl(this.#locale.value.baseName, packageMessages);
|
|
143
|
+
if (import.meta.hot || this.#supportsLiveChanges) {
|
|
144
|
+
const packageMessages = computed(() => this.#messages.value[packageName] ?? {}, {
|
|
145
|
+
equal: shallowEqual
|
|
146
|
+
});
|
|
147
|
+
let firstCall = true;
|
|
148
|
+
return computed(() => {
|
|
149
|
+
if (!firstCall) {
|
|
150
|
+
LOG.info("Updating i18n messages of package", packageName);
|
|
65
151
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
152
|
+
firstCall = false;
|
|
153
|
+
return makeIntl(packageMessages.value);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
return constant(makeIntl(this.#messages.value[packageName] ?? {}));
|
|
157
|
+
}
|
|
158
|
+
/** Loads the initial message bundle for the picked locale. Called once, from {@link create}. */
|
|
159
|
+
async #loadInitialMessages() {
|
|
160
|
+
this.#messages.value = await loadMessagesSafely(
|
|
161
|
+
this.#appMetadata,
|
|
162
|
+
this.#messageLocale.value
|
|
70
163
|
);
|
|
71
164
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
LOG.info("Updating i18n messages of package", packageName);
|
|
90
|
-
}
|
|
91
|
-
firstCall = false;
|
|
92
|
-
return createPackageIntl(locale, packageMessages.value);
|
|
93
|
-
});
|
|
94
|
-
return packageIntl;
|
|
95
|
-
} else {
|
|
96
|
-
const packageMessages = messages.value[packageName] ?? {};
|
|
97
|
-
const packageIntl = createPackageIntl(locale, packageMessages);
|
|
98
|
-
return constant(packageIntl);
|
|
165
|
+
async #applyHotUpdate(loader) {
|
|
166
|
+
const newMessages = await loader(this.#messageLocale.value.baseName) ?? {};
|
|
167
|
+
LOG.debug("Applying new i18n messages", newMessages);
|
|
168
|
+
this.#messages.value = newMessages;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
function filterAvailableLocales(messageLocaleStrings, restrictSupportedLocales) {
|
|
172
|
+
const isRestricted = restrictSupportedLocales != null;
|
|
173
|
+
const localesToSupport = new Set(restrictSupportedLocales ?? messageLocaleStrings);
|
|
174
|
+
const supportedLocales = [];
|
|
175
|
+
for (const l of localesToSupport) {
|
|
176
|
+
if (!messageLocaleStrings.includes(l)) {
|
|
177
|
+
if (isRestricted) {
|
|
178
|
+
throw new Error(
|
|
179
|
+
ErrorId.UNSUPPORTED_LOCALE,
|
|
180
|
+
`Configured supported locale '${l}' is not one of the application's message locales [${messageLocaleStrings.join(", ")}].`
|
|
181
|
+
);
|
|
99
182
|
}
|
|
183
|
+
LOG.warn(`Locale '${l}' is not included in app metadata locales and will be ignored.`);
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
const locale = tryParseLocale(l);
|
|
187
|
+
if (!locale) {
|
|
188
|
+
LOG.warn(
|
|
189
|
+
`Locale '${l}' from app metadata is not a valid BCP 47 tag and will be ignored.`
|
|
190
|
+
);
|
|
191
|
+
continue;
|
|
100
192
|
}
|
|
101
|
-
|
|
193
|
+
supportedLocales.push(locale);
|
|
194
|
+
}
|
|
195
|
+
if (supportedLocales.length === 0) {
|
|
196
|
+
LOG.debug(
|
|
197
|
+
`No valid supported locales found. Locale picking and switching will be unavailable.`
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
return supportedLocales;
|
|
201
|
+
}
|
|
202
|
+
async function loadMessagesSafely(appMetadata, messageLocale) {
|
|
203
|
+
const messageLocales = appMetadata?.locales ?? [];
|
|
204
|
+
if (!appMetadata?.loadMessages || !messageLocales.includes(messageLocale.baseName)) {
|
|
205
|
+
return {};
|
|
206
|
+
}
|
|
207
|
+
try {
|
|
208
|
+
const loader = unwrapBox(appMetadata.loadMessages);
|
|
209
|
+
const messagesRecord = await loader(messageLocale.baseName) ?? {};
|
|
210
|
+
return messagesRecord;
|
|
211
|
+
} catch (e) {
|
|
212
|
+
throw new Error(
|
|
213
|
+
ErrorId.INTERNAL,
|
|
214
|
+
`Failed to load messages for locale '${messageLocale.baseName}'.`,
|
|
215
|
+
{
|
|
216
|
+
cause: e
|
|
217
|
+
}
|
|
218
|
+
);
|
|
219
|
+
}
|
|
102
220
|
}
|
|
103
221
|
|
|
104
|
-
export {
|
|
222
|
+
export { AppIntl };
|
|
105
223
|
//# sourceMappingURL=AppIntl.js.map
|
package/i18n/AppIntl.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AppIntl.js","sources":["AppIntl.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport {\n computed,\n constant,\n reactive,\n ReadonlyReactive,\n watchValue\n} from \"@conterra/reactivity-core\";\nimport { createLogger, destroyResource, Error, Resource, shallowEqual } from \"@open-pioneer/core\";\nimport { sourceId } from \"open-pioneer:source-info\";\nimport { ErrorId } from \"../errors\";\nimport { ApplicationMetadata, MessageLoader, MessagesRecord } from \"../metadata\";\nimport { unwrapBox } from \"../metadata/ObservableBox\";\nimport { createPackageIntl, PackageIntl } from \"./PackageIntl\";\nimport { getBrowserLocales, I18nConfig } from \"./pick\";\n\nconst LOG = createLogger(sourceId);\n\n/**\n * Represents i18n info for the entire application.\n * Currently not exposed to user code.\n */\nexport interface AppIntl {\n destroy(): void;\n\n /** Chosen locale */\n readonly locale: string;\n\n /** Supported locales from app metadata. */\n readonly supportedMessageLocales: string[];\n\n /** True if the locale can be used in this application (i.e. if there are any messages). */\n supportsLocale(locale: string): boolean;\n\n /** Given the package name, constructs a package i18n instance. */\n createPackageI18n(packageName: string): ReadonlyReactive<PackageIntl>;\n}\n\n/**\n * Initializes the application's locale and fetches the appropriate i18n messages.\n */\nexport async function initI18n(\n appMetadata: ApplicationMetadata | undefined,\n forcedLocale: string | undefined\n): Promise<AppIntl> {\n const messageLocales = appMetadata?.locales ?? [];\n const userLocales = getBrowserLocales();\n if (LOG.isDebug()) {\n LOG.debug(\n `Attempting to pick locale for user (locales: ${userLocales.join(\n \", \"\n )}) from app (supported locales: ${messageLocales.join(\n \", \"\n )}) [forcedLocale=${forcedLocale}].`\n );\n }\n\n const i18nConfig = new I18nConfig(messageLocales);\n const { locale, messageLocale } = i18nConfig.pickSupportedLocale(forcedLocale, userLocales);\n\n if (LOG.isDebug()) {\n LOG.debug(`Using locale '${locale}' with messages from locale '${messageLocale}'.`);\n }\n\n const messages = reactive<MessagesRecord>({});\n\n // Initial load of i18n messages.\n // During development, i18n messages are also loaded when they change on disk (see below).\n if (appMetadata?.loadMessages && messageLocales.includes(messageLocale)) {\n try {\n const loader = unwrapBox(appMetadata.loadMessages);\n const messagesRecord = await loader(messageLocale);\n if (messagesRecord) {\n messages.value = messagesRecord;\n } else {\n messages.value = {};\n console.warn(\n `I18n messages couldn't be loaded. Check if your runtimeMeta version is not set to 1.0.0.`\n );\n if (LOG.isDebug()) {\n LOG.debug(`appMetadata.loadMessages doesn't support signal value'.`);\n }\n }\n } catch (e) {\n throw new Error(\n ErrorId.INTERNAL,\n `Failed to load messages for locale '${messageLocale}'.`,\n {\n cause: e\n }\n );\n }\n }\n\n // During dev: watch for changes of the loadMessage function\n // and fetch new I18N messages if the user edited any i18n file.\n let hmrWatch: Resource | undefined;\n if (import.meta.hot) {\n const handleHotUpdate = async (loader: MessageLoader) => {\n const newMessages = (await loader?.(messageLocale)) ?? {};\n LOG.debug(\"Applying new i18n messages\", newMessages);\n messages.value = newMessages;\n };\n\n hmrWatch = watchValue(\n () => (appMetadata?.loadMessages ? unwrapBox(appMetadata.loadMessages) : undefined),\n (loader) => {\n if (!loader) {\n return;\n }\n handleHotUpdate(loader).catch((e) => {\n LOG.error(`Failed to load messages after hot reload`, e);\n });\n }\n );\n }\n\n return {\n locale,\n supportedMessageLocales: messageLocales,\n destroy() {\n hmrWatch = destroyResource(hmrWatch);\n },\n supportsLocale(locale) {\n return i18nConfig.supportsLocale(locale);\n },\n createPackageI18n(packageName) {\n if (import.meta.hot) {\n const packageMessages = computed(() => messages.value[packageName] ?? {}, {\n equal: shallowEqual\n });\n\n let firstCall = true;\n const packageIntl = computed(() => {\n if (!firstCall) {\n LOG.info(\"Updating i18n messages of package\", packageName);\n }\n firstCall = false;\n return createPackageIntl(locale, packageMessages.value);\n });\n return packageIntl;\n } else {\n const packageMessages = messages.value[packageName] ?? {};\n const packageIntl = createPackageIntl(locale, packageMessages);\n return constant(packageIntl);\n }\n }\n };\n}\n"],"names":["locale"],"mappings":";;;;;;;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A,CAAW,eAAeA,OAAM,CAAA;AAAA,IAC3C,CAAA;AAAA,IACA,kBAAkB,WAAA,EAAa;AAC3B,MAAA,IAAI,YAAY,GAAA,EAAK;AACjB,QAAA,MAAM,eAAA,GAAkB,SAAS,MAAM,QAAA,CAAS,MAAM,WAAW,CAAA,IAAK,EAAC,EAAG;AAAA,UACtE,KAAA,EAAO;AAAA,SACV,CAAA;AAED,QAAA,IAAI,SAAA,GAAY,IAAA;AAChB,QAAA,MAAM,WAAA,GAAc,SAAS,MAAM;AAC/B,UAAA,IAAI,CAAC,SAAA,EAAW;AACZ,YAAA,GAAA,CAAI,IAAA,CAAK,qCAAqC,WAAW,CAAA;AAAA,UAC7D;AACA,UAAA,SAAA,GAAY,KAAA;AACZ,UAAA,OAAO,iBAAA,CAAkB,MAAA,EAAQ,eAAA,CAAgB,KAAK,CAAA;AAAA,QAC1D,CAAC,CAAA;AACD,QAAA,OAAO,WAAA;AAAA,MACX,CAAA,MAAO;AACH,QAAA,MAAM,eAAA,GAAkB,QAAA,CAAS,KAAA,CAAM,WAAW,KAAK,EAAC;AACxD,QAAA,MAAM,WAAA,GAAc,iBAAA,CAAkB,MAAA,EAAQ,eAAe,CAAA;AAC7D,QAAA,OAAO,SAAS,WAAW,CAAA;AAAA,MAC/B;AAAA,IACJ;AAAA,GACJ;AACJ;;;;"}
|
|
1
|
+
{"version":3,"file":"AppIntl.js","sources":["AppIntl.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport {\n batch,\n computed,\n constant,\n reactive,\n Reactive,\n ReadonlyReactive,\n watchValue\n} from \"@conterra/reactivity-core\";\nimport {\n createLogger,\n destroyResource,\n Error,\n Resource,\n shallowEqual,\n throwAbortError\n} from \"@open-pioneer/core\";\nimport { sourceId } from \"open-pioneer:source-info\";\nimport { ErrorId } from \"../errors\";\nimport { ApplicationMetadata, MessageLoader, MessagesRecord } from \"../metadata\";\nimport { unwrapBox } from \"../metadata/ObservableBox\";\nimport { tryParseLocale } from \"./intl-locale\";\nimport { createPackageIntl, PackageIntl } from \"./PackageIntl\";\nimport { LocalePicker, getBrowserLocales } from \"./pick\";\n\nconst LOG = createLogger(sourceId);\n\n/**\n * Options for {@link AppIntl.create}.\n */\nexport interface I18nOptions {\n // build metadata\n appMetadata: ApplicationMetadata | undefined;\n\n // locale override (e.g. from config or URL param)\n forcedLocale?: string | undefined;\n\n /**\n * Restriction on supported locales.\n * If provided, only these locales are considered for locale picking and switching.\n * If not provided, all locales from app metadata are supported.\n */\n restrictSupportedLocales?: readonly string[];\n\n /**\n * If true, locale switching via {@link AppIntl.changeLocale} is supported and\n * applied in place (locale, messageLocale and PackageIntl instances become reactive).\n *\n * If false (default), {@link AppIntl.changeLocale} triggers an application\n * restart via {@link restartWithLocale}.\n */\n supportsLiveChanges?: boolean;\n\n /** hook given by AppInstance to trigger restart of the application. Called by changeLocale when reactive switching is OFF. */\n restartWithLocale(locale: Intl.Locale | undefined): void;\n}\n\n/**\n * Represents i18n info for the entire application.\n * Not exposed to user code.\n *\n * @internal\n */\nexport class AppIntl {\n /**\n * Initializes the application's locale and fetches the appropriate i18n messages.\n */\n static async create(options: I18nOptions): Promise<AppIntl> {\n const appIntl = new AppIntl(options);\n await appIntl.#loadInitialMessages();\n return appIntl;\n }\n\n readonly #appMetadata: ApplicationMetadata | undefined;\n readonly #effectiveSupportedLocales: Intl.Locale[];\n readonly #userLocales: Intl.Locale[];\n readonly #localePicker: LocalePicker;\n readonly #supportsLiveChanges: boolean;\n readonly #restartWithLocale: (locale: Intl.Locale | undefined) => void;\n\n // Messages are reactive: they can change if the locale changes at runtime\n // or if the developer edits i18n files on disk during development.\n readonly #messages: Reactive<MessagesRecord>;\n\n // Locale and messageLocale support reactive changes if `supportsLiveChanges` is enabled.\n readonly #locale: Reactive<Intl.Locale>;\n readonly #messageLocale: Reactive<Intl.Locale>;\n\n /** During dev: watch for changes of the loadMessage function. */\n #hmrWatch: Resource | undefined;\n\n /** Monotonic counter to discard outdated changeLocale results. */\n #changeLocaleSeq = 0;\n\n private constructor(options: I18nOptions) {\n const {\n appMetadata,\n forcedLocale,\n restrictSupportedLocales,\n supportsLiveChanges = false,\n restartWithLocale\n } = options;\n\n const messageLocaleStrings = appMetadata?.locales ?? [];\n const effectiveSupportedLocales = filterAvailableLocales(\n messageLocaleStrings,\n restrictSupportedLocales\n );\n const userLocales = getBrowserLocales();\n const preferredLocale = tryParseLocale(forcedLocale);\n if (LOG.isDebug()) {\n const userLocalesList = userLocales.map((l) => l.baseName).join(\", \");\n const appLocalesList = messageLocaleStrings.join(\", \");\n const effectiveLocalesList = effectiveSupportedLocales\n .map((l) => l.baseName)\n .join(\", \");\n LOG.debug(\n `Attempting to pick locale for user (locales: ${userLocalesList}) from app (locales: ${appLocalesList}; ` +\n `restricted to ${effectiveLocalesList}) [forcedLocale=${preferredLocale?.baseName}].`\n );\n }\n\n const localePicker = new LocalePicker(effectiveSupportedLocales);\n const { locale: initialLocale, messageLocale: initialMessageLocale } =\n localePicker.pickSupportedLocale(preferredLocale, userLocales);\n\n if (LOG.isDebug()) {\n LOG.debug(\n `Using locale '${initialLocale.baseName}' with messages from locale '${initialMessageLocale.baseName}'.`\n );\n }\n\n this.#appMetadata = appMetadata;\n this.#effectiveSupportedLocales = effectiveSupportedLocales;\n this.#userLocales = userLocales;\n this.#localePicker = localePicker;\n this.#supportsLiveChanges = supportsLiveChanges;\n this.#restartWithLocale = restartWithLocale;\n\n // Initially empty; populated by #loadInitialMessages() before the instance is handed out.\n this.#messages = reactive<MessagesRecord>({});\n this.#locale = reactive(initialLocale);\n this.#messageLocale = reactive(initialMessageLocale);\n\n // During dev: watch for changes of the loadMessage function\n // and fetch new I18N messages if the user edited any i18n file.\n if (import.meta.hot) {\n this.#hmrWatch = watchValue(\n () =>\n this.#appMetadata?.loadMessages\n ? unwrapBox(this.#appMetadata.loadMessages)\n : undefined,\n (loader) => {\n if (!loader) {\n return;\n }\n this.#applyHotUpdate(loader).catch((e) => {\n LOG.error(`Failed to load messages after hot reload`, e);\n });\n }\n );\n }\n }\n\n destroy(): void {\n this.#hmrWatch = destroyResource(this.#hmrWatch);\n }\n\n /** Locale for Intl formatting. */\n get locale(): Intl.Locale {\n return this.#locale.value;\n }\n\n /**\n * The locale of the currently loaded message bundle.\n * Always one of {@link supportedMessageLocales}.\n */\n get messageLocale(): Intl.Locale {\n return this.#messageLocale.value;\n }\n\n /** Supported locales from app metadata. */\n get supportedMessageLocales(): Intl.Locale[] {\n return this.#effectiveSupportedLocales;\n }\n\n /** True if reactive locale switching is enabled. */\n get supportsLiveChanges(): boolean {\n return this.#supportsLiveChanges;\n }\n\n /** True iff `locale` best-fits a supported bundle. */\n supportsLocale(locale: Intl.Locale): boolean {\n return this.#localePicker.supportsLocale(locale);\n }\n\n /**\n * Switches to `locale`. Best-fit match; throws `UNSUPPORTED_LOCALE` on no match.\n */\n async changeLocale(targetLocale: Intl.Locale | undefined): Promise<void> {\n const { locale: nextLocale, messageLocale: nextMessageLocale } =\n this.#localePicker.pickSupportedLocale(targetLocale, this.#userLocales);\n if (!this.#supportsLiveChanges) {\n //NOTE: it is important for restarts to ensure the input value (targetLocale) is passed to restartWithLocale, not the best-fit value (nextLocale).\n this.#restartWithLocale(targetLocale);\n return;\n }\n const seq = ++this.#changeLocaleSeq;\n const newMessages = await loadMessagesSafely(this.#appMetadata, nextMessageLocale);\n if (seq !== this.#changeLocaleSeq) {\n // Superseded by a newer call.\n throwAbortError();\n }\n\n batch(() => {\n this.#messages.value = newMessages;\n this.#locale.value = nextLocale;\n this.#messageLocale.value = nextMessageLocale;\n });\n if (LOG.isDebug()) {\n LOG.debug(\n `Locale switched: locale='${nextLocale.baseName}', messageLocale='${nextMessageLocale.baseName}'.`\n );\n }\n }\n\n /** Given the package name, constructs a package i18n instance. */\n createPackageI18n(packageName: string): ReadonlyReactive<PackageIntl> {\n //NOTE: locale instead of messageLocale is intentional,\n // to ensure that number formatting is still according to the current locale\n const makeIntl = (packageMessages: Record<string, string>) =>\n createPackageIntl(this.#locale.value.baseName, packageMessages);\n\n if (import.meta.hot || this.#supportsLiveChanges) {\n const packageMessages = computed(() => this.#messages.value[packageName] ?? {}, {\n equal: shallowEqual\n });\n\n let firstCall = true;\n return computed(() => {\n if (!firstCall) {\n LOG.info(\"Updating i18n messages of package\", packageName);\n }\n firstCall = false;\n return makeIntl(packageMessages.value);\n });\n }\n return constant(makeIntl(this.#messages.value[packageName] ?? {}));\n }\n\n /** Loads the initial message bundle for the picked locale. Called once, from {@link create}. */\n async #loadInitialMessages(): Promise<void> {\n this.#messages.value = await loadMessagesSafely(\n this.#appMetadata,\n this.#messageLocale.value\n );\n }\n\n async #applyHotUpdate(loader: MessageLoader): Promise<void> {\n const newMessages = (await loader(this.#messageLocale.value.baseName)) ?? {};\n LOG.debug(\"Applying new i18n messages\", newMessages);\n this.#messages.value = newMessages;\n }\n}\n\nfunction filterAvailableLocales(\n // locales from app metadata\n messageLocaleStrings: readonly string[],\n // optional restriction on supported locales, must be subset of messageLocaleStrings\n restrictSupportedLocales: readonly string[] | undefined\n): Intl.Locale[] {\n //NOTE: the set preserves the order of 'restrictSupportedLocales', this is relevant and intentional.\n const isRestricted = restrictSupportedLocales != null;\n const localesToSupport = new Set(restrictSupportedLocales ?? messageLocaleStrings);\n const supportedLocales: Intl.Locale[] = [];\n for (const l of localesToSupport) {\n if (!messageLocaleStrings.includes(l)) {\n // 'supportedLocales' may only restrict the locales defined by the application, not extend them.\n // A value outside the application's message locales is a configuration error.\n if (isRestricted) {\n throw new Error(\n ErrorId.UNSUPPORTED_LOCALE,\n `Configured supported locale '${l}' is not one of the application's message locales [${messageLocaleStrings.join(\", \")}].`\n );\n }\n LOG.warn(`Locale '${l}' is not included in app metadata locales and will be ignored.`);\n continue;\n }\n const locale = tryParseLocale(l);\n if (!locale) {\n LOG.warn(\n `Locale '${l}' from app metadata is not a valid BCP 47 tag and will be ignored.`\n );\n continue;\n }\n supportedLocales.push(locale);\n }\n if (supportedLocales.length === 0) {\n LOG.debug(\n `No valid supported locales found. Locale picking and switching will be unavailable.`\n );\n }\n return supportedLocales;\n}\n\nasync function loadMessagesSafely(\n appMetadata: ApplicationMetadata | undefined,\n messageLocale: Intl.Locale\n): Promise<MessagesRecord> {\n const messageLocales = appMetadata?.locales ?? [];\n if (!appMetadata?.loadMessages || !messageLocales.includes(messageLocale.baseName)) {\n return {};\n }\n\n try {\n const loader = unwrapBox(appMetadata.loadMessages);\n const messagesRecord = (await loader(messageLocale.baseName)) ?? {};\n return messagesRecord;\n } catch (e) {\n throw new Error(\n ErrorId.INTERNAL,\n `Failed to load messages for locale '${messageLocale.baseName}'.`,\n {\n cause: e\n }\n );\n }\n}\n"],"names":[],"mappings":";;;;;;;;;AA2BA,MAAM,GAAA,GAAM,aAAa,QAAQ,CAAA;AAsC1B,MAAM,OAAA,CAAQ;AAAA;AAAA;AAAA;AAAA,EAIjB,aAAa,OAAO,OAAA,EAAwC;AACxD,IAAA,MAAM,OAAA,GAAU,IAAI,OAAA,CAAQ,OAAO,CAAA;AACnC,IAAA,MAAM,QAAQ,oBAAA,EAAqB;AACnC,IAAA,OAAO,OAAA;AAAA,EACX;AAAA,EAES,YAAA;AAAA,EACA,0BAAA;AAAA,EACA,YAAA;AAAA,EACA,aAAA;AAAA,EACA,oBAAA;AAAA,EACA,kBAAA;AAAA;AAAA;AAAA,EAIA,SAAA;AAAA;AAAA,EAGA,OAAA;AAAA,EACA,cAAA;AAAA;AAAA,EAGT,SAAA;AAAA;AAAA,EAGA,gBAAA,GAAmB,CAAA;AAAA,EAEX,YAAY,OAAA,EAAsB;AACtC,IAAA,MAAM;AAAA,MACF,WAAA;AAAA,MACA,YAAA;AAAA,MACA,wBAAA;AAAA,MACA,mBAAA,GAAsB,KAAA;AAAA,MACtB;AAAA,KACJ,GAAI,OAAA;AAEJ,IAAA,MAAM,oBAAA,GAAuB,WAAA,EAAa,OAAA,IAAW,EAAC;A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,iBAAkB,MAAM,MAAA,CAAO,aAAA,CAAc,QAAQ,KAAM,EAAC;AAClE,IAAA,OAAO,cAAA;AAAA,EACX,SAAS,CAAA,EAAG;AACR,IAAA,MAAM,IAAI,KAAA;AAAA,MACN,OAAA,CAAQ,QAAA;AAAA,MACR,CAAA,oCAAA,EAAuC,cAAc,QAAQ,CAAA,EAAA,CAAA;AAAA,MAC7D;AAAA,QACI,KAAA,EAAO;AAAA;AACX,KACJ;AAAA,EACJ;AACJ;;;;"}
|
package/i18n/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { AppIntl, type I18nOptions } from "./AppIntl";
|
|
2
|
+
export { parseLocale, tryParseLocale } from "./intl-locale";
|
|
2
3
|
export { type PackageIntl, type PackageIntlExtensions, type RichTextValue, createPackageIntl, createEmptyPackageIntl } from "./PackageIntl";
|
|
3
|
-
export { type LocalePickResult,
|
|
4
|
+
export { type LocalePickResult, LocalePicker, getBrowserLocales } from "./pick";
|
package/i18n/index.js
CHANGED
package/i18n/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses a BCP-47 locale tag into a {@link Intl.Locale}.
|
|
3
|
+
*
|
|
4
|
+
* The language, script, region, and variant subtags are preserved and
|
|
5
|
+
* canonicalized. Unicode extension keys (e.g. `-u-ca-gregory`) are stripped.
|
|
6
|
+
*
|
|
7
|
+
* Throws a `RangeError` if the input is malformed.
|
|
8
|
+
*/
|
|
9
|
+
export declare function parseLocale(input: string): Intl.Locale;
|
|
10
|
+
/**
|
|
11
|
+
* Like {@link parseLocale}, but returns `undefined` instead of throwing on malformed input.
|
|
12
|
+
*/
|
|
13
|
+
export declare function tryParseLocale(input: string | undefined): Intl.Locale | undefined;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
function parseLocale(input) {
|
|
2
|
+
const intlLocale = new Intl.Locale(input);
|
|
3
|
+
return new Intl.Locale(intlLocale.baseName);
|
|
4
|
+
}
|
|
5
|
+
function tryParseLocale(input) {
|
|
6
|
+
if (input == null) {
|
|
7
|
+
return void 0;
|
|
8
|
+
}
|
|
9
|
+
try {
|
|
10
|
+
return parseLocale(input);
|
|
11
|
+
} catch {
|
|
12
|
+
return void 0;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { parseLocale, tryParseLocale };
|
|
17
|
+
//# sourceMappingURL=intl-locale.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"intl-locale.js","sources":["intl-locale.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\n\n/**\n * Parses a BCP-47 locale tag into a {@link Intl.Locale}.\n *\n * The language, script, region, and variant subtags are preserved and\n * canonicalized. Unicode extension keys (e.g. `-u-ca-gregory`) are stripped.\n *\n * Throws a `RangeError` if the input is malformed.\n */\nexport function parseLocale(input: string): Intl.Locale {\n const intlLocale = new Intl.Locale(input);\n\n // Reconstruct an Intl.Locale that contains only the language-identifier\n // subtags (language + script + region + variants), dropping any Unicode\n // extension keys such as -u-ca-gregory.\n //\n // intlLocale.baseName is already the canonical language identifier without\n // extension keys, so it is exactly what we want. Re-wrapping it in a new\n // Intl.Locale gives us a clean object with no extension metadata.\n return new Intl.Locale(intlLocale.baseName);\n}\n\n/**\n * Like {@link parseLocale}, but returns `undefined` instead of throwing on malformed input.\n */\nexport function tryParseLocale(input: string | undefined): Intl.Locale | undefined {\n if (input == null) {\n return undefined;\n }\n try {\n return parseLocale(input);\n } catch {\n return undefined;\n }\n}\n"],"names":[],"mappings":"AAWO,SAAS,YAAY,KAAA,EAA4B;AACpD,EAAA,MAAM,UAAA,GAAa,IAAI,IAAA,CAAK,MAAA,CAAO,KAAK,CAAA;AASxC,EAAA,OAAO,IAAI,IAAA,CAAK,MAAA,CAAO,UAAA,CAAW,QAAQ,CAAA;AAC9C;AAKO,SAAS,eAAe,KAAA,EAAoD;AAC/E,EAAA,IAAI,SAAS,IAAA,EAAM;AACf,IAAA,OAAO,MAAA;AAAA,EACX;AACA,EAAA,IAAI;AACA,IAAA,OAAO,YAAY,KAAK,CAAA;AAAA,EAC5B,CAAA,CAAA,MAAQ;AACJ,IAAA,OAAO,MAAA;AAAA,EACX;AACJ;;;;"}
|
package/i18n/pick.d.ts
CHANGED
|
@@ -1,33 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Result of {@link LocalePicker.pickSupportedLocale}.
|
|
3
|
+
*/
|
|
1
4
|
export interface LocalePickResult {
|
|
2
5
|
/**
|
|
3
|
-
* The actual locale (e.g.
|
|
6
|
+
* The actual locale (e.g. `de-DE`) used for number/date formatting.
|
|
4
7
|
*/
|
|
5
|
-
locale:
|
|
8
|
+
locale: Intl.Locale;
|
|
6
9
|
/**
|
|
7
|
-
* The locale
|
|
10
|
+
* The locale of the matched message bundle (e.g. `de`).
|
|
8
11
|
*/
|
|
9
|
-
messageLocale:
|
|
12
|
+
messageLocale: Intl.Locale;
|
|
10
13
|
}
|
|
11
14
|
/**
|
|
12
|
-
* Picks a locale for the
|
|
15
|
+
* Picks a locale for an application based on the locales it supports
|
|
16
|
+
* and the locales requested by a user.
|
|
13
17
|
*/
|
|
14
|
-
export declare class
|
|
15
|
-
private
|
|
18
|
+
export declare class LocalePicker {
|
|
19
|
+
#private;
|
|
16
20
|
/**
|
|
17
|
-
* @param
|
|
21
|
+
* @param supportedLocales Locales the app has i18n messages for (e.g. `"en"`, `"de"`, `"de-CH"`).
|
|
22
|
+
* May be empty; in that case the picker behaves as if a single `"en"`
|
|
23
|
+
* supported locale were configured.
|
|
18
24
|
*/
|
|
19
|
-
constructor(
|
|
25
|
+
constructor(supportedLocales: ReadonlyArray<Intl.Locale>);
|
|
20
26
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
27
|
+
* Picks the locale to use during application startup or for a runtime locale switch.
|
|
28
|
+
*
|
|
29
|
+
* @param preferredLocale Optional preferred (forced) locale.
|
|
30
|
+
* @param userLocales Locales requested by the user's browser, in priority order.
|
|
23
31
|
*/
|
|
24
|
-
pickSupportedLocale(
|
|
25
|
-
|
|
26
|
-
|
|
32
|
+
pickSupportedLocale(preferredLocale: Intl.Locale | undefined, userLocales: Intl.Locale[]): LocalePickResult;
|
|
33
|
+
/**
|
|
34
|
+
* Returns `true` if the given locale is accepted by
|
|
35
|
+
* {@link pickSupportedLocale}.
|
|
36
|
+
*/
|
|
37
|
+
supportsLocale(locale: Intl.Locale): boolean;
|
|
27
38
|
}
|
|
28
39
|
/**
|
|
29
|
-
* Returns locales
|
|
40
|
+
* Returns the locales currently preferred by the user's browser, in order of preference.
|
|
30
41
|
*
|
|
31
42
|
* See also https://developer.mozilla.org/en-US/docs/Web/API/Navigator/languages
|
|
43
|
+
*
|
|
44
|
+
* Invalid entries (which should not occur in practice) are silently dropped.
|
|
32
45
|
*/
|
|
33
|
-
export declare function getBrowserLocales():
|
|
46
|
+
export declare function getBrowserLocales(): Intl.Locale[];
|