Mastering Flutter Localization Building
Create Multilingual Flutter App
In today's globalized world, building apps that cater to users from diverse linguistic backgrounds is no longer just an option; it's a necessity. Whether you're developing a simple utility app or a complex enterprise solution, ensuring that your app speaks the language of your users can significantly enhance user experience and engagement.
One of the most popular frameworks for building cross-platform mobile applications is Flutter. Flutter provides a rich set of tools and libraries for developing beautiful and performant apps, and it also offers robust support for localization and internationalization out of the box.
In this comprehensive guide, we'll dive deep into Flutter localization, exploring everything from setting up your project for multilingual support to implementing dynamic language switching within your app. By the end of this guide, you'll be equipped with the knowledge and skills to create multilingual Flutter apps that resonate with users worldwide.
So, without further ado, let's embark on our journey to mastering Flutter localization!
Below is visual demo:
Setting Up Your Project
Before we delve into the intricacies of Flutter localization, let's ensure that our project is properly configured to support multilingualism. The first step involves adding the necessary dependencies to our pubspec.yaml file:
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
cupertino_icons: ^1.0.2
flutter_localization: ^0.2.0
Understanding the Code
import 'package:flutter/material.dart';
import 'package:localizationdemo/localization/string_es.dart';
import 'package:localizationdemo/localization/string_en.dart';
class AppLocalizations {
final Locale locale;
AppLocalizations(this.locale);
static Map<String, Map<String, String>> _localizedValues = {
'en': englishStrings,
'es': spanishStrings,
};
String? translate(String key) {
return _localizedValues[locale.languageCode]?[key];
}
static AppLocalizations? of(BuildContext context) {
return Localizations.of<AppLocalizations>(context, AppLocalizations);
}
}
class AppLocalizationDelegate extends LocalizationsDelegate<AppLocalizations>{
const AppLocalizationDelegate();
@override
bool isSupported(Locale locale) {
return ['en','es'].contains(locale.languageCode);
}
@override
Future<AppLocalizations> load(Locale locale) async{
return AppLocalizations(locale);
}
@override
bool shouldReload(covariant LocalizationsDelegate<AppLocalizations> old) {
return false;
}
}
string_en.dart and string_es.dart
Map<String, String> englishStrings = {
'helloworld': 'Hello World',
'medicine' : 'Medicine',
'hospital' : 'Hospital'
};
Map<String, String> spanishStrings = {
'helloworld': '¡Hola, Mundo!',
'medicine':'Medicina',
'hospital':'Hospital'
};
localization_screen.dart
import 'package:flutter/material.dart';
import 'localization/applocalizationsdelegate.dart';
class LocalizationScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return LocalizationScreenState();
}
}
class LocalizationScreenState extends State<LocalizationScreen> {
Locale _currentLocale = Locale('en');
void _changeLanguage(Locale locale) {
setState(() {
_currentLocale = locale;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'Localization Demo in ' + _currentLocale.languageCode,
style: TextStyle(color: Colors.black, fontSize: 20),
),
SizedBox(height: 20),
Text(
AppLocalizations(_currentLocale).translate('medicine') ?? '',
style: TextStyle(color: Colors.black),
),
Text(
AppLocalizations(_currentLocale).translate('hospital') ?? '',
style: TextStyle(color: Colors.black),
),
Text(
AppLocalizations(_currentLocale).translate('helloworld') ?? '',
style: TextStyle(color: Colors.black),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
_changeLanguage(Locale('en'));
},
child: Text('English'),
),
SizedBox(height: 10),
ElevatedButton(
onPressed: () {
_changeLanguage(Locale('es'));
},
child: Text('Spanish'),
),
],
),
),
);
}
}
main.dart
import 'package:flutter/material.dart';
import 'package:flutter_localization/flutter_localization.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:localizationdemo/localization/string_en.dart' as en;
import 'package:localizationdemo/localization/string_es.dart' as es;
import 'package:localizationdemo/localization_screen.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
supportedLocales: [
Locale('en', 'EN'),
Locale('es', 'ES'),
],
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
AppLocalizationDelegate(),
],
home: LocalizationScreen(),
);
}
}
Comments
Post a Comment