Intl class
The Intl class provides a common entry point for internationalization
related tasks. An Intl instance can be created for a particular locale
and used to create a date format via anIntl.date()
. Static methods
on this class are also used in message formatting.
Examples:
String today(DateTime date) => Intl.message(
"Today's date is $date",
name: 'today',
args: [date],
desc: 'Indicate the current date',
examples: const {'date': 'June 8, 2012'},
);
print(today(DateTime.now().toString());
String howManyPeople(int numberOfPeople, String place) => Intl.plural(
numberOfPeople,
zero: 'I see no one at all in $place.',
one: 'I see $numberOfPeople other person in $place.',
other: 'I see $numberOfPeople other people in $place.',
name: 'howManyPeople',
args: [numberOfPeople, place],
desc: 'Description of how many people are seen in a place.',
examples: const {'numberOfPeople': 3, 'place': 'London'},
);
Calling howManyPeople(2, 'Athens');
would
produce "I see 2 other people in Athens." as output in the default locale.
If run in a different locale it would produce appropriately translated
output.
You can set the default locale.
Intl.defaultLocale = 'pt_BR';
Constructors
Properties
Methods
-
date(
[String? pattern, String? desiredLocale]) → DateFormat -
Return a new date format using the specified
pattern
. IfdesiredLocale
is not specified, then we default to locale. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- defaultLocale ↔ String?
-
The default locale. This defaults to being set from systemLocale, but
can also be set explicitly, and will then apply to any new instances where
the locale isn't specified. Note that a locale parameter to
Intl.withLocale
will supercede this value while that operation is active. Using
Intl.withLocale may be preferable if you are using different locales
in the same application.
getter/setter pair
- systemLocale ↔ String
-
The system's locale, as obtained from the window.navigator.language
or other operating system mechanism. Note that due to system limitations
this is not automatically set, and must be set by importing one of
intl_browser.dart or intl_standalone.dart and calling findSystemLocale().
getter/setter pair
Static Methods
-
canonicalizedLocale(
String? aLocale) → String -
Return the name
aLocale
turned into xx_YY where it might possibly be in the wrong case or with a hyphen instead of an underscore. IfaLocale
is null, for example, if you tried to get it from IE, return the current system locale. -
gender(
String targetGender, {String? female, String? male, required String other, String? desc, Map< String, Object> ? examples, String? locale, String? name, List<Object> ? args, String? meaning, bool? skip}) → String -
Format a message differently depending on
targetGender
. -
genderLogic<
T> (String targetGender, {T? female, T? male, required T other, String? locale}) → T - Internal: Implements the logic for gender selection - use gender for normal messages.
-
getCurrentLocale(
) → String - Accessor for the current locale. This should always == the default locale, unless for some reason this gets called inside a message that resets the locale.
-
message(
String messageText, {String? desc = '', Map< String, Object> ? examples, String? locale, String? name, List<Object> ? args, String? meaning, bool? skip}) → String - Use this for a message that will be translated for different locales. The expected usage is that this is inside an enclosing function that only returns the value of this call and provides a scope for the variables that will be substituted in the message.
-
plural(
num howMany, {String? zero, String? one, String? two, String? few, String? many, required String other, String? desc, Map< String, Object> ? examples, String? locale, int? precision, String? name, List<Object> ? args, String? meaning, bool? skip}) → String -
Formats a message differently depending on
howMany
. -
pluralLogic<
T> (num howMany, {T? zero, T? one, T? two, T? few, T? many, required T other, String? locale, int? precision, String? meaning, bool useExplicitNumberCases = true}) → T - Internal: Implements the logic for plural selection - use plural for normal messages.
-
select(
Object choice, Map< Object, String> cases, {String? desc, Map<String, Object> ? examples, String? locale, String? name, List<Object> ? args, String? meaning, bool? skip}) → String -
Format a message differently depending on
choice
. -
selectLogic<
T> (Object choice, Map< Object, T> cases) → T - Internal: Implements the logic for select - use select for normal messages.
-
shortLocale(
String aLocale) → String - Return the short version of a locale name, e.g. 'en_US' => 'en'
-
verifiedLocale(
String? newLocale, bool localeExists(String), {String? onFailure(String)?}) → String? -
Given
newLocale
return a locale that we have data for that is similar to it, if possible. -
withLocale<
T> (String? locale, T function()) → dynamic -
Run
function
with the default locale set tolocale
and return the result.