One of my favourite features is how easy Java makes a number of tasks – so that I can focus on the hardcore coding problems rather than tedious ‘admin’ issues. One of these is the way it allows you to setup so that your application can display languages according to the language of the user.
It is incredibly easy to setup your java application so that all the various component texts and messages can be swapped out for the correct language on start up. It can also automatically set currency symbols and even text orientations.
Java allows you to replace messages with tokens which can easily be replaced with the version appropriate for the user. Our JPedal PDF library uses the Messages class to support some languages whilst leaving the option for unsupported languages to be added by our users to fit their needs. You can have a default if you do not happen to be fluent in all languages…
The possible values are very easy to setup – just a folder containing text files for the various languages we have support for and load this folder into a resource bundle. Once we have loaded the resource bundle into the Messages class we load the keys and messages for the current locale into a map.
Now every time we require a components text or a message to the user we just need to make a call to Messages.getMessage(String key). The returned String is the text for the given component or message for the current Locale determined by the Java runtime.
The files used by the resource bundle are set to default to english if a file for the current locale is not set. If the user should want to support a certain locale or language they need only to copy an existing file from org/jpedal/international and add new text for each key. The advantages of having these files set up mean that once these classes are setup they are universal utilites that can easily be moved to other applications.
What is your favourite language feature in Java?
IDRsolutions develop a Java PDF library, a PDF forms to HTML5 converter, a PDF to HTML5 or SVG converter and a Java Image Library that doubles as an ImageIO replacement. On the blog our team post about anything interesting they learn about.