Sub topic 7
Using Translated Strings in Widgets
This chapter assumes basic Dart syntax, null safety, and Flutter widget construction. It also assumes that flutter_localizations, intl, l10n.yaml, and the ARB files are already configured, and that Flutter has generated AppLocalizations.
Get the Localizations Object
The generated AppLocalizations class provides the translated messages for the current locale. Retrieve it with AppLocalizations.of(context) inside a widget’s build method:
import 'package:flutter/material.dart';
import 'l10n/app_localizations.dart';
class TripScreen extends StatelessWidget {
const TripScreen({super.key});
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
return Scaffold(
appBar: AppBar(
title: Text(l10n.appTitle),
),
body: Center(
child: Text(l10n.greeting('Ada')),
),
);
}
}context is the widget’s location in the widget tree. Flutter uses it to find the localization data provided by MaterialApp.
AppLocalizations.of(context) returns a nullable value because a widget might be built outside a localization scope. The non-null assertion operator, !, is appropriate when the widget is known to be below a correctly configured MaterialApp.
A message without placeholders becomes a property, such as l10n.appTitle. A message with a placeholder becomes a method, such as l10n.greeting('Ada').
Read Messages in Different Widgets
Keep translated text out of widgets when the text comes from ARB files. This keeps translations in one place and lets translators work without editing Dart code.
class TripSummary extends StatelessWidget {
const TripSummary({super.key});
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
final departureDate = DateTime(2026, 3, 14);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
l10n.greeting('Ada'),
style: Theme.of(context).textTheme.headlineSmall,
),
Text(l10n.tripCount(3)),
Text(l10n.departure(departureDate)),
],
);
}
}Generated methods use the parameter types declared in the ARB metadata. For example, tripCount expects an int, while departure expects a DateTime. Passing a value with the wrong type produces a compile-time error.
You can use localized messages in labels, buttons, tooltips, validation text, and accessibility properties:
final l10n = AppLocalizations.of(context)!;
return ElevatedButton(
onPressed: () {},
child: Text(l10n.appTitle),
);Avoid saving a translated string in a long-lived field. The string depends on the current locale. Read it during build, so the widget displays the new translation after Flutter rebuilds it.
Understand the Lookup Flow
The relationship between a widget and its translated message is:
When the locale changes, Flutter updates the localization data and rebuilds widgets that depend on it. Calling AppLocalizations.of(context) during build ensures the widget observes that update.
Common Errors
- Calling
AppLocalizations.of(context)beforeMaterialAppcreates its localization scope. - Forgetting to include
AppLocalizations.localizationsDelegates. - Using a message key that does not exist in the template ARB file.
- Passing a
Stringwhere a generated method expects anintorDateTime. - Hard-coding part of a sentence around a translated message. Word order differs between languages, so put the complete sentence in ARB.
Exercise
Why should a widget retrieve AppLocalizations during build instead of storing a translated string in initState?
Show answer
The message depends on the current locale. Retrieving it during build lets Flutter display the updated translation after a locale change.