繁体   English   中英

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

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

我正在尝试使用变量作为使用i18next进行本地化的键。 虽然会返回正确的字符串,但是插值无效。

我尝试使用变量和原始字符串来查看问题的根源。 我敢肯定,它来自i18n.t()中变量的使用。

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

message值由函数返回。 这是一个片段:

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

那是json中的字符串:

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

它输出: The text channel is now called .

此代码段看起来不正确:

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

应该更像是:

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

注意两个变量的变化。 您能解释一下您试图在映射中执行的操作,从而使您感到需要使用ES6进行字符串插值,而不是仅使用库本身支持的插值吗?

暂无
暂无

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

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