Sub topic 10
Formatting Dates and Numbers per Locale
Assume you understand basic Dart, ARB files, and how MaterialApp loads generated AppLocalizations. This chapter focuses on formatting values so that the same data appears naturally in each language and region.
Why locale-aware formatting matters
A date such as 2026-03-14 can appear as Mar 14, 2026 in English or 14 mar 2026 in Spanish. A number such as 12345.67 can appear as 12,345.67 or 12.345,67. These differences are part of localization.
Keep values as DateTime, int, or double in your Dart code. Let the generated localization class format them when it creates display text.
flowchart LR
A[DateTime or number in Dart] --> B[Typed ARB placeholder]
B --> C[Generated AppLocalizations method]
C --> D[Locale-aware formatted text]Formatting a date in ARB
Declare a DateTime placeholder and give it an ICU date format. The format is read by gen-l10n, which generates a typed method.
lib/l10n/app_en.arb:
{
"@@locale": "en",
"departure": "Departs on {date}",
"@departure": {
"description": "Departure date shown on the trip details screen",
"placeholders": {
"date": {
"type": "DateTime",
"format": "yMMMd"
}
}
}
}A translated file keeps the same placeholder name:
{
"@@locale": "es",
"departure": "Sale el {date}"
}The generated method accepts a DateTime:
final l10n = AppLocalizations.of(context)!;
Text(l10n.departure(DateTime(2026, 3, 14)));The displayed date uses the app's active locale. The translated sentence and the date pattern are handled separately, so translators can place the placeholder where their language requires it.
Formatting numbers and currency
Number placeholders can use formats defined by the intl package. For example, compact turns 1200 into a shorter form such as 1.2K, while currency formats a monetary value.
{
"@@locale": "en",
"distance": "{kilometers} km",
"@distance": {
"description": "Distance for a trip",
"placeholders": {
"kilometers": {
"type": "int",
"format": "decimalPattern"
}
}
},
"price": "Price: {amount}",
"@price": {
"description": "Trip price",
"placeholders": {
"amount": {
"type": "double",
"format": "currency"
}
}
}
}Use the generated methods with the declared Dart types:
Text(l10n.distance(12345));
Text(l10n.price(49.95));The exact currency symbol and conventions depend on the locale and the format configuration. If your product must use one fixed currency, define that policy explicitly instead of assuming the user's language determines the currency.
| Value | Dart type | Typical locale-aware result |
|---|---|---|
| `12345` | `int` | `12,345` or `12.345` |
| `49.95` | `double` | Currency with local separators and symbol |
| `DateTime(2026, 3, 14)` | `DateTime` | A localized calendar date |
Common errors
- Passing a
Stringto aDateTimeplaceholder causes a type error. - Using a translated date pattern inside every message duplicates formatting logic.
- Treating a
DateTimeas local when it represents UTC can show the wrong calendar day. Convert it to the intended time zone before formatting. - Assuming a decimal point or comma has the same meaning in every locale.
Exercise
Why should a trip date remain a DateTime until the localization method formats it?
Show answer
The localization method needs the DateTime value and the active locale to choose the correct date order, month name, and separators.