Developers

Developers


API Methods


  • Language.get(String key, String template, Object... params) returns String

Get a formatted string constructed using the locale-specific template for the given key, with the given parameters inserted.

The default language.yml file is constructed using the given templates and provide a basis for translators to alter the file for other locales. The default locale template should contain text, and where parameters need to be inserted, there should be a number surrounded by curly braces

E.g. {0} has earned ${1} credits for {2}

Each parameter is inserted into the brace with the same index, so the first parameter replaces {0}, the second replaces {1} and the third replaces {2} and so on. There is no limit to the number of parameters that can be inserted, but there must be a corresponding parameter for every brace.

The "key" parameter is the key for this string template. It determines what this template is stored under in the language.yml. In the language.yml file, the keys must never be altered, only the message templates should be translated

The "template" parameter is the default locale template. This is what should be used as the default language for the plugin and a starting point for translation. This template will be stored in the language.yml automatically the first time it is encountered.

The "params" parameters are the dynamic data that are to be inserted into the string template.

The formatted string is returned, using the language template from the language.yml file, if it is available, or the default if it is not, with the values of the dynamic data parameters inserted into the template in place of their respective curly braces.


Example Plugin


A simple example plugin. Notifies the console when the plugin is activated, greets players when the join the server, and provides a /wave command to display an emote where you wave to another player. All output strings can be translated using the language.yml file.

Source code available on github: https://github.com/XHawk87/LanguageAPIExamplePlugin


Example Wrapper


The wrapper is intended to inserted into your plugin as a way to separate the LanguageAPI from your plugin. It will check if the LanguageAPI is loaded, and use it if it is available, but if it isn't then it will simply use the default translation. This allows you to offer your plugin for download without needing the LanguageAPI to go with it so long as your users do not need translations. If they do, they can download the LanguageAPI and slot in the correct language.yml file and it will use the translated strings.

Source code available on GitHub: https://github.com/XHawk87/LanguageAPIExamplePlugin


Testing


String templates are added to the default language.yml as they are executed, so be sure to thoroughly test your code before handing out the default language.yml file, or it will have omissions.

You could call each of your strings again at startup, however this is not recommended as it shows that you haven't thoroughly tested your plugin yet. Releasing untested plugins is against the BukkitDev terms of service, and it is lazy and unprofessional. So test your plugins!


Comments

Posts Quoted:
Reply
Clear All Quotes