DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. A Step by Step Guide to Internationalization for your Java Project

A Step by Step Guide to Internationalization for your Java Project

Cagdas Basaraner user avatar by
Cagdas Basaraner
·
Mar. 07, 12 · Interview
Like (0)
Save
Tweet
Share
57.39K Views

Join the DZone community and get the full member experience.

Join For Free

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:
  1.  
    • java.util.ResourceBundle (doesn't need spring dependency)
ResourceBundle initialization


  1.  
    • 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

 

From http://codebuild.blogspot.com/2012/02/customized-internationalization-i18n-in.html

Java (programming language) Strings

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Cloud Performance Engineering
  • Custom Validators in Quarkus
  • A Beginner's Guide to Infrastructure as Code
  • Testing Repository Adapters With Hexagonal Architecture

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: