UNPKG

macoolka-i18n

Version:

`macoolka-i18n` is a library for internationalization in TypeScript. It easily integrates some localization features to your Application.

134 lines 7.72 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildI18NFormat = void 0; var macoolka_object_1 = require("macoolka-object"); var Module_1 = __importDefault(require("../Module")); var invariant = Module_1.default.invariant, runIO = Module_1.default.runIO, wrapErrorToString = Module_1.default.wrapErrorToString; var errorText = function (type, id) { return "The Name(\"".concat(id, "\") can't be found in format ").concat(type, "."); }; var getLocalTemplate = function (config) { var templates = config.templates, template = config.template, defaultLocale = config.defaultLocale; var locale = defaultLocale; if (locale) { var templateLocale = (0, macoolka_object_1.get)(templates, [locale], {}); return (0, macoolka_object_1.merge)({}, template, templateLocale); } else { return template; } }; var buildI18NFormat = function (config) { var defaultLocale = config.defaultLocale, _a = config.formatters, formatDatetime = _a.formatDatetime, formatMessage = _a.formatMessage, formatMessageHtml = _a.formatMessageHtml, formatNumber = _a.formatNumber, formatPlural = _a.formatPlural, formatRelativeTime = _a.formatRelativeTime; var result = { formatDate: function (id) { var templateValue = result.getTemplate().date[id]; /* console.log(defaultLocale) console.log(result.getLocale()) console.log(formatDatetime({ ...templateValue, locale: defaultLocale })(new Date())) */ invariant({ value: templateValue, message: errorText('date', id) }); return runIO({ io: function () { return formatDatetime(__assign(__assign({}, templateValue), { locale: result.getLocale() })); }, message: "Error formatting date for locale: \"".concat(result.getLocale(), "\""), }); }, formatTime: function (id) { var templateValue = result.getTemplate().time[id]; invariant({ value: templateValue, message: errorText('time', id) }); return runIO({ io: function () { return formatDatetime(__assign(__assign({}, templateValue), { locale: result.getLocale() })); }, message: "Error formatting time for locale: \"".concat(result.getLocale(), "\""), }); }, formatMessage: function (_a) { var id = _a.id, defaultMessage = _a.defaultMessage, others = __rest(_a, ["id", "defaultMessage"]); var _b = result.getTemplate(), number = _b.number, date = _b.date, time = _b.time, messages = _b.messages; invariant({ value: id, message: 'An `id` must be provided to format a message.' }); var templateValue = messages[id] ? messages[id] : defaultMessage ? defaultMessage : undefined; invariant({ value: templateValue, message: errorText('message', id) }); return runIO({ io: function () { return formatMessage(__assign(__assign({ message: templateValue, formats: { number: number, date: date, time: time } }, others), { locale: result.getLocale() })); }, message: "Error formatting message: \"".concat(id, "\" for locale: \"").concat(result.getLocale(), "\""), }); }, formatErrorMessage: function (_a) { var title = _a.title, error = _a.error, others = __rest(_a, ["title", "error"]); var message = result.formatMessage(others); return wrapErrorToString({ message: message, exception: error, title: title }); }, formatMessageHtml: function (_a) { var id = _a.id, others = __rest(_a, ["id"]); var _b = result.getTemplate(), number = _b.number, date = _b.date, time = _b.time, messages = _b.messages; invariant({ value: id, message: 'An `id` must be provided to format a html message.' }); var templateValue = messages[id]; invariant({ value: templateValue, message: errorText('html message', id) }); return runIO({ io: function () { return formatMessageHtml(__assign(__assign({ message: templateValue, formats: { number: number, date: date, time: time } }, others), { locale: result.getLocale() })); }, message: "Error formatting html message: \"".concat(id, "\" for locale: \"").concat(result.getLocale(), "\""), }); }, formatNumber: function (id) { var templateValue = result.getTemplate().number[id]; invariant({ value: templateValue, message: errorText('number', id) }); return runIO({ io: function () { return formatNumber(__assign(__assign({}, templateValue), { locale: result.getLocale() })); }, message: "Error formatting number for locale: \"".concat(result.getLocale(), "\""), }); }, formatPlural: function (id) { invariant({ value: Intl.PluralRules, message: "Intl.PluralRules is not available in this environment.\n Try polyfilling it using \"@formatjs/intl-pluralrules\"\n ", }); var templateValue = result.getTemplate().plural[id]; invariant({ value: templateValue, message: errorText('plural', id) }); return runIO({ io: function () { return formatPlural(__assign(__assign({}, templateValue), { locale: result.getLocale() })); }, message: "Error formatting plural for locale: \"".concat(result.getLocale(), "\""), }); }, formatRelativeTime: function (id) { invariant({ value: Intl.RelativeTimeFormat, message: "Intl.RelativeTimeFormat is not available in this environment.\n Try polyfilling it using \"@formatjs/intl-relativetimeformat\"\n ", }); var templateValue = result.getTemplate().relative[id]; invariant({ value: templateValue, message: errorText('relative time', id) }); return runIO({ io: function () { return formatRelativeTime(__assign(__assign({}, templateValue), { locale: defaultLocale })); }, message: "Error formatting relative time for locale: \"".concat(result.getLocale(), "\""), }); }, getTemplate: function () { return getLocalTemplate(config); }, getLocale: function () { return defaultLocale; }, // getTimeZone: () => getGlobalI18N().timeZone }; return result; }; exports.buildI18NFormat = buildI18NFormat; //# sourceMappingURL=buildI18NFormat.js.map