translate-maker
Version:
Lightweight translation module. Internationalize your great project.
69 lines (58 loc) • 1.78 kB
JavaScript
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _objectSpread from "@babel/runtime/helpers/objectSpread";
import Adapter from './Adapter';
export function defaultResolvePath(locale, namespace, options) {
const {
ext,
path
} = options;
const fileName = ext ? `${locale}${ext}` : locale;
const namespacePath = namespace ? `/${namespace.replace('.', '/')}` : '';
const namespaceFilePath = namespacePath ? `${namespacePath}/${fileName}` : fileName;
return path ? `${path}/${namespaceFilePath}` : namespaceFilePath;
}
export default class File extends Adapter {
constructor(options = {}) {
if (!options.getData) {
throw new Error('You need to set getData');
}
super(_objectSpread({
path: undefined,
ext: undefined,
getData: undefined,
setData: undefined,
resolvePath: defaultResolvePath
}, options));
}
get(locale, namespace) {
var _this = this;
return _asyncToGenerator(function* () {
const options = _this.getOptions();
const {
resolvePath,
getData
} = options;
const path = resolvePath(locale, namespace, options);
return getData(path);
})();
}
set(locale, value, namespace) {
var _this2 = this;
return _asyncToGenerator(function* () {
if (typeof namespace === 'function') {
return _this2.set(locale, value, null, namespace);
}
const options = _this2.getOptions();
const {
resolvePath,
setData
} = options;
if (!setData) {
throw new Error('You need to set option setData');
}
const path = resolvePath(locale, namespace, options);
return setData(path, value);
})();
}
}
//# sourceMappingURL=File.js.map