首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果没有找到完整的密钥路径,我可以在密钥路径上使VueI18n回退吗?

如果没有找到完整的密钥路径,我可以在密钥路径上使VueI18n回退吗?
EN

Stack Overflow用户
提问于 2018-12-30 19:29:42
回答 1查看 1.4K关注 0票数 1

如果VueI18n没有找到它,它是否有可能退回到较短的键。

例如,我有以下信息:

代码语言:javascript
复制
{
   en: {
      "hello": "This is the fallback message!",
      "admin.hello": "This is some other message for another context"
   }
}

下面的代码说明了结果应该是什么:

代码语言:javascript
复制
{{ $t("does.not.exists.hello") }} // should fallback on hello, so the result will be "This is the fallback message!"
{{ $t("admin.hello") }} // Message exists so the result should be "This is some other message for another context"

{{ $t("hello") }} // Message exists so the result should be "This is the fallback message!"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-30 19:34:31

好吧,这个问题,我本来想快点的。MissingHandler对此非常有用。

代码示例如下所示: Vue.use(VueI18n)

//创建带有选项的VueI18n实例

代码语言:javascript
复制
export default new VueI18n({
    locale: 'en', // set locale
    silentTranslationWarn: true,
    missing: (locale: Locale, key: Path, vm?: Vue) => {
        if(key.includes(".")) {
            let newKey = /\.(.+)/.exec(key)[1];
            console.log(newKey)
            return vm.$t(newKey) as string
        }
        return key
    },
    //formatter: new CustomFormatter(),
    fallbackLocale: 'en',
    messages // set locale messages
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53980750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档