Internationalization for libgdx projects
Edit: I just added UTF-8 support
Localization is a key aspect in games, specially if you want to reach a wider audience. I recently ported Freegemas to the libgdx platform and, as the original one, I wanted to ship it with multi-language support. There is no such a thing as gettext for Java and I didn’t see the Android internationalization system as a good fit in a multiplatform development. That’s why I developed my little own internationalization module.
Its usage is extremely simple, first we need a media/languages.xml file where all the strings in our project will be located. The syntax is pretty straight forward, we lay language sections identified with the code for each language. Inside every language, we provide a list of key value pairs with the localized strings.
< ?xml version="1.0" encoding="UTF-8"?> <languages> <!-- English --> <language name="en_UK" > <!-- Menu --> <string key="Timetrial mode" value="Timetrial mode" /> <string key="How to play" value="How to play" /> <string key="Exit" value="Exit" /> </language> <!-- Spanish --> <language name="es_ES" > <!-- Menu --> <string key="Timetrial mode" value="Contrarreloj" /> <string key="How to play" value="Ayuda" /> <string key="Exit" value="Salir" /> </language> </languages>
We can retrieve localized strings in our project through the LanguagesManager class and its getString() method. It’s been implemented using the Singleton design pattern as we only need one instance accessible from, potentially, everywhere in the code. When we ask our manager for a certain string we will use its key, if it finds it within the current language, it’ll return it, otherwise it’ll return the key itself. That’s why using strings in a base language (English) as keys can be a good idea, those will be returned by default.
For now, it uses lazy initialization which means it’ll load the language the first time the getInstance() method is called but that might change in the future. It automatically detects the system language (no matter if we are on Android or a desktop environment) and it fallsback to English if the system language is not among the available ones in the data/languages.xml file. However, you can explicitly specify the language you want to load through the loadLanguage() method.
LanguagesManager lang; lang = LanguagesManager.getInstance(); String option1 = lang.getString("Timetrial mode"); String option2 = lang.getString("How to play"); String option3 = lang.getString("Exit");
Freegemas libgdx is open source (GPL v3) as is this internationalization module which means you’re more than welcome to download it, use it and improve it. If you do the latest, make sure you distribute it the same way as me.