UNPKG

@daimo/pay

Version:

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

59 lines (56 loc) 2 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}`); return `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) => { // use `replace` instead of `replaceAll` to support Node 14 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 (jsxs(React.Fragment, { children: [wrapTags(t), i < text.length - 1 && jsx("br", {})] }, i)); }); return text; }; const wrapTags = (text) => { // Bold markdown handling const textArray = text.split(/(\*\*[^\*]*\*\*)/g); let result = textArray.map((str, i) => { if (/(\*\*.*\*\*)/g.test(str)) { // use `replace` instead of `replaceAll` to support Node 14 return jsx("strong", { children: str.replace(/\*\*/g, "") }, i); } return `${str}`; }); // Replace text with logo return result.map((r) => { return r; }); }; export { useLocales as default }; //# sourceMappingURL=useLocales.js.map