Working with Internationalization and Localization

1. Understanding Locale Class

ElementDetail
IdentifiesLanguage + country + variant
ConstantLocale.US, Locale.JAPAN, etc.
DefaultLocale.getDefault()

2. Creating Locales

FormExample
ConstructorLocale.of("en", "US") (Java 19+)
TagLocale.forLanguageTag("en-US")
Buildernew Locale.Builder().setLanguage("fr").build()

3. Using ResourceBundle

Example: Resource bundle

// messages_en_US.properties: greeting=Hello, {0}!
// messages_fr_FR.properties: greeting=Bonjour, {0}!
ResourceBundle bundle = ResourceBundle.getBundle("messages", Locale.FRANCE);
String fmt = bundle.getString("greeting");
String msg = MessageFormat.format(fmt, "Alice");
AspectDetail
File namingbasename_lang_COUNTRY.properties
FallbackLocale → language → default → root
Java class bundleExtend ListResourceBundle

4. Loading Resource Bundles

MethodDetail
getBundle(name)Default locale
getBundle(name, locale)Specific locale
getBundle(name, locale, classloader)Custom CL

5. Formatting Numbers

FormExample
NumberFormat.getInstance(loc)Default
NumberFormat.getNumberInstance(loc)Plain
NumberFormat.getPercentInstance(loc)Percent
NumberFormat.getCompactNumberInstance() JAVA 12+1.5K, 2M

6. Formatting Currencies

Example: Currency

NumberFormat usd = NumberFormat.getCurrencyInstance(Locale.US);
String s = usd.format(1234.5);   // "$1,234.50"

Currency yen = Currency.getInstance("JPY");
int frac = yen.getDefaultFractionDigits();   // 0
MethodUse
NumberFormat.getCurrencyInstance(loc)Locale's currency
fmt.setCurrency(Currency.getInstance("EUR"))Override

7. Formatting Dates

MethodDetail
DateTimeFormatter.ofLocalizedDate(LONG)SHORT/MEDIUM/LONG/FULL
.withLocale(loc)Apply locale

8. Using MessageFormat

PatternMeaning
{0}Positional arg
{0,number,currency}Type + style
{0,date,short}Date format
{0,choice,...}Plural/conditional

9. Understanding Default Locale

MethodDetail
Locale.getDefault()JVM default
Locale.setDefault(loc)Set globally (caution)
CategoriesLocale.Category.DISPLAY / FORMAT

10. Supporting Multiple Languages

PracticeDetail
External stringsUse ResourceBundle (no hardcoded text)
Locale-aware compareCollator.getInstance(loc).compare(a, b)
Bidi textjava.text.Bidi for RTL languages
Unicode normalizationNormalizer.normalize(s, Form.NFC)