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?
Latest posts by Kieran France (see all)
- Off Screen Rendering Improvements to JPedal Viewer - February 2, 2018
- How to use Cipher streams in Java - January 10, 2018
- Improved Shape Rendering with Soft Clipping - August 24, 2017
- 4 Hidden Features in Java 9 - May 30, 2017
- Why HTTP/2 Client in Java 9 is important - May 23, 2017