【问题标题】:i18next Single translation String Fallback / Default (Vue JS)i18next 单翻译字符串后备/默认(Vue JS)
【发布时间】:2018-09-15 02:06:56
【问题描述】:

一直在使用i18next,然后我尝试为一些翻译后的字符串制作我后备值,该值将使用另一种语言,例如:

en:
  base.json
    "yes":"yes"
    "no": "no"

fr:
  base.json
    "yes": "oui"
     "no: ""

所以我想要的预期行为是当 vue 在 FR 上运行并且我尝试执行 {{ i18n.t('base:no' }} 时,输出应该是 no 而不是空字符串。

这是我目前尝试过的:

初始化:

i18next
    .use(languageDetector)
    .init({
      fallbackLng: 'en',
      load: 'languageOnly',
      detection: {
        order: ['querystring', 'navigator']
      }
    })

当输出时:{{ i18n.t('base:no', {lng: 'en'} }} 我只是得到一个空字符串。

还尝试在 init 上加载后备语言,但不起作用

以前有人偶然发现过这个吗? 感谢您的帮助!

【问题讨论】:

    标签: javascript vue.js internationalization i18next


    【解决方案1】:

    对不起,现在我明白了...您得到了一个空字符串作为法语中“no”的翻译...默认情况下,空字符串是有效的翻译。

    您可以在 init 上设置 returnEmptyString: false 以不允许空字符串作为翻译的有效值:https://www.i18next.com/configuration-options.html#translation-defaults

    【讨论】:

    【解决方案2】:

    从你的初始化看来你既没有传递翻译也没有加载它们:https://www.i18next.com/add-or-load-translations.html

    你得到console.log(i18next.services.resourceStore.data);的任何结果

    【讨论】:

    • 翻译工作正常,只是没有在我加载语言的地方发布片段,因为我们使用了一些循环,在这里发布它毫无意义
    • set debug: true on init...似乎更像是在调用 init 回调之前调用 i18n.t 的时间问题(翻译的异步加载)。应该加载 FallbackLng...对项目进行足够的测试来断言这一点。
    猜你喜欢
    • 2017-10-20
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-30
    • 1970-01-01
    相关资源
    最近更新 更多