@oxy.so/core 1.1.2 → 1.2.1
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/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/index.js +5 -4
- package/dist/cjs/utils/languageUtils.js +26 -0
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/utils/languageUtils.js +25 -0
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/utils/languageUtils.d.ts +13 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/utils/__tests__/languageUtils.test.ts +25 -0
- package/src/utils/languageUtils.ts +29 -0
package/dist/esm/index.js
CHANGED
|
@@ -91,7 +91,7 @@ export { TopicType, TopicSource } from './models/Topic.js';
|
|
|
91
91
|
// ---------------------------------------------------------------------------
|
|
92
92
|
// Languages
|
|
93
93
|
// ---------------------------------------------------------------------------
|
|
94
|
-
export { SUPPORTED_LANGUAGES, FALLBACK_LOCALE, getBaseLanguage, normalizeLocale, isSupportedLocale, getLanguageMetadata, getLanguageName, getNativeLanguageName, isRTLLocale, getUserLanguages, getPrimaryLanguage, } from './utils/languageUtils.js';
|
|
94
|
+
export { SUPPORTED_LANGUAGES, FALLBACK_LOCALE, getBaseLanguage, normalizeLocale, isSupportedLocale, getLanguageMetadata, getLanguageName, getNativeLanguageName, isRTLLocale, getUserLanguages, getPrimaryLanguage, coerceToSupportedLocale, } from './utils/languageUtils.js';
|
|
95
95
|
// ---------------------------------------------------------------------------
|
|
96
96
|
// Platform detection
|
|
97
97
|
// ---------------------------------------------------------------------------
|
|
@@ -210,3 +210,28 @@ export function getUserLanguages(user) {
|
|
|
210
210
|
export function getPrimaryLanguage(user) {
|
|
211
211
|
return getUserLanguages(user)[0];
|
|
212
212
|
}
|
|
213
|
+
/**
|
|
214
|
+
* Coerce a locale down to one from a CALLER-provided catalog: an exact
|
|
215
|
+
* canonical match wins, else the closest entry sharing the same base
|
|
216
|
+
* language (an `es-MX` account reads a host app's `es-ES` catalog), else
|
|
217
|
+
* `fallback`.
|
|
218
|
+
*
|
|
219
|
+
* A host app's own shipped translation catalog is rarely {@link SUPPORTED_LANGUAGES}
|
|
220
|
+
* itself — Oxy's account-locale catalog is broader than any one app's
|
|
221
|
+
* translated strings — so this is the one place that gap is bridged. Every Oxy
|
|
222
|
+
* app was independently re-deriving this exact algorithm against its own
|
|
223
|
+
* catalog before it moved here.
|
|
224
|
+
*/
|
|
225
|
+
export function coerceToSupportedLocale(locale, supportedLocales, fallback) {
|
|
226
|
+
if (locale) {
|
|
227
|
+
const canonical = normalizeLocale(locale);
|
|
228
|
+
if (canonical && supportedLocales.includes(canonical)) {
|
|
229
|
+
return canonical;
|
|
230
|
+
}
|
|
231
|
+
const base = getBaseLanguage(locale);
|
|
232
|
+
const byBase = supportedLocales.find((entry) => getBaseLanguage(entry) === base);
|
|
233
|
+
if (byBase)
|
|
234
|
+
return byBase;
|
|
235
|
+
}
|
|
236
|
+
return fallback;
|
|
237
|
+
}
|