0% found this document useful (0 votes)
8 views6 pages

intl

Uploaded by

ismailovich1904
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

intl

Uploaded by

ismailovich1904
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼

▲▼▲▼▲▼
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.

Here’s everything you should know about the intl package:

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.

2. Date and Time Formatting


The intl package provides functionality to format and parse dates and times using the
DateFormat class.

Common DateFormat patterns:

d : Day of the month (1–31)


M : Month of the year (1–12)
y : Year (e.g., "2025")
H : Hour (24-hour clock)
m : Minute
s : Second
a : AM/PM
EEEE : Full day name (e.g., "Monday")
MMM : Abbreviated month name (e.g., "Jan")
MMMM : Full month name (e.g., "January")
Example of Date Formatting:

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.

3. Locale and Localization


The intl package helps you format numbers, dates, and times in a way that respects the
user’s locale (language and country).

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:

String formattedDate = DateFormat.yMMMMd().format(now);

This will format the date according to the device’s locale.

4. Number Formatting
The intl package also includes the ability to format numbers, currencies, and percentages in
a localized manner using NumberFormat .

Example of Number Formatting:


import 'package:intl/intl.dart';

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:

You can format numbers as percentages as well:

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.

Example of Message Localization:


import 'package:intl/intl.dart';

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.

6. Working with Time Zones


The intl package does not directly support time zone conversion, but you can work with time
zones in conjunction with the intl package. Use the DateTime class to work with UTC time
and local time.

DateTime utcTime = DateTime.utc(2025, 1, 6, 12, 0); // UTC Time


DateTime localTime = utcTime.toLocal(); // Convert to local time

For advanced time zone handling, consider using the timezone package in conjunction with
intl .

7. Plurals and Gendered Messages


The intl package allows you to format plural forms and gendered messages.

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 .

Example of Date Parsing:

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"
}

9. Fallbacks and Default Locale


If you don't explicitly set a locale when formatting or parsing, the system will default to the
device’s locale.

String formattedDate = DateFormat.yMMMMd().format(DateTime.now()); // Locale


is default (device's locale)

10. Handling Edge Cases with intl


The intl package is very versatile, but when working with complex internationalization, be
aware of:

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:

Format and parse dates and times according to different locales.


Format numbers, currencies, and percentages.
Localize static strings and messages.
Support pluralization and gendered messages.

By leveraging intl , you can easily create Flutter apps that cater to a global audience with
proper localization and formatting support.

You might also like