intl
intl
▲▼▲▼▲▼
tags : #coding #flutter #package
references : Date and Time Packages
▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼
▲▼▲▼▲▼
The intl package in Flutter (and Dart in general) is a library that provides internationalization
and localization support, enabling you to work with dates, numbers, and messages in a way
that is appropriate for different regions and languages. The package is essential for building
apps that cater to a global audience.
1. Basic Setup
To use the intl package in your Flutter project, you need to include it in your pubspec.yaml :
dependencies:
intl: ^0.18.0 # or the latest version available
After adding it, run flutter pub get to fetch the package.
import 'package:intl/intl.dart';
void main() {
DateTime now = DateTime.now();
String formattedDate = DateFormat('EEEE, MMMM d, yyyy').format(now);
print(formattedDate); // Output: "Sunday, January 6, 2025"
}
You can use DateFormat to display dates in various ways based on your requirements.
You can specify a locale (language and region) when formatting. For example, to display the
date in French:
import 'package:intl/intl.dart';
void main() {
DateTime now = DateTime.now();
String formattedDate = DateFormat('EEEE, MMMM d, yyyy',
'fr_FR').format(now);
print(formattedDate); // Output: "dimanche, janvier 6, 2025"
}
You can also use the current locale of the system automatically:
4. Number Formatting
The intl package also includes the ability to format numbers, currencies, and percentages in
a localized manner using NumberFormat .
void main() {
int number = 1234567;
NumberFormat formatter = NumberFormat('#,###');
String formattedNumber = formatter.format(number);
print(formattedNumber); // Output: "1,234,567"
}
Currency Formatting:
To format a number as currency:
import 'package:intl/intl.dart';
void main() {
double currency = 1234.56;
NumberFormat currencyFormatter = NumberFormat.simpleCurrency(locale:
'en_US');
String formattedCurrency = currencyFormatter.format(currency);
print(formattedCurrency); // Output: "$1,234.56"
}
Percentage Formatting:
import 'package:intl/intl.dart';
void main() {
double percentage = 0.75;
NumberFormat percentageFormatter = NumberFormat.percentPattern();
String formattedPercentage = percentageFormatter.format(percentage);
print(formattedPercentage); // Output: "75%"
}
5. Message Localization
The intl package also includes a feature for localizing messages in your app. The idea is to
use the Intl.message() method to mark strings for translation, and then use the intl
package tools to generate the necessary code for localization.
class MyAppLocalizations {
static String get hello => Intl.message('Hello', name: 'hello', desc:
'Greeting message');
}
You would then use a tool to generate the .arb files for different languages (such as
intl_utils ), which contain the localized strings.
For advanced time zone handling, consider using the timezone package in conjunction with
intl .
Example of Pluralization:
import 'package:intl/intl.dart';
void main() {
int itemCount = 5;
String message = Intl.plural(itemCount,
one: '1 item',
other: '$itemCount items',
);
print(message); // Output: "5 items"
}
This will adjust the message based on the number, ensuring proper grammar based on the
count.
8. Date Parsing
You can also parse a string into a DateTime object using DateFormat .
import 'package:intl/intl.dart';
void main() {
String dateStr = 'January 6, 2025';
DateTime parsedDate = DateFormat('MMMM d, yyyy').parse(dateStr);
print(parsedDate); // Output: "2025-01-06 00:00:00.000"
}
Timezone handling: intl doesn’t handle time zones directly, so for advanced use cases,
the timezone package might be needed.
Custom locale formats: You may need to create your own custom formatting logic if you
have very specific requirements for a locale or format.
Conclusion
The intl package is an essential tool for any internationalized or localized Flutter app. It
allows you to:
By leveraging intl , you can easily create Flutter apps that cater to a global audience with
proper localization and formatting support.