UNPKG

@coin-voyage/paykit

Version:

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

60 lines (59 loc) 2.24 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import React, { useMemo } from "react"; import Logos from "../assets/logos"; import usePayContext from "../components/contexts/pay"; import { getLocale } from "../lib/localizations"; export default function useLocales(replacements) { const context = usePayContext(); const language = context.options?.language ?? "en-US"; const translations = useMemo(() => getLocale(language), [language]); const translated = {}; for (const key in translations) { if (Object.prototype.hasOwnProperty.call(translations, key)) { translated[key] = localize(translations[key], replacements); } } return translated; } const localize = (text, replacements) => { let parsedText = text; if (replacements) { Object.keys(replacements).forEach((key) => { // Allow optional spaces around the key inside the {{ }} const pattern = new RegExp(`{{\\s*${key}\\s*}}`, "g"); parsedText = parsedText.replace(pattern, String(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); const 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) => { if (typeof r === "string") { return r.split(/(\[WALLETCONNECTLOGO\])/g).map((s) => { if (s === "[WALLETCONNECTLOGO]") { return (_jsx("span", { className: "ck-tt-logo", children: _jsx(Logos.WalletConnect, {}) }, s)); } return s; }); } return r; }); };