Sub topic 10
Formatting dates and numbers per locale
This chapter assumes you have a working gen-l10n setup: an l10n.yaml, a template app_en.arb, a translated app_es.arb, and AppLocalizations wired into MaterialApp. You should also know how typed placeholders work in ARB files.
Why formatting belongs to the locale
The same date and the same number are written differently around the world. March 14, 2026 is Mar 14, 2026 in US English and 14 mar 2026 in Spanish. The number 1234567.891 is 1,234,567.891 in English and 1.234.567,891 in Spanish and German. If you build these strings yourself with toString() or string interpolation, they will be wrong for most of your users.
The intl package contains the formatting rules in its DateFormat and NumberFormat classes. gen-l10n can call them for you. You declare the format in the ARB metadata, and the generated code applies it using the locale of the translation being used. You write the format once in the template file, and each locale formats the value its own way.
Date placeholders
A placeholder with "type": "DateTime" must have a format. The format is the name of a DateFormat named constructor, such as yMd, yMMMd, yMMMMd, jm, or yMMMMEEEEd. These names are ICU skeletons. A skeleton lists which fields to show, and the locale decides their order, separators, and month names.
The departure message from the earlier chapters already does this:
{
"@@locale": "en",
"departure": "Departs on {date}",
"@departure": {
"description": "Departure date of a trip",
"placeholders": {
"date": {
"type": "DateTime",
"format": "yMMMd"
}
}
}
}The generated method is String departure(DateTime date). Inside each locale class, the generated code runs intl.DateFormat.yMMMd(localeName).format(date). Here localeName is the locale of that class, such as es. Your Dart code only passes a DateTime.
Skeletons and what they produce
For DateTime(2026, 3, 14):
| Skeleton | `en` | `es` | `de` |
|---|---|---|---|
| `yMd` | 3/14/2026 | 14/3/2026 | 14.3.2026 |
| `yMMMd` | Mar 14, 2026 | 14 mar 2026 | 14. März 2026 |
| `yMMMMd` | March 14, 2026 | 14 de marzo de 2026 | 14. März 2026 |
The translator does not touch any of this. The Spanish file has "Sale el {date}", and the date inside it follows Spanish rules.
Custom patterns and why to avoid them
If no skeleton fits, you can give a literal pattern and set isCustomDateFormat to "true":
"@launchNote": {
"placeholders": {
"date": {
"type": "DateTime",
"format": "d 'of' MMMM",
"isCustomDateFormat": "true"
}
}
}A custom pattern is fixed text. The order of day and month, and the word "of", are hard-coded for every locale. Only the month name changes. Use a skeleton unless the design requires an exact layout.
Number placeholders
A placeholder of type int, double, or num may have a format. The value is the name of a NumberFormat constructor. The supported names are:
compact,compactLong,compactCurrency,compactSimpleCurrencycurrency,simpleCurrencydecimalPattern,decimalPercentPattern,percentPatternscientificPattern
Some of these accept optionalParameters, which become named arguments of the NumberFormat constructor. The common ones are decimalDigits, name (an ISO currency code), symbol, and customPattern.
This template file shows several formats together:
{
"@@locale": "en",
"views": "{count} views",
"@views": {
"description": "How many times a trip page was opened, shown in short form",
"placeholders": {
"count": {
"type": "int",
"format": "compact"
}
}
},
"distance": "Distance: {km} km",
"@distance": {
"description": "Trip distance in kilometers",
"placeholders": {
"km": {
"type": "double",
"format": "decimalPattern"
}
}
},
"price": "Total: {amount}",
"@price": {
"description": "Trip price, always in US dollars",
"placeholders": {
"amount": {
"type": "double",
"format": "simpleCurrency",
"optionalParameters": {
"name": "USD",
"decimalDigits": 2
}
}
}
}
}Each key still needs a matching entry in the other ARB files, for example "views": "{count} visitas". The metadata is read only from the template file.
Values in a sample screen
This widget shows all three messages. It assumes the earlier MaterialApp wiring.
import 'package:flutter/material.dart';
import 'l10n/app_localizations.dart';
class SummaryScreen extends StatelessWidget {
const SummaryScreen({super.key});
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
return Scaffold(
body: Column(
children: [
Text(l10n.views(1234567)),
Text(l10n.distance(1234567.891)),
Text(l10n.price(1499.5)),
],
),
);
}
}Approximate output:
| Message | `en` | `es` | `de` |
|---|---|---|---|
| `views(1234567)` | 1.2M views | 1,2 M visitas | 1,2 Mio. Aufrufe |
| `distance(1234567.891)` | 1,234,567.891 | 1.234.567,891 | 1.234.567,891 |
| `price(1499.5)` | $1,499.50 | 1499,50 US$ | 1.499,50 $ |
(For es and de, the price row assumes the ARB files provide translations of the price message. The views and distance rows likewise assume translations of those messages.)
Exact spacing and symbols come from the CLDR data bundled with intl. The space before a currency symbol or a unit is often a non-breaking space. Treat the table as a guide and let the tests you write assert whatever your intl version produces.
Choosing the currency
currency and simpleCurrency use the locale's default currency unless you pass name. The default for es is the euro. A price of 1499.5 US dollars would be shown as euros without any conversion, which is a serious bug. Set name whenever the amount has a fixed currency. The locale controls how the number is written. It does not change the currency.
Formatting numbers inside plurals
A numeric placeholder used inside a plural expression can also have a format. The plural rule picks the branch from the raw number, and the printed {count} uses the format:
"viewsPlural": "{count, plural, =0{No views} =1{1 view} other{{count} views}}",
"@viewsPlural": {
"placeholders": {
"count": { "type": "int", "format": "compact" }
}
}With a count of 1500, the English text is 1.5K views. The plural rule still selects other from the raw number 1500. Use this when the number can be large. A format of decimalPattern also adds thousands separators.
How the pieces connect
Formatting outside ARB messages
Some values do not belong in a sentence, for example a table cell that holds only a date. You can call intl directly. Use the locale from the widget tree so the result matches the rest of the UI:
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class DateCell extends StatelessWidget {
const DateCell({super.key, required this.date});
final DateTime date;
@override
Widget build(BuildContext context) {
final localeName = Localizations.localeOf(context).toString();
return Text(DateFormat.yMMMd(localeName).format(date));
}
}Date symbols for a locale must be loaded before DateFormat can use them. The Material delegates in AppLocalizations.localizationsDelegates do this when the locale loads, so widgets under MaterialApp are covered. In plain Dart tests with no delegates, call initializeDateFormatting('es') from package:intl/date_symbol_data_local.dart first.
Edge cases
- Time zones.
DateFormatformats the fields of theDateTimeyou pass. It does not convert the zone. A UTC value near midnight can show the wrong calendar day. CalltoLocal(), or convert to the destination's zone, before you format. - Missing
formaton a date. gen-l10n rejects aDateTimeplaceholder without aformat. A placeholder of typeObjectorStringis never formatted, so passing aDateTimethere prints itstoString()result. - Wrong format name. The name must match a
DateFormatorNumberFormatconstructor. A typo fails at generation time, not at runtime. - Locales without full data. A language with no formatting data in
intlfalls back to a default, which usually means English patterns. Check unusual locales on a device. - Digits. Some locales, such as Arabic in certain regions, use different digit characters. Formatting through
intlhandles this. Manual string building does not.
Testing formatted output
Force a locale in a widget test, as in the testing chapter, and assert on text. The test below checks the Spanish date and the German number grouping:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:your_app/l10n/app_localizations.dart';
void main() {
testWidgets('formats the date per locale', (tester) async {
await tester.pumpWidget(
MaterialApp(
locale: const Locale('es'),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Builder(
builder: (context) => Text(
AppLocalizations.of(context)!.departure(DateTime(2026, 3, 14)),
),
),
),
);
await tester.pumpAndSettle();
expect(find.text('Sale el 14 mar 2026'), findsOneWidget);
});
}Replace your_app with the name in your pubspec.yaml. If a currency or compact test fails on whitespace, print the string and check for non-breaking spaces. Compare against '1,2\u00A0M' rather than editing the expected text until it looks right.
Exercise
A trip costs 1499.5 US dollars. You add a price message with a double placeholder using "format": "currency" and no optionalParameters. A user with the device language set to Spanish (Spain) sees the price in euros. What went wrong, and what change to the placeholder fixes it?
Show answer
The currency format uses the default currency of the active locale, which is the euro for Spanish (Spain). The locale only controls how the number is written, so the amount is shown with the wrong currency. Fix it by naming the currency in the metadata: use "format": "simpleCurrency" (or currency) with "optionalParameters": { "name": "USD", "decimalDigits": 2 }.
Exercise
Why is "format": "d 'of' MMMM" with "isCustomDateFormat": "true" usually a poor choice for a message that is translated into many languages, and what should you use instead?
Show answer
A custom pattern is literal text, so every locale gets the same field order and the same English word "of". Only the month name is localized. A skeleton such as yMMMMd lets intl choose the order, separators, and connecting words for each locale, so use a skeleton unless the design needs an exact layout.