I18n (Internationalization) in Dart involves the process of adapting or modifying your application to support different languages and cultures. This ensures that your application can be used by people from different regions, who speak different languages and have different cultural backgrounds.
Dart provides the intl package, which is a powerful library for formatting and parsing dates, times, numbers, currencies and messages in various locales. It allows you to define messages in different languages and switch language dynamically at runtime.
Here is a step-by-step guide to implement i18n in your Dart application:
- Install the intl package by adding it to your pubspec.yaml file:
dependencies:
intl: ^0.17.0
- Create a directory named "l10n" (short for localization) at the root level of your project. Inside this directory, create a file named "intl_messages.arb". This file will contain all your messages in their default language.
{
"@@locale": "en", // default locale
"hello": "Hello, world!",
"greeting": "Welcome {name}!",
"app_name": "My Awesome App",
"title": "{appName} - Home",
...
}
In the above example, hello, greeting, app_name, title are message keys and their corresponding values are the default messages in English.
- Add more files to the "l10n" directory for other languages you wish to support. The file names should be in the format
intl_messages_<locale>.arb, where locale is the ISO language code for the language. For example, for French, the file name would be intl_messages_fr.arb.
{
"@@locale": "fr", // French locale
"hello": "Bonjour, le monde!",
"greeting": "Bienvenue {name}!",
"app_name": "Mon application géniale",
"title": "{appName} - Accueil",
...
}
In the above example, hello and greeting messages are defined in French.
- Generate Dart code from the ARB files by running this command in your terminal:
flutter pub run intl_translation:generate_from_arb --output-dir=lib/l10n lib/l10n/intl_messages.arb lib/l10n/intl_messages_*.arb
This will generate a messages.dart file in the lib/l10n directory, which contains code that maps the message keys to their translated messages.
- Use the
Intl class and its methods to format messages, numbers, dates, and currencies based on the user's locale. For example, to format a date in the user's locale, you can use:
import 'package:intl/intl.dart';
...
Text(DateFormat.yMd().format(DateTime.now()))
- To switch language at runtime, use the
Intl.defaultLocale property to set the user's preferred locale. For example, to switch to French:
import 'package:intl/intl.dart';
...
Intl.defaultLocale = 'fr';
- Wrap your app with an
Intl widget to rebuild the UI when the locale changes:
return MaterialApp(
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: AppLocalizations.supportedLocales,
home: MyHomePage(),
);
In the above example, AppLocalizations is a class generated by the intl package that provides access to the translated messages.
That's it! Your app now supports internationalization and can be used by people from different cultures and languages.