@coin-voyage/paykit
Version:
Seamless crypto payments. Onboard users from any chain, any coin into your app with one click.
64 lines • 2.37 kB
JavaScript
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(() => {
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);
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;
});
};
//# sourceMappingURL=useLocales.js.map