The Power of Internationalization and Localization in Flutter: Reaching Global Audiences

Murtaza Sulaihi
4 min readJun 20, 2023

Introduction

Developing applications that can cater to a global audience is crucial in today's interconnected world. Flutter, a versatile cross-platform framework, offers robust internationalization (i18n) and localization (l10n) capabilities. By implementing i18n and l10n in Flutter apps, developers can effortlessly tailor their user interfaces to support multiple languages and cultural preferences, enabling them to reach a broader global user base.

Understanding Internationalization and Localization.

Internationalization, often referred to as i18n, is the process of designing and developing an application to make it easily adaptable to different languages, regions, and cultures. On the other hand, localization, known as l10n, involves adapting an application to a specific language or region, encompassing elements like date and time formats, number formatting, currency symbols, and text translations.

Flutter’s Internationalization Support.

Flutter provides robust tools and libraries that simplify the internationalization process. The core of Flutter’s i18n support is the `intl` package, which offers utilities for formatting dates, times, currencies, and messages in a locale-specific manner. Developers can leverage this package to handle language-specific text translations as well. Flutter also provides the `flutter_localizations` package, which offers pre-defined localization resources for a wide range of languages, saving developers significant time and effort.

Implementing Internationalization and Localization in Flutter.

To enable i18n and l10n in a Flutter app, developers need to follow a few key steps. First, they should define localized strings using the `intl` package or external localization files. These strings can be easily translated and localized for different languages. Next, developers can use Flutter’s `Localizations` widget to load the correct localization resources based on the user’s preferred locale. They can also use `intl` methods for date and number formatting, ensuring consistency across different regions. Additionally, Flutter’s `MaterialApp` and `CupertinoApp` widgets automatically handle some localization settings, such as language direction and text direction.

Benefits and Challenges of Internationalization and Localization in Flutter.

By incorporating internationalization and localization in Flutter, developers unlock numerous benefits. They can expand their user base by providing localized experiences, allowing users to engage with the app in their preferred language. Localization also enhances user experience and engagement, as users feel more comfortable interacting with content tailored to their cultural norms. However, implementing i18n and l10n can present challenges, such as managing translation files, maintaining language-specific UI layouts, and ensuring efficient testing for each supported language.

A step-by-step guide on implementing internationalization and localization in Flutter:

Step 1: Prepare Your Flutter Project Ensure that you have a Flutter project set up and running. Open your project in your preferred IDE or text editor.

Step 2: Add Dependencies In your project’s pubspec.yaml file, add the following dependencies:

dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl: ^0.18.1

Save the file and run flutter pub get to fetch the dependencies.

Step 3: Define Localized Strings Create a new directory in your project’s root directory called l10n or i18n. Inside this directory, create a file named app_localizations.dart. This file will contain the localized strings for your app.

In app_localizations.dart, define a class called AppLocalizations that extends LocalizationsDelegate. This class will handle loading the appropriate localization resources.

Add the following code to app_localizations.dart:

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class AppLocalizations {
static Future<AppLocalizations> load(Locale locale) {
final String name =
locale.countryCode.isEmpty ? locale.languageCode : locale.toString();
final String localeName = Intl.canonicalizedLocale(name);

return initializeMessages(localeName).then((_) {
Intl.defaultLocale = localeName;
return AppLocalizations();
});
}

static AppLocalizations of(BuildContext context) {
return Localizations.of<AppLocalizations>(context, AppLocalizations);
}

// Define your localized strings as static methods here
String get helloWorld {
return Intl.message(
'Hello, World!',
name: 'helloWorld',
desc: 'The conventional newborn programmer greeting',
);
}

// Add more localized strings as needed

// ...

// Add support for more languages by adding their respective message files
static const LocalizationsDelegate<AppLocalizations> delegate =
_AppLocalizationsDelegate();
}

class _AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
const _AppLocalizationsDelegate();

@override
bool isSupported(Locale locale) {
return ['en', 'es'].contains(locale.languageCode);
}

@override
Future<AppLocalizations> load(Locale locale) {
return AppLocalizations.load(locale);
}

@override
bool shouldReload(_AppLocalizationsDelegate old) => false;
}

Step 4: Provide Localization Support Open your app’s entry point file (usually main.dart) and make the following changes:

Import the necessary packages:

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import './l10n/app_localizations.dart';

Wrap your MaterialApp or CupertinoApp widget with MaterialApp and Localizations widgets:

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale('en', ''), // English
const Locale('es', ''), // Spanish
// Add more locales as needed
],
// ...
);
}
}

Step 5: Implement Localization In your app’s UI, access the localized strings defined in AppLocalizations by using the AppLocalizations.of(context) method. For example:

@override
Widget build(BuildContext context) {
final localizations = AppLocalizations.of(context);

return Scaffold(
appBar: AppBar(
title: Text(localizations.helloWorld),
),
// ...
);
}

Step 6: Generate Localization Files (Optional) If you prefer using external localization files, you can use the intl package's tools to generate language-specific .arb (Application Resource Bundle) files.

Run the following command in your project’s root directory:

flutter pub pub run intl_translation:extract_to_arb --output-dir=l10n lib/localization.dart

This command generates a .arb file for each supported language. You can then provide translations for these files and use the intl package to load and use them.

That’s it! You’ve successfully implemented internationalization and localization in your Flutter app. Remember to add more localized strings and provide translations for each language as needed.

Conclusion

With its comprehensive internationalization and localization support, Flutter empowers developers to create apps that resonate with global audiences. By embracing i18n and l10n, developers can break language barriers and offer immersive experiences to users worldwide, thereby maximizing the reach and impact of their applications.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Murtaza Sulaihi
Murtaza Sulaihi

Written by Murtaza Sulaihi

By profession, I am a school professor and I also develop Android applications and also Flutter applications.

No responses yet

Write a response