How to write internationalized Java applications?

This post answers questions like — How to write internationalized Java applications? What do you understand by the terms internationalization (i18n) and localization (l10n) relating to Java programming? What is a ResourceBundle?

Localization (aka i10n, where 10 is the number of letters between the letter ‘i’ and the letter ‘n’ in the word localization ) refers to the adaptation of an application or a component to meet the language, cultural and other requirements to a specific locale (i.e. a target market).

Internationalization (aka i18n, where 18 is the number of letters between the letter ‘i’ and the letter ‘n’ in the word internationalization) refers to the process of designing a software so that it can be localized to various languages and regions cost-effectively and easily without any engineering changes to the software. A useful website on i18n is http://www.i18nfaq.com.

Q. What are the characteristics of an internationalized program?
A.

  • The same executable can run worldwide without having to recompile for other or new languages. 
  • Text messages and GUI component labels are not hard-coded in the program. Instead they are stored outside the source code in “.properties” files and retrieved dynamically based on the locale. 
  • Culturally dependent data such as dates and currencies appear in formats that conform to end user’s region and language. (e.g. USA date format mm/dd/yyyy, Australian date format dd/mm/yyyy). 

Q. What are the different types of data that vary with region or language?
A. Messages (error messages, warning messages, info messages, etc), dates, currencies, numbers, measurements, phone numbers, postal addresses, tax calculations, graphics, icons, GUI labels, sounds, colors, online help, etc.

Q. What is a Locale?
A.  A Locale has the form of xx_YY (xx – is a two character language code && YY is a two character country code. E.g. en_US (English – United States), en_GB (English – Great Britain), fr_FR (french – France). The java.util.Locale class can be used as follows:

Resource bundles can be created using the locale to externalize the locale-specific messages:

Message_en_US.properties


Message_fr_FR.properties


These resource bundles reside in classpath and gets read at runtime based on the locale.

Note: When paired with a locale, the closest matching file will be selected. If no match is found then the default file will be the Message.properties. In JEE, locale is stored in HTTP session and resource bundles (stored as *.properties files under WEB-INF/classes directory) are loaded from the web.xml deployment descriptor file. Locale specific messages can be accessed via tags (e.g. Struts, JSTL etc).

The java.text package consists of classes and interfaces that are useful for writing internationalized programs. By default they use the default locale, but this can be overridden. E.g. NumbeFormat, DateFormat, DecimalFormat, SimpleDateFormat, MessageFormat, ChoiceFormat, Collator (compare strings according to the customary sorting order for a locale) etc.

DateFormat:


NumberFormat:

To use default locale:

To use specific locale:

Q. When a web application has users with different time zones, how will you manage dates and time in the system in a manner that looks consistent to all users
A.

  • UTC is the coordinate universal time in the UK (like Greenwhich mean time). All time zones are calculated as offsets to UTC time. For example, Sydney is UTC+10, India is UTC+5.30, etc.
  • During somer, some regions do have the daylight savings. UTC time is independent of daylight savings time and it needs to be taken into consideration.
  • If your application needs to store dates and time internally, for example in a database, convert it to UTC time before storing it.
  • When the user retrieves records with date/time, it gets converted to their locale time as users like to see that date and time converted to his own time zone.
  • Websites need to capture the user customization info like preferred language, country of residence, current location, preferred time zone

Q. What is the difference between String.compareTo( ) and Collator.compare()?
A. Every language has its own rules for how strings and letters are sorted. So, using the String.compareTo( ) method may not work for all languages. So, you need use a java.text.Collator instance created for a specific Locale.


Q. How does Java keep stings internally?
A. In Java all strings are kept in unicode (UTF-16 is 16 bit unicode). Since not all text received from users or the outside world is in unicode, your application may have to convert from non-unicode to unicode when reads and do the reverse by converting from unicode to non-unicode when writes. The Java String class and io package have methods to perform this conversion.


300+ Java & Big Data Interview FAQs

16+ Java Key Areas Interview Q&As

800+ Java Interview Q&As

300+ Java & Big Data Tutorials

Top