I was speaking with @Ali_RS recently about jME-TrueTypeFont and happened to mention that one of the goals there was creating an environment that was more conducive to supporting multiple languages and it reminded me of a more general Java library I wrote a while ago and figured I’d share here.
JLing is a library that I created to help facilitate the creation of Java applications with support for more than one language. Inspired by the way localization is done in Android JLing is actually incredibly simple. You can find it on my Google Drive at JLing.zip - Google Drive
The sources and javadoc are included in the jar. Basic usage is quite simple, first, rather than directly include Strings in your game you write them into an XML file that contains multiple translations of the same string, each Translation is given the same identifier name. So, for example:
<Languages>
<English>
<entry name="language">We speak english.</entry>
</English>
<French>
<entry name="language">Nous parlons français.</entry>
</French>
</Languages>
Then in your application you instantiate a JLing object and set it to the desired language:
JLing jl = new JLing("Resources/Lang/Example.xml");
try {
jl.setLanguage("English");
} catch (SAXException e) {
//A problem was encountered parsing the XML file.
} catch (IOException ioe) {
//A problem was encountered opening the file.
}
Keep in mind that JLing was not created specifically for jME so it does not use the assetManager, to use this you must store your XML files in a package in your jar, not the Assets folder. In the above example the package Resources.Lang was created and Example.xml was placed in that package.
Once the language is set you reference your strings via the name attribute so when you want to print something to the screen in the selected language you use JLing.get("identifierString")
:
Label label = new Label(jl.get("language"));
try {
jl.setLanguage("French");
} catch (SAXException e) {
//A problem was encountered parsing the XML file.
} catch (IOException ioe) {
//A problem was encountered opening the file.
}
Label label = new Label(jl.get("language"));
Another good point to note, when JLing parses the XML it loads all of the text for a particular language into a HashMap. This is probably no big deal for arcade shooters and such, but RPGs and other text heavy games might not want to have every String ever displayed throughout the entire game all loaded into memory at the same time. For these cases it is probably best to maintain multiple xml files, for instance you might have one containing all the text for one region in your RPG game world and another for another region then load the proper XML file when the player enters the respective region in the game.
Since we’re just talking about Strings here this could also be used to load different textures or fonts:
<Languages>
<English>
<entry name="UITexture">Textures/UI/English/Title.png</entry>
</English>
<French>
<entry name="UITexture">Textures/UI/French/Title.png</entry>
</French>
</Languages>
try {
jl.setLanguage("French");
} catch (SAXException e) {
//A problem was encountered parsing the XML file.
} catch (IOException ioe) {
//A problem was encountered opening the file.
}
Texture = assetManager.loadTexture(jl.get("UITexture"));
You do not have to use JLing.setLanguage(String) every time you want to get a String, you only set the language once at which point JLing parses the XML file and loads all the strings from the selected language. When you want to change to a different language then you just call JLing.setLanguage(String) again.
Also good to note, XML files need to be parsed so certain special characters in your Strings should be replaced with entity references, for instance <
should be replaced with <
. Check out the entity reference section here: http://www.w3schools.com/XML/xml_syntax.asp
P.S. I also made one for C# called CSLing http://1337atr.weebly.com/scripts.html