Free Ebook cover How to create apps from scratch to advanced using Flutter and Dart complete course

How to create apps from scratch to advanced using Flutter and Dart complete course

5

(4)

267 pages

Internationalization and Localization in Flutter: Multiple Language Support

Capítulo 141

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

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).

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

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.

Internationalization (i18n) involves designing and preparing the app for use in different languages and regions. On the other hand, localization (l10n) is about translating and adapting the internationalized app for specific regions, ensuring proper cultural adaptation such as date and time formats.

Next chapter

Internationalization and localization in Flutter: Automatic device language detection

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.