Setup - LunarWatcher/JVMI18N GitHub Wiki

Setup can either be done by downloading the jar from releases, or by using Gradle, Maven or a similar build system.

This particular guide uses Gradle, but it can easily be adapted to any other build system with similar abilities. First, add the repo url:

repositories {
    mavenCentral()
    maven{
        url 'https://raw.githubusercontent.com/LunarWatcher/mvn-repo/master/release/'
    }
    //Other repos
}

That gives access to the repo where the jars are stored. Now you need to add the dependency itself:

dependencies {
    //other dependencies
    compile "io.github.lunarwatcher.jvmi18n:jvmi18n:1.0.1"
}

And that's it! You may need to sync Gradle or whatever build system you are using to get it to fully update, but you should now be able to access the library, and get on to the actual setup of the code.

You're going to need the translation files set up. For testing, a single one will do. The files (default extension is .i18n) is a .properties file that contains the translations. In addition to the default system, this library allows for advanced formatting which you'll read more about later.

This library isn't just used for translations. It could also be used to move the Strings you use to an external file. A base bundle is the minimum requirement for anything to work, but having a translation file is optional.

The files are split into two categories: base resources and translation resources. The base resources are the base for a given translation set. if you have a String resource set you call characterClasses.i18n, that is the name of the base resource. The translation resources are named with the base name and the locale. For an instance characterClasses-en_US.i18n. The naming conventions are strict:

Base bundle: "[base name].[extension]"
Translation bundle: "[base name]-[locale].[extension]"

The extension is universally used for all the resources, meaning there can't be one file with a different extension.

With all of this in mind, you can load a translation easily:

Translation translation = new Translation("en_US");
try {
    translation.addBaseBundle("res/", "mainBundle");
    translation.addTranslation("mainBundle", "nb_NO");//example of a translation resource
}catch(IOException e){
    System.err.println("File not found!");
    e.printStackTrace();
}

Translation is the core class in all of this, and it's the one you'll be dealing with the most. It's the interface to the other classes, though they too can be used for a custom system.

Assuming there are resources in the loaded bundle, you can now call:

String string = translation.get("key");

The code checks for resources in two sets:

Translation applied by locale -> Base

If there are no translations, it skips that step and goes straight for the base reference. If a translation has a key that isn't in the base resource, and the locale is changed to a resource without that key and that key is called, an exception will be thrown. If you want to always throw an exception if the key isn't found in the translation, set the static boolean CRASH_IF_NOT_FOUND in the Translation class to true.

And that's it! You now have the ability to load translations or just non-translated Strings from files in just a couple of lines of code. The library itself has more behind it you can use for storing Strings in whatever way you need.