Skip to main content

With Markdown

i18n-keyless only accepts strings, therefore if you need to style some words, you should use markdown. Translating markdown keeps the styling.

/src/components/MyText.tsx
import { useI18nKeyless, getTranslation, type I18nKeylessTextProps } from 'i18n-keyless-react';
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import rehypeRaw from 'rehype-raw';

export default function MyText({
skipTranslation
children,
i18nProps,
}: {
skipTranslation?: boolean;
i18nProps?: I18nKeylessTextProps;
children: I18nKeylessTextProps["children"];
}) {
// if you want to live update easily when changing language,
// you need to force rerendering with lastRefresh because
// react-markdown prevent re-rendering otherwise
const lastRefresh = useI18nKeyless((state) => state.lastRefresh);
return (
<ReactMarkdown
key={lastRefresh}
remarkPlugins={[remarkGfm]}
rehypePlugins={[rehypeRaw]}
components={{
strong: ({ ...props }) => <span className="text-neo-pink" {...props} />,
}}
>
{skipTranslation ? children : getTranslation(children, i18nProps)}
</ReactMarkdown>
);
}