@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.
19 lines (15 loc) • 672 B
JavaScript
import React from "react";
import DictionaryContext from "../Dictionary/DictionaryContext";
import DEFAULT_DICTIONARY from "../data/dictionary/en-GB.json";
export const pureTranslate = (translations, key, values = {}) => {
const translation = translations[key] || DEFAULT_DICTIONARY[key];
if (!translation) {
return key;
}
return Object.keys(values).reduce((acc, placeholder) => acc.replace(new RegExp(`__${placeholder}__`, "g"), String(values[placeholder])), translation);
};
const Translate = ({
tKey,
values
}) => React.createElement(DictionaryContext.Consumer, null, dictionary => pureTranslate(dictionary, tKey, values));
export default Translate;