Application Localization in Flutter
Last Updated :
08 Jul, 2024
In mobile applications, Localization is the process of adapting an application to support multiple languages and regional settings. This allows your app to be more accessible to users from different parts of the world. In Flutter, for implementing Localization flutter_localization is a package used for in-app localization. It allows developers to create a single codebase that can display content in various languages, making the app accessible to a global audience. This is crucial for reaching users worldwide and providing a native experience regardless of their language preferences.
In this article, we'll explore how to implement localization in Flutter, making your app accessible to a global audience.
Prerequisites
- Flutter SDK installed (version 2.0 or later)
- Basic knowledge of Dart and Flutter development
- A code editor (e.g., Visual Studio Code, Android Studio)
How to Implement App Localization in Flutter?
Here's a complete step-by-step guide on how to implement app localization in Flutter with an example project, changing languages from English to Spanish.
Step 1: Create a New Flutter Project
Create a new project with your project_name, In our case here it is geeksforgeeks_localization.
flutter create geeksforgeeks_localization
cd geeksforgeeks_localization
Step 2: Add Dependencies
Open the pubsec.yaml file and add the following dependencies required for the project.
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl: ^0.19.0
flutter:
generate: true
Run flutter pub get to fetch the dependencies.
Step 3: Creating Localization Files
Now, let's create the localization files for English and Spanish.
- Create a new directory lib/l10n.
- In this directory, create two files: app_en.arb and app_es.arb.
1. app_en.arb (English):
{
"appTitle": "GeeksforGeeks Localization",
"welcomeMessage": "Welcome to GeeksforGeeks!",
"languageSelection": "Select a language:",
"englishOption": "English",
"spanishOption": "Spanish"
}
2. app_es.arb(Spanish):
{
"appTitle": "Localización de GeeksforGeeks",
"welcomeMessage": "¡Bienvenido a GeeksforGeeks!",
"languageSelection": "Selecciona un idioma:",
"englishOption": "Inglés",
"spanishOption": "Español"
}
Step 4: Configuring the Main App
Let's set up our main app to use localization.
Replace the contents of lib/main.dart with:
main.dart
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'home.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'GeeksforGeeks Localization',
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
Locale('en', ''), // English
Locale('es', ''), // Spanish
],
home: Home(),
);
}
}
- This code initializes a Flutter app that supports localization and also configures it for English and Spanish.
- It then loads the necessary resources needed for localization and makes the Home widget the initial screen.
- This application is able to load and show localized buttons or strings from my localizationsDelegates and supportedLocales based on user locale settings.
Step 5: Creating the Home Screen
Now, let's create a home screen that demonstrates localization in action.
Create a new file lib/home.dart:
home.dart
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
final localizations = AppLocalizations.of(context)!;
return Scaffold(
appBar: AppBar(
title: Text(localizations.appTitle),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
localizations.welcomeMessage,
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
SizedBox(height: 20),
Text(
localizations.languageSelection,
style: TextStyle(fontSize: 18),
),
SizedBox(height: 10),
ElevatedButton(
onPressed: () => _changeLocale(context, Locale('en')),
child: Text(localizations.englishOption),
),
SizedBox(height: 10),
ElevatedButton(
onPressed: () => _changeLocale(context, Locale('es')),
child: Text(localizations.spanishOption),
),
],
),
),
);
}
void _changeLocale(BuildContext context, Locale newLocale) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (BuildContext context) => Localizations.override(
context: context,
locale: newLocale,
child: Home(),
),
),
);
}
}
Don't forget to import HomeScreen in main.dart
- The Home widget shows localized text for different parts of the app such as title, welcome message, language selection options.
- This app has been made in such a way that users can switch between English and Spanish languages without having to restart it every time they want to change language settings dynamically.
- The _changeLocale method creates a new instance of this class with the current build context, ensuring that when it’s called again later on all changes will apply accordingly so as not only does our app reflect selected languages but also keeps sending important data back home even though we may lose those at some point in future due say an update by flutter itself or just normal software updates during which several recompilations take place especially if many libraries were updated along with their dependencies.
Step 6: Generating Localization Files
flutter gen-l10n
This command will generate the necessary Dart files for localization based on our ARB files.
Step 7: Running the App
flutter run
You should see an app with a welcome message and two buttons to switch between English and Spanish.
Output :
Similar Reads
Multi Page Applications in Flutter
Apps are widely used by humans in this techie world. The number of apps in the app store is increasing day by day. Due to this competition, app developers have started to add a number of features to their apps. To reduce this complexity, the content of the app is mostly divided into various pages so
5 min read
Photo Editing Application in Flutter
With the introduction of the powerful image_editor_plus package, editing images directly within your Flutter app has become significantly more accessible. In this article, we'll dive deep into building a user-friendly image editor app. Users will have the ability to select an image from their camera
7 min read
Background local notifications in Flutter
Sometimes user wants some essential features like the latest news updates, latest articles updates, weather condition alert, complete his/her profile update, future events update and much more in his/ her Android or iOS device without opening App and if you would like to develop this kind of Android
6 min read
Flutter - Physics Simulation in Animation
Physics simulation in Flutter is a beautiful way to Animate components of the flutter app to make it look more realistic and interactive. These can be used to create a range of animations like falling objects due to gravity to making a container seem attached to a spring. In this article, we will ex
4 min read
Recipe Finder Application in Flutter
This app will display a list of recipes in a card format, each containing the recipe title, a rating, the cooking time, and a thumbnail image to give users a visual preview. The app is dynamic and flexible, allowing you to easily update the recipe list or modify the UI as needed. By the end of this
5 min read
EBook reader Application in Flutter
EBook reader Application brings the library to your fingertips. This application will be travel partner of every book lover who loves to read books of their choice. The app is developed using Flutter and provider state management. It uses google books API to fetch the data of books. The app allows y
7 min read
How to Build a ToDo Application in Flutter?
Flutter offers a stable framework for constructing richly UI-driven cross-platform applications. In this article, we will learn to build a ToDo Flutter Application. What is the ToDo Application?The ToDo application helps users manage and organize their tasks and responsibilities more easily. Managin
6 min read
Flutter - Page Transition Animation
In Flutter, we can easily animate or handle the page transitions by using the page_transition package. The page_transition package is a valuable addition to the Flutter package, offering a variety of transition effects that can elevate the app's UI. In this article, we are going to explore how to in
6 min read
Flutter - Lottie Animation
Visualization is an integral part of any application. Animations can highly glorify the UI of an app, but animations can be hectic to implement for an application. This is where the Lottie animation comes in. Lottie is a JSON-based animation file. It can be used both as a network asset and a static
4 min read
Creating a Simple Application in Flutter
Flutter is an open-source cross-platform mobile application development SDK created by Google. It is highly user-friendly and builds high-quality mobile applications. The intention behind this article is to guide readers through the process of building an application through Flutter by creating a si
5 min read