您需要在 i18n.js 中像这样配置 react i18next 以支持基本标签:
import i18n from "i18next";
import LanguageDetector from "i18next-browser-languagedetector";
import { initReactI18next } from "react-i18next";
i18n
.use(LanguageDetector)
.use(initReactI18next)
.init({
fallbackLng: "en",
react: {
// https://react.i18next.com/latest/trans-component#trans-props
transSupportBasicHtmlNodes: true,
transKeepBasicHtmlNodesFor: ["br", "strong", "b", "i"],
}
});
export default i18n;
然后你可以像@jamuhl所说的那样使用组件:
const keyInTranslationJsonFile=`My text that can be <b>{{boldPlaceholder}}</b>`;
<Trans i18nKey={keyInTranslationJsonFile}>{{boldPlaceholder: "bold"}}</Trans>
我刚刚注意到不可能提醒 Trans 组件。据我所知,你不能用这个t()
函数做粗体或使用任何标签。但是您至少仍然可以使用普通占位符。
像这样:
const keyInTranslationJsonFile=`You've selected the max of {{selectLimit}} elements`;
alert(
t(keyInTranslationJsonFile, {
selectLimit: selectLimit,
})
);