Custom locales
Register a locale under any code to add a language or replace the strings of a built-in one. A registered locale is used for pattern, relative, calendar and duration formatting.
Extend a built-in locale
To change only a few strings, extend the built-in class and override what you need.
class MyVietnamese extends ViLocale {
String ordinalNumber(int n) => 'ngày thứ $n';
}
SupportedLocalesUtils.registerLocale('vi', MyVietnamese());
Add a new locale
Extend DateFormatterLocale. Calendar and duration strings are optional; without them those APIs use English.
class MyLocale extends DateFormatterLocale {
String code() => 'xx';
String ordinal(int n) => '.';
String ordinalNumber(int n) => '$n.';
RelativeDateTime relativeDateTime() => MyRelativeTime();
RelativeDateTime shortRelativeDateTime() => MyShortRelativeTime();
CalendarDateTime calendarDateTime() => MyCalendar(); // optional
DurationUnits durationUnits() => MyDurationUnits(); // optional
}
SupportedLocalesUtils.registerLocale('xx', MyLocale());
Interfaces
| Interface | Strings |
|---|---|
RelativeDateTime | prefixes, suffixes and units of relative times ("5 minutes ago") |
DirectionalRelativeDateTime | optional: different unit forms for past and future (Czech, Slovak) |
CalendarDateTime | today, tomorrow, yesterday, next/last weekday; receives isSameWeek |
DurationUnits | standalone unit forms for humanized durations |
Flutter users
The base class used to be called Locale, which clashed with Flutter's Locale. It is now DateFormatterLocale; the old name remains as a deprecated typedef.