Internationalizing a Locale-specific Application in Java [con't]
You must also take care to format numbers and currencies in a locale-independent manner. The following code will not display an internationalized number and will format it the same way for all countries:
A better way to write the above code is to take advantage of Java's several
number and currency formatting classes:
The preceding code would print something like the following, depending on the
currentLocale
value:
- English (US): 345,987.246
- French (France): 345 987,246
getCurrencyInstance()
to create a formatter instance. When you
invoke the format method, it returns a string that includes the formatted number
and the appropriate currency sign. This code example shows how to format currency
in a locale-specific manner:
The output generated by the preceding lines of code might look as follows:
- French (France): 1 234 567,89 Â
- German (Dutch): 1.234.567,89 Â
- English (US): $1,234,567.89
Note that you are still responsible for calculating equivalent values using
current exchange rates!
Use Unicode Character Sets
As I wrote in the Globalize Your Web Applications: The Universal Character Set article, there are a far greater number of character sets than those used in Western languages. Without taking the full range of Unicode characters into consideration, it's all too easy to make invalid assumptions, such as in the following code, which attempts to verify that a character is a letter:
This function won't work with languages other than English. For example, it misses Unicode characters such as those found in:
- Johann StrauÃ, the waltz king.
- René Magritte, Belgian painter.
- Mika Häkkinen, Finnish formula 1 race car driver.
Java implements a number of Character
comparison methods that
use the Unicode standard to identify character properties. Thus, you could replace
the previous function body with the following:
Perform String Comparisons Using Locale-aware Methods
Comparisons between strings, such as those done when sorting text, should not
be performed on Unicode strings using the comparison methods of the String
class. The reason is that String methods such as equals()
and compareTo ()
perform binary comparisons, which are ineffective when sorting in most languages.
Hence, the following French words would be sorted as follows:
- peach
- péché
- pêche
- sin
According to the collation rules of the French language, the preceding list
is in the wrong order. In French, péché should follow pêche in a sorted list.
The Collator
class, on the other hand, accepts a locale in the constructor:
The French Collator sorts the array of words correctly, as follows:
- peach
- pêche
- péché
- sin
Up Next: Character Translation
Characters in the Java programming language are encoded in Unicode. If your
application outputs non-Unicode text, you might need to translate it into Unicode.
The next article will demonstrate some ways to accomplish that.
Have a suggestion for an article topic? Do you have a product or service that you'd like reviewed? Email it to Rob .
Rob Gravelle combined his love of programming and music to become a software guru and accomplished guitar player. He created systems that are used by Canada Border Services, CSIS and other Intelligence-related organizations. As a software consultant, Rob has developed Web applications for many businesses and recently created a MooTools version of PHPFreechat for ViziMetrics. Musically, Rob recently embarked on a solo music career, after playing with Ivory Knight since 2000. That band was rated as one Canada's top bands by Brave Words magazine (issue #92) and released two CDs. Rob's latest, entitled KNIGHTFALL, was a collaboration between himself, the former Ivory Knight vocalist, and legendary guitarist/producer, Jeff Waters of Annihilator fame. Rob is available for software projects and recording session work. to inquire, but note that, due to the volume of emails received, he cannot respond to every email. Potential jobs and praise receive highest priority!
Original: April 9, 2010