簡體   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