Internationalization and localization tools


Locale-Sensitive Java Method

java.lang.String

public int compareTo(String anotherString)

Internationalization (I18n) Method Overview

This method compares two strings based on the UTF-16 Unicode numeric code points of each character in the strings. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.

I18n Issues

When this method is being used to compare two strings for the purpose of collation (alphabetic sorting), it should be replaced with the appropriate call to the java.text.Collator class to take advantage of locale-based character sorting.

Globalyzer will not be able to determine whether you are calling this method for purposes of determining equality or to perform locale-sensitive character collation. In cases where it is called to determine String equality and hence you know that the call does not pose an I18n problem, you can use Globalyzer's Ignore Comment functionality to ensure that it isn't picked up again in a subsequent scan.

Suggested Replacement

java.text.Collator

public int compare(Object o1,
   Object o2)

Instead of:

int i = stringA.compareTo(stringB);

Use:

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

//pass the user's locale as an argument
Collator myCollator = Collator.getInstance(locale);
int i = myCollator.compare(stringA,stringB);

Please see Java Collation for more information.

Locale-Sensitive Java Methods

 

 

Lingoport internationalization and localization services and software