translate-maker
Version:
Lightweight translation module. Internationalize your great project.
240 lines (192 loc) • 5.47 kB
JavaScript
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _objectSpread from "@babel/runtime/helpers/objectSpread";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import isPlainObject from 'lodash/isPlainObject';
import parseLocale from 'locale-id';
import EventEmitter from 'events';
import Tree from './Tree';
import baseFilters from './filters';
import MemoryAdapter from './adapters/Memory';
import MemoryCache from './caches/Memory';
import Mode from './constants/Mode';
import join from './utils/join';
function baseDefaultValue(path) {
return `Missing default translation for: ${path}`;
} // TODO add || syntax
export default class Translate extends EventEmitter {
constructor(options = {}) {
super();
if (isPlainObject(options.adapter)) {
throw new Error('You need to use instance of adapter or data option');
}
const {
locale,
namespace,
data
} = options,
rest = _objectWithoutProperties(options, ["locale", "namespace", "data"]);
if (locale || namespace) {
throw new Error('Use method setLocale instead of option locale and namespace');
}
this.options = _objectSpread({
locales: undefined,
// available locales
cache: new MemoryCache({}),
adapter: new MemoryAdapter({}),
dotNotation: true,
mode: Mode.MAIN,
references: true,
variables: true,
combinations: true,
defaultValue: baseDefaultValue
}, rest, {
filters: _objectSpread({}, baseFilters, rest.filters)
});
this.tree = new Tree(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 = _objectSpread({}, filters, {
[name]: filter
});
}
clear() {
const {
cache
} = this.getOptions();
if (cache) {
cache.clear();
}
this.tree = new Tree(this);
}
waitForLocale() {
var _this = this;
return _asyncToGenerator(function* () {
if (!_this.setLocalePromise) {
return undefined;
}
return _this.setLocalePromise;
})();
}
setLocale(locale, namespace) {
var _this2 = this;
return _asyncToGenerator(function* () {
const currentPromise = _this2.setLocalePromise;
_this2.setLocalePromise = new Promise(
/*#__PURE__*/
function () {
var _ref = _asyncToGenerator(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 = _objectSpread({}, 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 _asyncToGenerator(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 (isPlainObject(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 : join(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 = parseLocale(locale);
return l && l.language;
}
return locale;
}
getCache() {
return this.getOptions().cache;
}
getAdapter() {
return this.getOptions().adapter;
}
setFilter(type, fn) {
if (isPlainObject(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];
}
}
//# sourceMappingURL=Translate.js.map