Java SE 8 Programmer II study materials
Java SE 8 Programmer II study resources

Java SE 8 Programmer II: Localization

Java SE 8 Programmer II · Work through your exam, one topic at a time.

All guide sections

Localization

Localization tests Locale, resource bundles, formatting, and locale-sensitive parsing.

Core rules

  • A Locale has language, country, and optional variant; Locale.forLanguageTag("en-US") uses BCP 47 tags. Locale.getDefault() returns the JVM default.
  • ResourceBundle.getBundle("messages", locale) loads messages.properties plus locale-specific variants. Resolution tries base_language_country_variant, then base_language_country, then base_language, then the base bundle, then the default locale.
  • Properties bundles in Java 8 are read as ISO-8859-1; non-Latin text is usually escaped with Unicode escapes.
  • MessageFormat inserts arguments with {0}, {1}, and supports choice, date, and number subformats. Patterns use single quotes for literal braces.
  • NumberFormat and DateFormat are locale-sensitive and are not thread-safe; create per use or synchronize.
  • DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(locale) formats java.time values.

Worked example

Locale l = Locale.forLanguageTag("fr-FR"); ResourceBundle b = ResourceBundle.getBundle("app", l); String s = b.getString("greeting"); If app_fr_FR.properties is missing but app_fr.properties exists, the latter is used. If no French bundle exists, the base app.properties is used; a missing key throws MissingResourceException.

Traps

  • new Locale("fr", "FR") and Locale.forLanguageTag("fr-FR") both work, but Locale constructors do not validate.
  • Bundle lookup falls back through the parent chain; a key missing everywhere throws MissingResourceException.
  • MessageFormat treats a single quote as an escape; '{0}' is literal, not a placeholder.
  • NumberFormat.getInstance() uses grouping and locale decimal separators; it is not a parser for arbitrary formats.
  • Formatting with the default locale gives different output on different machines.