@findify/react-components
Version:
Findify react UI components
33 lines (29 loc) • 1.03 kB
JavaScript
import { useConfig } from '@findify/react-connect';
import { useMemo } from 'react';
import unescape from 'lodash/unescape';
var createTranslator = function createTranslator() {
var strings = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : Map();
var selector = arguments.length > 1 ? arguments[1] : undefined;
return function (key) {
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
var value = strings.get(key);
var isTemplate = selector.test(value);
if (!value) return key;
if (!isTemplate) return value;
var tpl = unescape(value);
var index = -1;
return tpl.replace(selector, function () {
index++;
return args[index] || '';
});
};
};
export default (function () {
var _useConfig = useConfig(),
config = _useConfig.config;
return useMemo(function () {
return createTranslator(config.get('translations'), /%s/);
}, []);
});