Skip to content

Plugins_Translation

Lars Bärtschi edited this page Jan 24, 2018 · 1 revision

Translations API

An easy to use translation API is included in Twasi. It is very lightweight and supports fallback-languages. It's fully integrated and automatically selects the language by the current user.

Translation Files

You have to pack the translation files into your plugin jar. For that, create a new folder in the resources directory which is called "translations". Inside, create a file for every language you support. The file names are described in the table below.

Every translations file consist of key-value pairs. The key is always separated by the first = (equal sign). Therefore, the key is not allowed to use the char = (equal sign). We recommand for better readability to split the key with dots per feature / part of your application.

The translation-file EN_GB.lang of the plugin commands looks like this:

add.usage=Usage: !add [command] [content] Example for !bot: !add !bot Twasi is cool!
add.successful=The command %s was added successfully.
add.alreadyExist=The command %s does already exist.

edit.usage=Usage: !edit [command] [new content] Example for !bot: !edit !bot Twasi is the most expandable bot!
edit.successful=The command %s was edited successfully.
edit.doesntExist=The command %s doesn't exist.

delete.usage=Usage: !delete [command] Example for !bot: !delete !bot
delete.successful=The command %s was deleted successfully.
delete.doesntExist=The command %s doesn't exist.

commands.noneFound=There are no commands available right now. But hey, you can add one: !add [command] [content]
commands.available=Available commands: %s

Supported languages

Language Code (filename) Language Description Supported by (maintainer)
EN_GB (EN_GB.lang) Default english Twasi-Team and plugin contributors
DE_DE (DE_DE.lang) German Twasi-Team and plugin contributors

If you want to help us translate (into other languages), get in touch: [email protected]

Format Specifiers (templates)

You can pass in your own variables using the common java string format. Use %s for a string, or %d for a decimal integer. Learn more about Java Format Specifiers here.

Plugin API

So you are a plugin and want to translate some text? Here you go!

Just use this code in your TwasiUserPlugin (since translations are based per user, it is only available there):

User user = getTwasiInterface().getStreamer().getUser();

// Without format specifiers
String translated = getTranslations().getTranslation(user, "add.usage");

// With format specifiers (you can specify an infinite amount of additional specifiers)
String name = "myCommandNameThatWillBePutIntoTheTranslationYeeeyyyy";
String translated2 = getTranslations().getTranslation(user, "add.successful", name);