【问题标题】:Can I make VueI18n fallback on a key path if not found the complete key path?如果找不到完整的关键路径,我可以让 VueI18n 回退到关键路径吗?
【发布时间】:2019-05-27 14:10:33
【问题描述】:

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

这方面的一个例子可能是,我有以下消息:

{
   en: {
      "hello": "This is the fallback message!",
      "admin.hello": "This is some other message for another context"
   }
}

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

{{ $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!"

【问题讨论】:

    标签: vue.js internationalization


    【解决方案1】:

    好的,我要快速回答这个问题。 MissingHandler 对此很有用。

    代码示例变成了这样: Vue.use(VueI18n)

    // 使用选项创建 VueI18n 实例

    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
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      • 2016-08-19
      • 1970-01-01
      • 2011-04-22
      • 2018-06-18
      • 2018-06-27
      相关资源
      最近更新 更多