Lesson · Internationalization & Localization
Internationalization: text, numbers and dates for every locale
13 min readInternationalization & Localization
A locale bundles a language, a region and the formatting conventions that follow from them. Numbers, currency, dates, case folding and sorting all change with it, and hardcoding any of them quietly breaks for every user outside the default.
The default locale is a hidden machine setting
Code that formats 1234567.89 without naming a locale gets the JVM's default, which varies by machine and by user settings. Tests pass in CI, and the first user with a German or Indian locale sees a different output than anyone expected. The fix is the same principle as time zones: be explicit at every boundary where text meets numbers or dates.
Locale is not just a language. It is language plus region plus variant: English (US) writes 9/21/26 and $1,234 and strasse; English (UK) writes 21/09/2026; Turkish lowercases I to a dotless ı. Region picks the conventions, language picks the words.
String.format("%,.2f", 1234567.89); // default locale: different on every machine NumberFormat.getInstance(Locale.GERMANY).format(1234567.89); // explicit: 1.234.567,89
Numbers and currency
NumberFormat covers the three shapes that appear in every interface: plain numbers (thousand separators, decimal comma or point), currency (symbol, placement, and the currency's own digit count) and percent (scaling and symbol). Each takes a Locale and returns strings that follow that region's conventions.
Currency is the sharpest example of why this matters: the same method formats one value as $1,234,567.89 in the US and with swapped separators in Germany, and a Japanese locale rounds to whole yen because JPY has no minor unit. The formatting decisions are locale data, not code.
NumberFormat.getNumberInstance(Locale.US).format(1234567.89); // 1,234,567.89 NumberFormat.getNumberInstance(Locale.GERMANY).format(1234567.89); // 1.234.567,89 NumberFormat.getCurrencyInstance(Locale.US).format(1234.5); // $1,234.50
Dates follow the same rule
DateTimeFormatter.ofLocalizedDate styles decide how much of a date to show (short, medium, long, full) and the locale decides how: 9/21/26 in the US, 21.09.26 in Germany. Both dimensions are explicit, and neither reads the machine's defaults once you pass them.
The month-name formats are the visible payoff: Monday, September 21 versus Montag, 21. September, with the name, order, punctuation and even the capitalisation coming from locale data.
DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(Locale.GERMANY); // 21.09.26 DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(Locale.US); // 9/21/26
Text has locale rules too
toLowerCase and toUpperCase take a Locale because case mapping is language-specific: Turkish has four i variants, and the German sharp s uppercases to SS (one character to two). Code that folds case without a locale works for English and misbehaves for millions of users.
The rule of thumb: pass Locale.ROOT when comparing identifiers (protocol strings, property keys, enum names) so behavior never depends on the machine, and pass the user's locale when transforming text to display.
"I".toLowerCase(Locale.forLanguageTag("tr")); // ı: the dotless i "STRASSE".toLowerCase(Locale.US); // strasse identifier.equalsIgnoreCase(other); // fine: case-insensitive comparison of ASCII
Locale lens · one value, three conventions
switch the lensthe same values, formatted with this locale
collation: Ö sits beside O
case folding is language-specific
"I".toLowerCase(Locale.forLanguageTag("tr"))
becomes the dotless ı: Turkish has four i variants
collation is language-specific
German sort: [Apfel, Öl, Zebra]
Swedish sort: [Apfel, Zebra, Öl]: Å Ä Ö end the alphabet
machine text stays on ROOT
"TITLE".toLowerCase(Locale.ROOT)
protocol values and keys never follow user settings
Sorting is a locale decision, not an ordering
Alphabetical order is not universal. Collator uses the locale's rules: German sorts Ö next to O, Swedish puts Å Ä Ö at the end of the alphabet, and Danish treats aa as an Å. A plain String.compareTo sorts by code point, which is right for machines and wrong for users.
Use Collator.getInstance(locale) for display order, and keep code-point order (natural String order) for keys, protocol values and anything that must be stable across locales.
words.sort(Collator.getInstance(Locale.GERMAN)); // Ö beside O words.sort(Collator.getInstance(Locale.forLanguageTag("sv"))); // Ö after Z
The habit list
Internationalization is ninety percent habits and ten percent API. The habits:
- Never parse or format a number or date without a locale. The default is machine state, not a decision.
- Locale.ROOT for machine-facing text. Parsing identifiers, writing protocol values, comparing keys: locale-independent by contract.
- The user's locale for human-facing text. Display formats, month names, currency, sorted lists.
- Store raw values, format at the edge. Keep BigDecimal or long cents in the model, produce strings only for output.
- Text is more than code points. Grapheme clusters (emoji, combining accents) need break iterators or ICU for length and slicing.
Two questions pick the right Locale every time: who reads this string (their locale), or does no human read it (Locale.ROOT).
A locale is an input like any other
Treating locale as ambient machine state is the same bug as treating the time zone that way. Make it an explicit parameter at the boundaries where formatting happens, and both categories of bug disappear together.
Interview angles
What exactly does a Locale bundle?+
Language, region and variant, which together select conventions: number and date formats, currency rules, case mapping, collation order and month/day names. It is data driving behavior, not just a label.
Default locale versus Locale.ROOT?+
The default comes from the JVM, OS and user settings: non-deterministic across machines. Locale.ROOT is the invariant baseline (English-like, machine-stable). Use ROOT for machine-facing text, the user's locale for display.
Why does currency formatting need a locale?+
Symbol, symbol position, separators and the number of fraction digits are locale and currency data: JPY has no minor unit, Germany swaps the separators. The same amount formats differently and both are correct for their audience.
Why must toLowerCase/toUpperCase take a Locale?+
Case mapping is language-specific: Turkish I lowercases to the dotless ı, German ß uppercases to SS. A no-arg call uses the default locale, so behavior changes with the machine. Pass the user's locale, or ROOT for identifiers.
Why is String.compareTo usually wrong for user-visible sorting?+
It orders by UTF-16 code points, which puts accented letters and case in machine order. Collator implements the locale's collation: Ö beside O in German, Å Ä Ö after Z in Swedish, case-insensitive variants available.