@adyen/adyen-platform-experience-web
Version:

191 lines (190 loc) • 5.88 kB
JavaScript
import { FALLBACK_LOCALE as h, DEFAULT_TRANSLATIONS as w, getLocalesFromTranslationSourcesRecord as y, DEFAULT_DATETIME_FORMAT as A, SUPPORTED_LOCALES as C } from "./constants/localization.js";
import { en_US as p } from "../../translations/index.js";
import { getLocalisedAmount as D } from "./amount/amount-util.js";
import { createTranslationsLoader as b, getLocalizationProxyDescriptors as S } from "./localization-utils.js";
import { toTwoLetterCode as d, formatCustomTranslations as O, getTranslation as c } from "./utils.js";
import { httpGet as g } from "../Http/http.js";
import u from "../../config/translations/swapConfig.json.js";
import { createWatchlist as v } from "../../primitives/reactive/watchlist/main.js";
import E from "./datetime/restamper/restamper.js";
import { isNullish as L, isUndefined as R, isNull as T } from "../../utils/value/is.js";
import { ALREADY_RESOLVED_PROMISE as z } from "../../utils/async/constants.js";
import { struct as F } from "../../utils/struct/main.js";
import { noop as P } from "../../utils/common.js";
class V {
#t = h;
#c = d(this.#t);
#a = [h];
#u = this.#a;
#s;
#n = w;
#e = b.call(this);
#f;
#m = z;
#r;
#i;
#l = v({ timestamp: () => performance.now() });
#h = E();
#o = u;
#p = /* @__PURE__ */ new Set();
watch = this.#l.subscribe.bind(void 0);
i18n = F(S.call(this));
preferredTranslations;
constructor(t = h, e, s = "", r = "") {
this.watch(P), this.#f = (i) => g({
loadingContext: s,
path: `/${i}.json`,
versionless: !0,
skipContentType: !0,
errorLevel: "info"
}), this.preferredTranslations = Object.freeze(
e?.reduce((i, o) => ({ ...i, ...o }), p) ?? { ...p }
), this.#a = y(this.preferredTranslations), this.locale = t, (async () => {
if (!r)
return u;
try {
return await g({
loadingContext: r,
path: "/translations/swapConfig.json",
versionless: !0,
skipContentType: !0,
errorLevel: "error"
});
} catch (i) {
return console.warn("Failed to load swapConfig from CDN, using local fallback", i), u;
}
})().then((i) => this.#o = i);
}
get customTranslations() {
return this.#s ?? {};
}
set customTranslations(t) {
let e, s = [...this.#a];
if (!L(t)) {
e = O(t, C);
const r = Object.keys(e);
s = [...s, ...r].sort().filter((i, o, l) => l.indexOf(i) === o);
}
this.#e.supportedLocales = s, this.#d(e);
}
get languageCode() {
return this.#c;
}
get lastRefreshTimestamp() {
return this.#l.snapshot.timestamp;
}
get locale() {
return this.#t;
}
set locale(t) {
if (L(t))
this.locale = h;
else {
if (this.#e.locale = t, this.#t === this.#e.locale) return;
this.#d(this.#s);
}
}
get ready() {
return this.#m;
}
get supportedLocales() {
return this.#u;
}
get timezone() {
return this.#h.tz.current;
}
set timezone(t) {
this.#h.tz = t;
}
#d(t) {
R(this.#i) && (this.#m = new Promise((r) => {
this.#i = () => {
r(this.#r), this.#r = this.#i = void 0;
};
}));
const e = () => {
this.#r === s && this.#i?.();
}, s = this.#r = (async () => {
this.#n = await this.#e.load(this.#f, t), this.#t = this.#e.locale, this.#u = Object.freeze(this.#e.supportedLocales), this.#s = t, this.#c = d(this.#t), this.#l.requestNotification();
})();
s.then(e).catch((r) => {
e(), console.error(r);
});
}
/**
* Returns a translated string from a key in the current {@link Localization.locale}
* @param key - Translation key
* @param options - Translation options
* @returns Translated string
*/
get(t, e) {
const s = this.#s?.[this.#t], r = this.#o[t];
if (s && r && !Array.isArray(r) && r !== t) {
let o = t;
const l = [], f = /* @__PURE__ */ new Set();
for (; !f.has(o); ) {
f.add(o), l.push(o);
const a = this.#o[o];
if (!a || Array.isArray(a)) break;
o = a;
}
for (let a = 0; a < l.length; a++) {
const n = l[a], m = c(s, n, e);
if (!T(m))
return n !== t && (this.#p.has(n) || (console.warn(
`[Adyen Platform Experience Web] Deprecated translation key detected: "${n}". Please update to use the new key: "${t}". This backward compatibility will be removed in a future version.`
), this.#p.add(n)), a > 1 && (this.#o[t] = n)), m;
}
}
const i = c(this.#n, t, e);
return T(i) ? t : i;
}
/**
* Returns a boolean that checks if the translation key exists in the current {@link Localization.locale}
* @param key - Translation key
* @param options - Translation options
* @returns boolean
*/
has(t, e) {
return !!c(this.#n, t, e);
}
/**
* Returns a localized string for an amount
* @param amount - Amount to be converted
* @param currencyCode - Currency code of the amount
* @param options - Options for String.prototype.toLocaleString
*/
amount(t, e, s) {
const { hideCurrency: r, ...i } = s || {}, o = D(t, this.#t, e, r, {
...i,
currencyDisplay: "symbol",
signDisplay: "never"
});
return t < 0 ? `- ${o}` : o;
}
/**
* Returns a localized string for a date
* @param date - Date to be localized
* @param options - Options for {@link Date.toLocaleDateString}
*/
date(t, e = {}) {
const s = { ...A, timeZone: this.#h.tz.current, ...e };
return new Date(t).toLocaleDateString(this.#t, s);
}
/**
* Returns a localized string for a full date
* @param date - Date to be localized
*/
fullDate(t) {
return this.date(t, {
month: "short",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: !1
});
}
}
export {
V as default
};