Day 22: Internationalization
Harpal Matholiya
Mobile App Developer | Flutter Developer | 3+ Years Experience | Worked on 50+ Projects | 4.7/5 Rating | Building Scalable, User-Centric Apps | Quality-Driven | Innovation-Focused | Committed to Excellence
Topic: Adding Multiple Languages
Creating an app that supports multiple languages (internationalization) is crucial for reaching a broader audience and enhancing user experience. Flutter provides excellent support for internationalization through the intl package.
Why Internationalize Your App?
Steps to Internationalize Your Flutter App
Step 1: Add Dependencies
Add the flutter_localizations and intl packages to your pubspec.yaml file:
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl: ^0.17.0
Run flutter pub get to install the packages.
Step 2: Set Up Localization Files
Create a l10n directory in the root of your project. Inside this directory, create localization files for each language you want to support (e.g., app_en.arb for English, app_es.arb for Spanish).
app_en.arb:
{
"title": "Hello World",
"message": "Welcome to our Flutter application."
}
app_es.arb:
领英推荐
{
"title": "Hola Mundo",
"message": "Bienvenido a nuestra aplicación Flutter."
}
Step 3: Configure Localizations in MaterialApp
Update your MaterialApp to support localization.
main.dart:
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:intl/intl.dart';
import 'generated/l10n.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale('en', ''), // English
const Locale('es', ''), // Spanish
],
home: MyHomePage(),
);
}
}
Step 4: Use Translations in Your Widgets
Use the localized strings in your widgets.
my_home_page.dart:
import 'package:flutter/material.dart';
import 'generated/l10n.dart';
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(AppLocalizations.of(context).title),
),
body: Center(
child: Text(AppLocalizations.of(context).message),
),
);
}
}
Generating Localization Files
Run the following command to generate localization files:
flutter pub run intl_translation:generate_from_arb --output-dir=lib/l10n lib/main.dart lib/l10n/*.arb
Conclusion
Internationalizing your Flutter app allows you to reach a global audience and improve the user experience by providing content in their preferred languages. Follow these steps to add multiple language support to your app seamlessly.
Ready to take your Flutter app to the next level? Join our 30-day development course for just ?5000/month. Learn from experts and build real-world apps. Enroll now! #Flutter #AppDevelopment #Internationalization #LearnToCode