简体   繁体   English

i18next - 在用于插值的 data-i18n-options 属性中提供一个键

[英]i18next - Providing a key in data-i18n-options attribute for interpolation

Currently using i18next.js and have run in to a problem.当前使用 i18next.js 并且遇到了问题。

Say there's two keys:假设有两个键:

{
    'keyOne':'I am __name__',
    'item': 'a person'
}

In the html:在 html 中:

<div data-i18n="keyOne" data-i18n-options={"name":"item"}></div>

And the result I'd like:我想要的结果是:

'I am a person' '我是一个人'

Where 'a person' isn't the string literal but the reference to the key (ie like using data-i18n attribute)其中 'a person' 不是字符串文字而是对键的引用(例如使用 data-i18n 属性)

Would it be possible to provide the 'item' key (not the string 'a person') for use in 'keyOne' for interpolation?是否可以提供在“keyOne”中使用的“item”键(而不是字符串“a person”)进行插值? This would be useful as when I change the locale I'd only have to call .i18n() on the div and both keys would be translated.这将很有用,因为当我更改语言环境时,我只需要在 div 上调用 .i18n() 并且两个键都将被翻译。 Without this functionality I'd have to re-evaluate the context and update the div manually.如果没有此功能,我将不得不重新评估上下文并手动更新 div。

Any help would be much appreciated.任何帮助将非常感激。 Thanks谢谢

i18next has nesting functionality: https://www.i18next.com/translation-function/nesting i18next 具有嵌套功能: https ://www.i18next.com/translation-function/nesting

So I could make what you want.所以我可以做你想要的。 You can test in this jsfiddle: https://jsfiddle.net/leonardonsantos/3zy4xmnm/1/您可以在此 jsfiddle 中进行测试: https ://jsfiddle.net/leonardonsantos/3zy4xmnm/1/

Just change in data-i18n-options with this $t :只需使用此$t更改data-i18n-options

<div data-i18n="keyOne" data-i18n-options='{"name":"$t(item)"}'>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM