Internationalization and localization tools


Locale-Sensitive Java Method

java.text.BreakIterator

public static BreakIterator getCharacterInstance()

public static BreakIterator getCharacterInstance(Locale locale)

Internationalization (I18n) Method Overview

The BreakIterator class implements methods for finding the location of boundaries in text. The zero-argument, static call getCharacterInstance returns a BreakIterator instance that can be used to detect character boundaries within a block of text according to the rules of the system's default locale.

I18n Issues

Character boundaries within text vary according to the grammar rules of each Locale. The getCharacterInstance method provides two signatures - one that accepts Locale and one with zero arguments. In order to ensure that the resulting BreakIterator instance will detect character breaks according to the rules of the correct Locale, the signature that accepts a Locale argument should always be used and the correct runtime Locale instance should be passed in as an argument. This allows the code to detect character boundaries according to locale-rules that are independent of the OS locale.

Globalyzer will detect this method and report it as an I18n issue regardless of the signature used and regardless of whether it is being used correctly. If Locale is already being passed as an argument, Globalyzer will detect it to force developers to double check that the correct Locale is being passed. If you have determined that the call is being handled correctly, you can use Globalyzer's Ignore Comment functionality to ensure that it isn't picked up in a subsequent scan.

Suggested Replacement

public static BreakIterator getCharacterInstance(
   Locale where)

Instead of:

BreakIterator boundary =
  BreakIterator.getCharacterInstance();

Use:

//retrieve the user's locale
Locale locale = getUserLocale();

//pass the user's locale as an argument
BreakIterator boundary =
  BreakIterator.getCharacterInstance(locale);

Please see the Java Internationalization Trail for more information about the BreakIterator class.

Locale-Sensitive Java Methods

 

Lingoport internationalization and localization services and software