I’m currently working on a bigger Java desktop-application and now are at the point where I want to add translations. What bothers me about the l18n-system is, that it doesn’t provide any kind of compiletime checking.
In java’s system, you have something like a HashMap, where every localized string has a “Key” and the translated string then is the “Value”. This looks something like this (taken from the tutorials example):
Locale currentLocale;
ResourceBundle messages;
currentLocale = new Locale(language, country);
messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
System.out.println(messages.getString("greetings"));
This works nice if you have a simple/small application. But in a big application with thousands of translated strings, it might so happen that you have a typo in the “Key” and therefore get an empty or wrong string.
With a little luck, the application throws a RuntimeException to tell you about it, but even then it is possible that you don’t even come to this point because the wrong “Key” is used in a dialog that might not show up under certain circumstances (say it’s an error dialog).
To prevent this from happening, using a system which offers compiletime checking of the used “Keys” would be the better idea. This is for example used in Android, where you specify the Resources in an XML-file which is then indexed and mapped to a class (including the “Keys” to use). This way, you get something like this (from the Android Docs):
// Set the text on a TextView object using a resource ID
TextView msgTextView = (TextView) findViewById(R.id.msg);
msgTextView.setText(R.string.hello_message);
If you have a typo in that “key”, you’ll get an error at compile-time (also you have “auto-completion” from your IDE).
Now, to make something like this work, you’ll need a little tool/script that does the indexing part and generates the Resource-class (R.java). In Android, the Eclipse-plugin (or your IDE in general) does that for you.
My question now is: Is there already a system that that I can use for normal desktop-applications in Java? Or am I horribly wrong with what I’m saying?
There is a fairly simple solution to this problem. First, don’t use magic strings as codes, define constants and refer to them. So you change..
to
and have the corresponding class;
Next write a test case where each code in your
I18class is looked up in each language resource file. If any resource file is missing the code, then fail the test. Its not compile time, but your project will fail its test if you have any problems.