UNPKG

@kiwicom/orbit-components

Version:

Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.

25 lines (21 loc) 902 B
import * as React from "react"; import DictionaryContext from "../Dictionary/DictionaryContext"; import DEFAULT_DICTIONARY from "../data/dictionary/en-GB.json"; export var pureTranslate = function pureTranslate(translations, key) { var values = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; var translation = translations[key] || DEFAULT_DICTIONARY[key]; if (!translation) { return key; } return Object.keys(values).reduce(function (acc, placeholder) { return acc.replace(new RegExp("__".concat(placeholder, "__"), "g"), String(values[placeholder])); }, translation); }; var Translate = function Translate(_ref) { var tKey = _ref.tKey, values = _ref.values; return /*#__PURE__*/React.createElement(DictionaryContext.Consumer, null, function (dictionary) { return pureTranslate(dictionary, tKey, values); }); }; export default Translate;