Article image Internationalization and Localization in Flutter: Multiple Language Support

10.8. Internationalization and Localization in Flutter: Multiple Language Support

Page 141 | Listen in audio

Internationalization and localization are critical aspects when developing an application in Flutter. That's because, to reach a global audience, it's vital that your app can be used in multiple languages ​​and date, time, and number formats. This is where internationalization and localization come into play. Internationalization (i18n) is the process of designing and preparing your application to be used in different languages ​​and regions. Localization (l10n), in turn, is the process of translating the internationalized application and adapting it to a specific region.

To start internationalizing your Flutter app, you need to add the `flutter_localizations` and `intl` dependencies in your `pubspec.yaml` file. The first is Flutter's localization library, while the second is a Dart library for internationalization and localization.

dependencies:
  flutter:
    sdk: flutter
  flutter_locations:
    sdk: flutter
  intl: ^0.17.0

After adding the dependencies, you need to import them into your main file and define the locales your application will support. This can be done in the `MaterialApp` or `CupertinoApp` widget.

import 'package:flutter_localizations/flutter_localizations.dart';

MaterialApp(
  localizationsDelegates: [
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
  ],
  supportedLocales: [
    const Locale('en', ''), // English
    const Locale('es', ''), // Spanish
    // Other locales...
  ],
);

The `localizationsDelegates` property lists the delegates that will provide locations for the application. The `GlobalMaterialLocalizations` and `GlobalWidgetsLocalizations` location delegates provide default locations for Material Design and Flutter widgets, respectively. `GlobalCupertinoLocalizations` provides localizations for iOS-style widgets.

The `supportedLocales` property defines the locales that the application supports. Each locale is represented by a `Locale` object, which has a language code (`en` for English, `es` for Spanish, etc.) and, optionally, a country code (to specify regional variants of the language).

Once you define the supported locales, you can use the `Localizations` widget to access the locations in your widgets. For example, to display a date formatted according to the current locale, you could do the following:

Text(
  DateFormat.yMMMd().format(DateTime.now()),
);

The `DateFormat` class comes from the `intl` library and provides methods to format dates according to the current locale.

To translate strings, you can define a custom locale delegate that provides a locale class. The localization class contains methods that return the translated strings. You can generate localization files from model files using the `intl_translation` tool.

In summary, internationalization and localization in Flutter are important processes to make your app accessible to a global audience. While it may seem complex at first, Flutter provides several tools and libraries to make i18n and l10n easier.

Now answer the exercise about the content:

What is the difference between internationalization and localization when developing a Flutter app?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Internationalization and localization in Flutter: Automatic device language detection

Next page of the Free Ebook:

142Internationalization and localization in Flutter: Automatic device language detection

3 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text