简体   繁体   English

如何在i18next中将变量用作键?

[英]How to use a variable as a key with i18next?

I am trying to use a variable as a key for a localisation using i18next . 我正在尝试使用变量作为使用i18next进行本地化的键。 Although, the right strings are returned but the interpolation does not work. 虽然会返回正确的字符串,但是插值无效。

I tried to play with variables and raw strings to see where the issue comes from. 我尝试使用变量和原始字符串来查看问题的根源。 I am sure it comes from the use of variables in i18n.t() . 我敢肯定,它来自i18n.t()中变量的使用。

return newChannel.guild.channels.get(config.defaultChannel).send(i18n.t(message.content, message.options));

message value is returned by a function. message值由函数返回。 This is a snippet: 这是一个片段:

else if (newChannel.name !== oldChannel.name) {
    return message = {
        content: i18n.t('events:channel.update.name.text'),
        options: { oldChannel: `**#${oldChannel.name}**`, newChannel: newChannel, interpolation: { escapeValue: false }}
    };
}

That is the string in the json: 那是json中的字符串:

"update": {
    "name": {
        "text": "The text channel {{oldChannel}} is now called {{newChannel}}."
    }
}

It outputs: The text channel is now called . 它输出: The text channel is now called .

This snippet looks incorrect: 此代码段看起来不正确:

else if (newChannel.name !== oldChannel.name) {
    return message = {
        content: i18n.t('events:channel.update.name.text'),
        options: { oldChannel: `**#${oldChannel.name}**`, newChannel: newChannel, interpolation: { escapeValue: false }}
    };
}

It should be something more like: 应该更像是:

else if (newChannel.name !== oldChannel.name) {
    return message = {
        content: i18n.t('events:channel.update.name.text'),
        options: { oldChannel: oldChannel.name, newChannel: newChannel.name, interpolation: { escapeValue: false }}
    };
}

Note the changes to both variables. 注意两个变量的变化。 Can you explain what you are trying to do in the mappings that makes you feel that you need to do string interpolation with ES6 instead of just using the interpolation that the library itself supports? 您能解释一下您试图在映射中执行的操作,从而使您感到需要使用ES6进行字符串插值,而不是仅使用库本身支持的插值吗?

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

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