admin管理员组文章数量:1353164
I have en.json and ru.json files, I use variables by type there:
levelupmessage: "Congratulations on the new $ {level}"
because in json you cannot put `` what needs to be done so that there is not just a string?
I have en.json and ru.json files, I use variables by type there:
levelupmessage: "Congratulations on the new $ {level}"
because in json you cannot put `` what needs to be done so that there is not just a string?
Share Improve this question edited Aug 12, 2020 at 12:22 KristalkillPlay asked Aug 12, 2020 at 12:06 KristalkillPlayKristalkillPlay 2123 silver badges10 bronze badges 3- Are you asking how to escape backticks in JSON? – jannis Commented Aug 12, 2020 at 12:31
- Yes,i am asking how to escape backticks in JSON – KristalkillPlay Commented Aug 12, 2020 at 12:32
- JSON allows unescaped backticks. – jannis Commented Aug 12, 2020 at 12:38
1 Answer
Reset to default 9You need to create a module that loads all messages from en.json
and ru.json
and saves them in an object.
Then, create a function that finds the specified string inside your object. This function also replaces %VAR%
with a parameter you pass to that function.
Take a look at this example:
let strings = {
en: {
// I prefer % for variables
levelUp: "Congratulations on leveling up: %VAR%"
},
ru: {
levelUp: "Поздравляем с новым уровнем: %VAR%"
}
};
// Function to get locales and replace variables
function getLocale(language, string, ...vars) {
let locale = strings[language][string];
let count = 0;
locale = locale.replace(/%VAR%/g, () => vars[count] !== null ? vars[count] : "%VAR%");
return locale;
}
getLocale("en", "levelUp", "10"); // Congratulations on leveling up: 10
getLocale("ru", "levelUp", "10"); // Поздравляем с новым уровнем: 10
That's how many popular Discord bots handle internationalization.
本文标签: javascriptHow to make multilanguages in discord botStack Overflow
版权声明:本文标题:javascript - How to make multi-languages in discord bot? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743924258a2562665.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论