@descope/sdk-mixins
Version:
Descope JavaScript SDK mixins
34 lines (30 loc) • 1.74 kB
JavaScript
;
var sdkHelpers = require('@descope/sdk-helpers');
/**
* Exposes the widget's locale to mixins that need it (HTML fetching, flow propagation).
* Mirrors web-component's BaseDescopeWc locale handling:
* - `locale` getter returns the raw attribute value, defaulting to '' when unset (like themeMixin's
* theme/styleId), so callers can pass it straight to setAttribute without emitting "undefined".
* - `localeCandidates` returns the lowercased locales to try, most-specific first: the resolved
* locale and then its language-only fallback (e.g. 'en-us' then 'en'), matching the
* web-component's getHtmlFilenameWithLocale probe order. Falls back to navigator.language.
* - `firstAvailableLocale(availableLocales)` returns the most-specific candidate that the consumer
* actually publishes (e.g. a widget's config.json targetLocales), or '' when none match.
*/
const localeMixin = sdkHelpers.createSingletonMixin((superclass) => class LocaleMixinClass extends superclass {
get locale() {
return this.getAttribute('locale') || '';
}
get localeCandidates() {
const { locale, fallback } = sdkHelpers.getUserLocale(this.locale);
return [...new Set([locale, fallback].filter(Boolean))];
}
firstAvailableLocale(availableLocales) {
var _a;
const available = (availableLocales !== null && availableLocales !== void 0 ? availableLocales : []).map((l) => l.toLowerCase());
// localeCandidates are already lowercased, most-specific first.
return (_a = this.localeCandidates.find((c) => available.includes(c))) !== null && _a !== void 0 ? _a : '';
}
});
exports.localeMixin = localeMixin;
//# sourceMappingURL=localeMixin.js.map