@synergy-design-system/components
Version:
140 lines (137 loc) • 4.49 kB
JavaScript
import {
__spreadValues
} from "./chunk.VK2FVWOF.js";
// src/utilities/localize/core.ts
var connectedElements = /* @__PURE__ */ new Set();
var translations = /* @__PURE__ */ new Map();
var fallback;
var documentDirection = "ltr";
var documentLanguage = "en";
var isClient = typeof MutationObserver !== "undefined" && typeof document !== "undefined" && typeof document.documentElement !== "undefined";
function update() {
if (isClient) {
documentDirection = document.documentElement.dir || "ltr";
documentLanguage = document.documentElement.lang || navigator.language;
}
[...connectedElements.keys()].forEach((el) => {
if (typeof el.requestUpdate === "function") {
el.requestUpdate();
}
});
}
if (isClient) {
const documentElementObserver = new MutationObserver(update);
documentDirection = document.documentElement.dir || "ltr";
documentLanguage = document.documentElement.lang || navigator.language;
documentElementObserver.observe(document.documentElement, {
attributeFilter: ["dir", "lang"],
attributes: true
});
}
function registerTranslation(...translation) {
translation.forEach((t) => {
const code = t.$code.toLowerCase();
if (translations.has(code)) {
translations.set(code, __spreadValues(__spreadValues({}, translations.get(code)), t));
} else {
translations.set(code, t);
}
if (!fallback) {
fallback = t;
}
});
update();
}
var LocalizeController = class {
constructor(host) {
this.host = host;
this.host.addController(this);
}
hostConnected() {
connectedElements.add(this.host);
}
hostDisconnected() {
connectedElements.delete(this.host);
}
/**
* Gets the host element's directionality as determined by the `dir` attribute. The return value is transformed to
* lowercase.
*/
dir() {
return `${this.host.dir || documentDirection}`.toLowerCase();
}
/**
* Gets the host element's language as determined by the `lang` attribute. The return value is transformed to
* lowercase.
*/
lang() {
return `${this.host.lang || documentLanguage}`.toLowerCase();
}
// eslint-disable-next-line class-methods-use-this
getTranslationData(lang) {
var _a, _b;
const locale = new Intl.Locale(lang.replace(/_/g, "-"));
const language = locale == null ? void 0 : locale.language.toLowerCase();
const region = (_b = (_a = locale == null ? void 0 : locale.region) == null ? void 0 : _a.toLowerCase()) != null ? _b : "";
const primary = translations.get(`${language}-${region}`);
const secondary = translations.get(language);
return {
language,
locale,
primary,
region,
secondary
};
}
/** Determines if the specified term exists, optionally checking the fallback translation. */
exists(key, options) {
var _a;
const { primary, secondary } = this.getTranslationData((_a = options.lang) != null ? _a : this.lang());
const finalOptions = __spreadValues({
ignoreFallback: false
}, options);
if (primary && primary[key] || secondary && secondary[key] || finalOptions.includeFallback && fallback && fallback[key]) {
return true;
}
return false;
}
/** Outputs a translated term. */
term(key, ...args) {
const { primary, secondary } = this.getTranslationData(this.lang());
let term;
if (primary && primary[key]) {
term = primary[key];
} else if (secondary && secondary[key]) {
term = secondary[key];
} else if (fallback && fallback[key]) {
term = fallback[key];
} else {
console.error(`No translation found for: ${String(key)}`);
return String(key);
}
if (typeof term === "function") {
return term(...args);
}
return term;
}
/** Outputs a localized date in the specified format. */
date(dateToFormat, options) {
const date = new Date(dateToFormat);
return new Intl.DateTimeFormat(this.lang(), options).format(date);
}
/** Outputs a localized number in the specified format. */
number(numberToFormat, options) {
const num = Number(numberToFormat);
return isNaN(num) ? "" : new Intl.NumberFormat(this.lang(), options).format(num);
}
/** Outputs a localized time in relative format. */
relativeTime(value, unit, options) {
return new Intl.RelativeTimeFormat(this.lang(), options).format(value, unit);
}
};
export {
update,
registerTranslation,
LocalizeController
};
//# sourceMappingURL=chunk.P4YIFZXB.js.map