We use java.util.Locale to format dates, numbers, currency, and more. But in some circumstances, these formatted strings have changed with JDK 9, leading to a multitude of subtle (and sometimes not so subtle) bugs.

TABLE OF CONTENTS

What Are Locales?
Where Do Locales Come From?
What Changed With JDK 9?
How to Deal With the Changes
Conclusion

What Are Locales?

To better understand a problem, we must first know what we’re dealing with.

“A locale is a set of parameters that defines the user`s language, region, and any special variant preferences that the user wants to see in her user interface ”— Wikipedia

The core use of a locale is formatting many kinds of different data for output purposes:

  • Numbers
  • Date, time, weekdays, eras, etc.
  • Currency
  • String collation
  • etc.

Locales are defined by a hierarchy of properties:

<language>[_<COUNTRY>[_<variant>]]

language:  ISO 639 (mandatory)
COUNTRY:   ISO 639 (optional)
variant:   any string (optional)

These three parameters are enough to define most combinations possible. Often a language is enough, but sometimes we need to be more specific. The variant is used to specify a locale even further:

Locale.GERMAN        => de
Locale.GERMANY       => de_DE

Locale.ENGLISH       => en
Locale.UK            => en_GB
Locale.CANADA        => en_CA
Locale.FRENCH        => fr
Locale.CANADA_FRENCH => fr_CA

#java #software-development #programming #localization #software-engineering

Localization Changes in Java 9
2.00 GEEK