UNPKG

@daimo/pay

Version:

Seamless crypto payments. Onboard users from any chain, any coin into your app with one click.

61 lines (58 loc) 1.78 kB
import { jsxs, jsx } from 'react/jsx-runtime'; import React, { useMemo } from 'react'; import { usePayContext } from './usePayContext.js'; import { getLocale } from '../localizations/index.js'; function useLocales(replacements) { const context = usePayContext(); const language = context.options?.language ?? "en-US"; const translations = useMemo(() => { return getLocale(language); }, [language]); if (!translations) { console.error(`Missing translations for: ${language}`); throw new Error(`Missing translations for: ${language}`); } const translated = {}; Object.keys(translations).map((key) => { const string = translations[key]; return translated[key] = localize(string, replacements); }); return translated; } const localize = (text, replacements) => { let parsedText = text; if (replacements) { Object.keys(replacements).forEach((key) => { parsedText = parsedText.replace( new RegExp(`({{ ${key} }})`, "g"), replacements[key] ); }); } return replaceMarkdown(parsedText); }; const replaceMarkdown = (markdownText) => { let text = markdownText; text = text.split("\n"); text = text.map((t, i) => { return /* @__PURE__ */ jsxs(React.Fragment, { children: [ wrapTags(t), i < text.length - 1 && /* @__PURE__ */ jsx("br", {}) ] }, i); }); return text; }; const wrapTags = (text) => { const textArray = text.split(/(\*\*[^\*]*\*\*)/g); let result = textArray.map((str, i) => { if (/(\*\*.*\*\*)/g.test(str)) { return /* @__PURE__ */ jsx("strong", { children: str.replace(/\*\*/g, "") }, i); } return `${str}`; }); return result.map((r) => { return r; }); }; export { useLocales as default }; //# sourceMappingURL=useLocales.js.map