Cagdas Basaraner

About Cagdas Basaraner

Cagdas Basaraner is a software engineer graduated from Hacettepe University Computer Engineering department (Turkey), having 5 years professional experience. He is working on JEE web technologies, and also a former developer of information systems using Microsoft technologies and Command & Control (C4I) systems with Java technologies.

Customized Internationalization (i18n) in Java

Internationalization (i18n) is very important in our software projects. It brings mainly these benefits:

  • Externalizing UI strings into external files other than code files and so easy-to-manage UI content.
  • Supporting multiple languages.
In this post, a brief practical example of i18n will be given for Eclipse and Java projects, including customizing i18n mechanism to have more maintainable and encapsulated approach.

  • First, we must have some classes which includes string values shown on user interface:
An example UI class
  • Then we must have an instance of an i18n utility class. This is generally one of the two in Java:
    • java.util.ResourceBundle (doesn’t need spring dependency)
ResourceBundle initialization
    • org.springframework.context.support.ResourceBundleMessageSource (has multiple word externalization capability (which will be told later)).
ResourceBundleMessageSource initialization
We’ll use ResourceBundleMessageSource instance in this tutorial because of its extended capabilities.

  • Then right click on the class and choose Source –> Externalize Strings. A window as below will be shown. Enter keys for strings into the right column. Keys will be started with class name as default. Keys must be unique on the system, so a predefined pattern should be applied (e.g. <class_name>.<type_id>.<description>)
Eclipse string externalization window
  • Click Next–> Finish and your strings will be changed as follows. And also Messages class and externalizer properties file will be created automatically (auto-comments on the right are markers for eclipse which means
    “externalized”):
Class with externalized strings
Auto-created i18n utility and property classes
  • Now externalization is complete. But we want i18n, and we must support multiple languages. For this, define another properties file with location post-tag (e.g. “EN”, “FR”, “TR”, …), copy keys and fill values with new language and set locale of resource bundle in a proper place/action of your application (e.g. on settings window or login page):
Multiple property files for each language
Messages_tr_TR.properties file content
Setting new locale to the resource bundle
  • As the last step, we want to encapsulate i18n utility class and also want to use a more  capable i18n utility (e.g.
    ResourceBundleMessageSource). For this, define a class as below:
Customized and encapsulated message source (i18n utility) class
  • Finally, change “Messages.getString” statements into your new instance:
Class with externalized strings (with customized i18n utility class)
  • You can also externalize parameterized strings using your class. Its usage will be as follows:
Getting a parameterized string from i18n utility
Defining a parameterized string in property file

Reference: Customized Internationalization (i18n) in Java – Step by Step from our JCG partner Cagdas Basaraner at the CodeBuild blog.

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

Leave a Reply


× seven = 35



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books