I am currently working on a java swing GUI for a project. The user interface will need to be in multiple languages(English German French, etc...) that I do not know. It was suggested that I use a config file to store all of the text so that a translator can translate after the project is complete without recompiling. This has the added bonus of being able to change text and add languages after completion, and I could also put some configurations for the UI in here so that it is more configurable. After some reflection though, I realized this would be very slow, as I would need to do file IO every time the GUI changed. How can I design a GUI so that I can still have a layman translate it without me, but not slow down the program?
There are definitely many ways of doing this, but here is one possible implementation if you wish to write it yourself.
Following the ISO 639 language codes, you will typically have a resource directory laid out like so:
resources/
languages/
en/
strings.conf
de/
strings.conf
Then when your application first loads, you will get the users default language, then load the language file accordingly; the file should be only loaded once from disk, and then kept in memory. You can use something like the following, and by keeping an instance of the LanguageLoader, you can call .getStringById()
and load a key by name.
public class LanguageLoader() {
final HashMap<String, String> localizedStrings;
public LanguageLoader(String language) {
localizedStrings = new HashMap<String, String>();
File f = new File( "languages/" + language + "/strings.conf" );
// TODO read file line by line and add key / value pairs to hash map
}
public String getStringById( String stringId ) {
return localizedString.get(stringId);
}
}
And in your application
String languagePack = Locale.getDefault();
LanguageLoader localization = new LanguageLoader(languagePack);
String inAnyLanguage = localization.getStringById("welcome_001");
Where your language files consist of
en/strings.conf
welcome_001=Hello World!
de/strings.conf
welcome_001=Hallo Welt!