Internationalization in Flutter
Learn to internationalize your Flutter application.
Overview
If our application is accessed and used by people speaking different languages, we’ll need to internationalize it.
The application below is in English only; we’ll internationalize it to add Spanish translations.
Note: The code in the code widget below will not successfully compile until you’ve completed step 3 given below.
// TODO-9: Import Dart UI library
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
// TODO-5: Import your app localization class
import 'home_page.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Internationalization Demo',
// TODO-6: Set your localization delegates and supported locales here
theme: ThemeData(primarySwatch: Colors.blue, useMaterial3: false),
home: const HomePage(),
// TODO-10: Set localeResolutionCallback to support web
debugShowCheckedModeBanner: false,
);
}
}
Internationalizaton lesson example starter code
Preparing to internationalize the application
- Go to the
pubspec.yamlfile and replace# TODO-1: Add flutter_localizations and intl packages(line 13) with the following lines under thedependencieskey:
flutter_localizations:
sdk: flutter
intl: ^0.18.0
The lines above will add flutter_localizations and intl packages as your application’s dependencies.
- Still in
pubspec.yaml, replace# TODO-2: Enable code generationwith the line below. The key should be under theflutterkey in the YAML file.
generate: true
Enabling the generate flag ...