Internationalization and localization tools


Locale-Sensitive Java Method

java.text.BreakIterator

public static BreakIterator getSentenceInstance()

public static BreakIterator getSentenceInstance(Locale locale)

Internationalization (I18n) Method Overview

The BreakIterator class implements methods for finding the location of boundaries in text. Instances of BreakIterator maintain a current position and scan over text returning the index of characters where boundaries occur. The zero-argument signature for the static getSentenceInstance method returns a BreakIterator instance that looks for sentence-breaks using system's default locale.

I18n Issues

What users consider to be a sentence break differs between languages. The getSentenceInstance method provides two signatures - one that accepts Locale and one with zero arguments. When no Locale object is passed in as an argument the BreakIterator instance operates according to the rules of the default system locale. These rules might not be correct for all users. For this reason it is good I18n practice to instead use the signature that accepts a Locale argument.

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 getSentenceInstance(
   Locale where)

Instead of:

BreakIterator boundary =
  BreakIterator.getSentenceInstance();

Use:

//retrieve the run-time user's locale
Locale aLocale = getUserLocale();

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

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

Locale-Sensitive Java Methods

 

Lingoport internationalization and localization services and software