Creating a multilingual Discord bot: How to implement language support?

I’m working on a Discord bot and want to add support for multiple languages. Right now, I have separate JSON files for English (en.json) and Russian (ru.json). In these files, I’m using variables like this:

{
  "levelupmessage": "Congratulations on the new ${level}"
}

The problem is, I can’t use backticks (`) in JSON files. This limits how I can format my strings. Is there a way to make these messages more dynamic? I want to be able to insert variables and maybe even use some basic formatting.

Has anyone tackled this issue before? What’s the best approach for handling multiple languages in a Discord bot while keeping the messages flexible? Any tips or examples would be super helpful!

Having implemented multilingual support in several Discord bots, I can share a practical approach. Consider using a templating library like Handlebars or Mustache. These allow for more complex string interpolation within JSON files.

For example, your JSON could look like:

{
  "levelupmessage": "Congratulations on reaching level {{level}}!"
}

In your code, you’d then use the templating engine to render the string with the appropriate variables. This method provides flexibility for formatting and variable insertion while maintaining clean, readable JSON files.

Additionally, structuring your language files hierarchically can help organize complex bots with many commands and responses. This approach scales well as your bot grows in functionality and language support.

hey, i’ve got a diff approach for ya. try using string interpolation with ${} in ur code instead of json. keep ur json simple like "levelupmessage": "Congrats on new level" and handle the formatting in ur bot logic. it’s way more flexible an u can add all the fancy stuf u want. jus a thought!

I’ve dealt with this exact problem in my own Discord bot projects. Instead of using backticks in JSON, I switched to using placeholders like {level} or %level%. Then in my code, I use a simple string replacement function to swap out these placeholders with actual values.

For formatting, I ended up using Discord’s markdown syntax directly in the JSON strings. So bold text would be like this and italics like this. It’s not as clean as template literals, but it works well enough.

One trick I found helpful was creating a utility function that takes the language string and an object of replacements. Something like:

formatString(‘levelupmessage’, { level: 5 }, ‘en’)

This made it really easy to use throughout the codebase. Hope this helps! Let me know if you want more details on implementation.