Plugin developers

How to make my plugin localizable?

Translate works by loading strings associated with keys into a HashMap. This means that for each string litterals in your plugin sources, you'll need to use Translate's string instead. Translate is finely tuned to make this as easy as possible. First step is setting up your project to use Translate.

Setting up the project

  1. Create a new file in at the root of your project (where plugin.yml is located).
  2. Call it "language.txt".
  3. Add the Translate plugin to your build path, just like you did for Bukkit.
  4. Add Translate to the softdepend option of your plugin.yml file.

"language.txt" is the default language file of your plugin. On the first launch of your plugin with Translate, this will be be copied over in the "Translations" folder. Make sure to include it in your JAR export options (like plugin.yml).

Modifying your code

There are two ways of getting the string associated to a key. The first one is to get them from Translate's TranslationManager. The second one is to get them using a PluginHook instance. In both case, for each Id you use, you must add a key and string to the Translate.txt file.

Using the TranslationManager

Best explained with a code example :

TranslationManager translationManager=Translate.getTranslationManager();
translationManager.getString(this, "exampleId");

Not that the getTranslationManager is a static method, so you don't need to get the Translate plugin from Bukkit. The arguments for the getString(Plugin, String) method are the plugin you want to get the id for (most likely yours) and the string Id you want to get. Also note that the getString method's argument is case-insensitive, it will always be converted to upper case.

Using a PluginHook

PluginHook is a wrapper around TranslationManager. Here's a code example :

class SimplePlugin extends JavaPlugin {
	public static PluginHook t;

	@Override
	public void onEnable() {
		t = Translate.getPluginHook(this);
		LocalizedLogger.log(t.s("enabled"));
	}
}

Note that getPluginHook is a static method, so you don't need to get the Translate plugin from Bukkit. PluginHook takes the plugin you want to get future Ids from (usually yours) as its constructor argument. PluginHook has a shortened s(String) method, which works as its longer counterpart getString(String).

The LocalizedLogger Object

Translate also offers a LocalizedLogger object, which only have a static log(String) method. Its only difference with the classic Logger is that it will allow displaying unicode characters into the Windows console.


Comments

Posts Quoted:
Reply
Clear All Quotes