@dialpad/dialtone-vue
Version:
Vue component library for Dialpad's design system Dialtone
99 lines (98 loc) • 3.08 kB
JavaScript
import { RawBundleSource as i, LocaleManager as f, useI18N as l } from "@dialpad/i18n-vue2";
import d from "./en-US.js";
import m from "./zh-CN.js";
import g from "./nl-NL.js";
import p from "./fr-FR.js";
import S from "./de-DE.js";
import w from "./it-IT.js";
import N from "./ja-JP.js";
import L from "./pt-BR.js";
import R from "./ru-RU.js";
import h from "./es-LA.js";
const e = "dialtone", t = {
ENGLISH: "en-US",
CHINESE: "zh-CN",
DUTCH: "nl-NL",
FRENCH: "fr-FR",
GERMAN: "de-DE",
ITALIAN: "it-IT",
JAPANESE: "ja-JP",
PORTUGUESE: "pt-BR",
RUSSIAN: "ru-RU",
SPANISH: "es-LA"
}, n = "en-US", u = "user-locale";
class o {
constructor(r = null) {
if (typeof o.instance == "object")
return o.instance;
this._locale = r || o.getPreferredLocale();
const a = new i({
resources: i.builtResources([
["en-US", e, d],
["zh-CN", e, m],
["nl-NL", e, g],
["fr-FR", e, p],
["de-DE", e, S],
["it-IT", e, w],
["ja-JP", e, N],
["pt-BR", e, L],
["ru-RU", e, R],
["es-LA", e, h]
])
});
return new f({
bundleSource: a,
allowedLocales: Object.values(t),
fallbackLocale: n,
preferredLocale: this._locale,
namespaces: [e]
}).install(e), o.instance = this, typeof window < "u" && (window.onstorage = (c) => {
c.key === u && (this.currentLocale = c.newValue);
}), this;
}
/**
* Gets the preferred locale from user's locale stored in localStorage or the browser language
* @returns { string }
*/
static getPreferredLocale() {
if (typeof window > "u" || !window.localStorage)
return n;
const r = window.localStorage.getItem(u), a = Object.values(t).find((s) => s.startsWith(navigator.language.slice(0, 2)));
return r || a || n;
}
static getAllowedLocales() {
return t;
}
/**
* Passthrough function to the i18n $t function including the dialtone namespace
* Returns a translated string based on a key and optional variables.
* It’s used for simple text translations.
* https://github.com/dialpad/goblin-client-tools/tree/main/packages/i18n#t
*/
$t(...r) {
return l(e).$t(...r);
}
/**
* Passthrough function to the i18n $ta function including the dialtone namespace
* Returns an object containing translated attributes to pass directly as props to components, it can contain
* aria-label, title, etc, rather than just a plain text. It’s useful for handling element attributes in the UI.
* https://github.com/dialpad/goblin-client-tools/tree/main/packages/i18n#ta
*/
$ta(...r) {
return l(e).$ta(...r);
}
get currentLocale() {
return this._locale;
}
set currentLocale(r) {
if (r !== this._locale) {
if (!Object.values(t).includes(r))
throw new Error(`Locale ${r} is not allowed, please use one of the following: ${Object.values(t).join(", ")}`);
this._locale = r, l(e).setI18N({ preferredLocale: r }, e);
}
}
}
export {
o as DialtoneLocalization
};
//# sourceMappingURL=index.js.map