UNPKG

translate-maker

Version:

Lightweight translation module. Internationalize your great project.

262 lines (199 loc) 6.24 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread")); var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _isPlainObject = _interopRequireDefault(require("lodash/isPlainObject")); var _localeId = _interopRequireDefault(require("locale-id")); var _events = _interopRequireDefault(require("events")); var _Tree = _interopRequireDefault(require("./Tree")); var _filters = _interopRequireDefault(require("./filters")); var _Memory = _interopRequireDefault(require("./adapters/Memory")); var _Memory2 = _interopRequireDefault(require("./caches/Memory")); var _Mode = _interopRequireDefault(require("./constants/Mode")); var _join = _interopRequireDefault(require("./utils/join")); function baseDefaultValue(path) { return `Missing default translation for: ${path}`; } // TODO add || syntax class Translate extends _events.default { constructor(options = {}) { super(); if ((0, _isPlainObject.default)(options.adapter)) { throw new Error('You need to use instance of adapter or data option'); } const { locale, namespace, data } = options, rest = (0, _objectWithoutProperties2.default)(options, ["locale", "namespace", "data"]); if (locale || namespace) { throw new Error('Use method setLocale instead of option locale and namespace'); } this.options = (0, _objectSpread2.default)({ locales: undefined, // available locales cache: new _Memory2.default({}), adapter: new _Memory.default({}), dotNotation: true, mode: _Mode.default.MAIN, references: true, variables: true, combinations: true, defaultValue: baseDefaultValue }, rest, { filters: (0, _objectSpread2.default)({}, _filters.default, rest.filters) }); this.tree = new _Tree.default(this); const { adapter } = this.getOptions(); if (data) { adapter.rehydrate(data); } } hasFilter(name) { return !!this.options.filters[name]; } addFilter(name, filter) { const { filters } = this.options; this.options.filters = (0, _objectSpread2.default)({}, filters, { [name]: filter }); } clear() { const { cache } = this.getOptions(); if (cache) { cache.clear(); } this.tree = new _Tree.default(this); } waitForLocale() { var _this = this; return (0, _asyncToGenerator2.default)(function* () { if (!_this.setLocalePromise) { return undefined; } return _this.setLocalePromise; })(); } setLocale(locale, namespace) { var _this2 = this; return (0, _asyncToGenerator2.default)(function* () { const currentPromise = _this2.setLocalePromise; _this2.setLocalePromise = new Promise( /*#__PURE__*/ function () { var _ref = (0, _asyncToGenerator2.default)(function* (resolve, reject) { try { yield currentPromise; } catch (e) { reject(e); return; } if (!locale) { throw new Error('Locale is undefined'); } const options = _this2.getOptions(); if (options.locales && options.locales.indexOf(locale) === -1) { throw new Error('Locale is not allowed. Setup locales'); } const adapter = _this2.getAdapter(); adapter.get(locale, namespace).then(data => { const sameLocale = options.locale === locale; if (!sameLocale) { _this2.clear(); _this2.options = (0, _objectSpread2.default)({}, options, { locale }); } if (namespace) { _this2.set(namespace, data); } else { _this2.set(data); } if (!sameLocale) { _this2.emit('locale', locale, namespace); } resolve(data); }, reject); }); return function (_x, _x2) { return _ref.apply(this, arguments); }; }()); return _this2.setLocalePromise; })(); } loadNamespace(namespace) { var _this3 = this; return (0, _asyncToGenerator2.default)(function* () { // we need to wait for async setLocale yield _this3.setLocalePromise; const options = _this3.getOptions(); return _this3.setLocale(options.locale, namespace); })(); } get(path, attrs, defaultValue, pure) { if (path === undefined || path === null) { return undefined; } if ((0, _isPlainObject.default)(path)) { const translated = {}; Object.keys(path).forEach(key => { translated[key] = this.get(path[key], attrs, defaultValue); }); return translated; } const items = this.tree.get(path, attrs, defaultValue); return pure ? items : (0, _join.default)(items); } set(path, value) { const result = this.tree.set(path, value); this.emit('changed'); return result; } getOptions() { return this.options; } getLocale() { return this.getOptions().locale; } getLanguage() { const locale = this.getLocale(); if (locale) { const l = (0, _localeId.default)(locale); return l && l.language; } return locale; } getCache() { return this.getOptions().cache; } getAdapter() { return this.getOptions().adapter; } setFilter(type, fn) { if ((0, _isPlainObject.default)(type)) { Object.keys(type).forEach(filterType => this.setFilter(filterType, type[filterType])); return; } this.getOptions().filters[type] = fn; } getFilter(type) { const { filters } = this.getOptions(); return filters && filters[type]; } } exports.default = Translate; //# sourceMappingURL=Translate.js.map