-
Notifications
You must be signed in to change notification settings - Fork 18
Localisation
Localisation simply refers to the process of translating text within an application to that of a user's preferred language. In IB.ai we achieve that with the i18n
package, located at /src/main/java/com/ibdiscord/i18n
. The lang directory is located at /lang/
.
- Duplicate the 'en' (English) directory, from
/lang/
- Rename it to the new language
- Add the language to the
available_languages.json
file - Start translating line-by-line. Leave all English translations in place until a suitable foreign translation has been made
To reference the main 'localiser' method (public String translate(CommandContext context, String key, Object... format)
), you should use LocaliserHandler.INSTANCE.translate(CommandContext context, String key, Object... format)
.
This is often not needed as the CommandContext has many helper methods that make using translations easy.
// Without i18n.
context.replyRaw("Consider it done");
// With i18n.
context.reply("success.done");
What this is doing internally is:
- Finding the user's preferred language from the database (reason the context object is needed)
- Looks in the JSON file 'feedback' (first half of the 'key' parameter)
- Finds the string at the key 'done'. In this case, the string would be "Consider it done"
- That value is returned as a string.
The JSON file that is queried can be found in the directory corresponding to the language of choice for the user. The names of the JSON files and the keys located within them are replicated across all of the different language directories. In the case of having Spanish and English options with the example above, the structure would look like:
lang/
en/
feedback.json
es/
feedback.json
Inside each feedback.json:
{
"done": "Consider it done.",
}
{
"done": "Considérelo hecho.",
}
If the user has Spanish as their preference, 'Considérelo hecho' would be sent once a command is successful.
The idea is that all Strings that are user-facing (including those in embedded messages) should at least be localised to English and found within the json files.
Some strings may have variables inside of them. These are slightly more complicated. For example, Thank you, Pants, you are not a bot.
would look like this:
{
"thankUser": "Thank you, {0}, you {1} a bot.",
}
This example is a little contrived, but would look like this:
context.reply("feedback.thankUser",
context.getMember().getUser().getName(),
commandContext.getMember().getUser().isBot() ? "are" : "are not"
);
or...
{
"userID": "Your user ID is: {0}.",
}
context.reply("feedback.userID", context.getMember().getUser().getId());
An IB Discord Server project. Community funded and available CAS hours for IB students.