Using locale-dependent methods on locale-dependent data can produce unexpected results when the locale is unspecified. Programming language identifiers, protocol keys, and HTML tags are often specified in a particular locale, usually Locale.ENGLISH
. However, a user can run the program in a different locale, which may cause the program to behave erratically. It may even be possible to bypass input filters by changing the default locale. For this reason, any program that inspects data generated by a locale-dependent function must specify the locale used to generate that data.
Some programs use locale-dependent functions only for presenting output, such as dates. In these cases the locale-dependent data is not inspected by the program, and it may safely rely the default locale.
For example, most European languages associate the letter I
as the uppercase version of i
. But Turkish is an exception: it has a dotted i
whose uppercase version is also dotted: İ
, and an undotted ı
whose uppercase version is undotted I
. Changing capitalization on most strings in the Turkish locale therefore produces surprising results [API 2006].
For example, the following program:
Code Block | ||
---|---|---|
| ||
public class Example { public static void main(String[] args) { System.out.println( "DavidTitle".toUpperCase()); } } |
behaves as expected in an English locale:
Code Block |
---|
% java Example DAVIDTITLE % |
but produces different results in the Turkish localeHowever, most European languages associate the letter I
as the uppercase version of i
. But Turkish is an exception: it has a dotted i
whose uppercase version is also dotted: İ
, and an undotted ı
whose uppercase version is undotted I
. Changing capitalization on most strings in the Turkish locale [API 2006] may produce unexpected results:
Code Block |
---|
% java -Duser.language=tr Example DAVİDTİTLE % |
Programs that use locale-dependent functions only for presenting output, such as dates. In these cases the locale-dependent data is not inspected by the program, and it may safely rely the default locale.
Noncompliant Code Example (toUpperCase()
)
In HTML, tags are case-insensitive, and can therefore be specified using uppercase, lowercase, or any mixture of cases. This noncompliant code example uses the locale-dependent String.toUpperCase()
method to convert an HTML tag to upper case, to check it for futher further processing. While the English locale would convert "title" to "TITLE", the Turkish locale will convert "title" to "TİTLE", and the check will fail.
...