Core Java Advanced LiveLessons

Internationalization

Copyright © Cay S. Horstmann 2016

Understand the concept of a locale

Locales

Specifying a Locale

jshell

Locale en_US = Locale.forLanguageTag("en-US");
Locale fr_CA = Locale.CANADA_FRENCH;
en_US.getDisplayName()
en_US.getDisplayName(fr_CA)

Use locale-specific formatting for numbers and dates

Number Formats

jshell

import java.text.*;
Locale de_DE = Locale.GERMANY;
NumberFormat formatter = NumberFormat.getCurrencyInstance(de_DE);
double amt = 123456.78;
formatter.format(amt)
NumberFormat.getCurrencyInstance(Locale.US).format(amt)
NumberFormat.getCurrencyInstance(Locale.forLanguageTag("th-TH-u-nu-thai")).format(amt)

Date and Time Formats

jshell

import java.time.*;
import java.time.format.*;
DateTimeFormatter formatter =
   DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
LocalDate now = LocalDate.now();
formatter.withLocale(Locale.US).format(now);

Repeat with Locale.FRENCH and Locale.CHINESE

Month and Weekday Names

JShell

import java.time.*;
import java.time.format.*;
import java.util.stream.*;

List<String> weekdayNames(Locale locale) {
   return Stream.of(DayOfWeek.values()).
      map(m -> m.getDisplayName(TextStyle.FULL, locale)).
      collect(Collectors.toList());
}

weekdayNames(Locale.US)
weekdayNames(Locale.CHINESE)
weekdayNames(Locale.forLanguageTag("fi"))

import java.time.temporal.*

WeekFields.of(Locale.US).getFirstDayOfWeek()
WeekFields.of(Locale.GERMAN).getFirstDayOfWeek()

Work with strings in multiple languages

Collation

String Normalization

lesson07/collation

Letter Case

Message Formatting

Choices

Organize locale-specific data in resource bundles

Resource Bundles

lesson07/retire