我按照文档配置了vue-i18next。 当我直接从
t()
或在带有 i18next.t
的组件内使用 useTranslation()
时,它不会返回翻译后的字符串,而是返回一个函数
(ctx) => {const { normalize: _normalize } = ctx;return _normalize(["MY TRANSLATED STRING"])}
utils/i18n/i18next.ts:
import i18next from "i18next"
import fr from "@/locales/fr.json"
const i18nCreated = i18next.init({
lng: "fr",
interpolation: {
escapeValue: false,
},
fallbackLng: false,
resources: { fr: { translation: fr } },
})
export { i18nCreated, i18next }
main.ts:
import { i18nCreated, i18next } from "@/utils/i18n/i18next"
import I18NextVue from "i18next-vue"
// ...
await i18nCreated
console.log(i18next.t("home.title"))
app.use(I18NextVue, {
i18next,
})
控制台.log输出:
渲染组件翻译:
我自己发现的:我们使用的是 intlify 中的 i18n,我忘记删除一些预编译语言环境的代码...... 找到解决方案阅读Vue.js 中导入的 json 中的值不正确。
从 intlify 中删除了所有内容,现在运行良好。