From 'Hello' to 'Hola': Your Fun First Guide to Flutter Localization

10 Minby Muhammad Fahid Sarker
FlutterLocalizationl10nInternationalizationi18nDartMobile App DevelopmentBeginner GuideFlutter TutorialARB files
From 'Hello' to 'Hola': Your Fun First Guide to Flutter Localization

So, You Built an Awesome App. But Can My Grandma in Spain Use It?

Picture this: you've spent weeks building the next big thing. A cat photo-sharing app. It's sleek, it's fast, it's got infinite scroll. You show it to your friend from Germany, and they just stare blankly at the button that says "Purr-fectly Upload!". Uh oh. Your app only speaks English!

This is the exact problem that Localization solves. It's the process of making your app feel native to users in different parts of the world. And today, we're going to learn how to do it in Flutter, without pulling our hair out.

The Two Musketeers: i18n and l10n

You'll see these weird-looking acronyms everywhere: i18n and l10n. They look like a cat walked over a keyboard, but they're actually quite simple:

  • Internationalization (i18n): This is the prep work. It's like building a house with universal power outlets that can accept any plug type. You're designing your app's code from the ground up so that it can be translated easily. (Why i18n? There are 18 letters between the 'i' and the 'n' in 'internationalization'. Nerds.)

  • Localization (l10n): This is the actual translation. It's like plugging a specific Spanish adapter into your universal outlet. You're providing the Spanish text, date formats, and currency symbols for your Spanish users. (Yep, 10 letters between 'l' and 'n'.)

In short: i18n is the how, l10n is the what.

Step 1: Setting Up Your Translation Library

First, we need to tell our app that it's about to become a polyglot. We do this by adding a few dependencies to our pubspec.yaml file. Think of this file as your app's shopping list.

yaml
dependencies: flutter: sdk: flutter # This is the official Flutter package for localization flutter_localizations: sdk: flutter # This package helps with formatting, dates, and other locale-specific stuff intl: ^0.18.1 # Use the latest version # ... rest of your pubspec.yaml

After adding these, run flutter pub get in your terminal to download your new tools.

Step 2: Creating Your Dictionaries (ARB Files)

Now, where do we store our translations? In special files called ARB files (.arb), which stands for Application Resource Bundle. Just think of them as simple dictionary files.

  1. Create a new folder in your lib directory called l10n.
  2. Inside lib/l10n, create your first dictionary file: app_en.arb. This is for English.
json
{ "@@locale": "en", "helloWorld": "Hello World!", "welcomeMessage": "Welcome to my awesome app, {userName}!", "@welcomeMessage": { "description": "A welcome message that greets the user by name", "placeholders": { "userName": { "type": "String", "example": "Alex" } } } }

Notice a few things:

  • "@@locale": "en": Tells the system this file is for English.
  • "helloWorld": "Hello World!": A simple key-value pair.
  • "welcomeMessage": "...{userName}!": You can even have placeholders! This is super powerful.
  • The "@welcomeMessage" block is extra info for you or your translators. It's like a helpful sticky note!

Now, let's create the Spanish dictionary, app_es.arb:

json
{ "@@locale": "es", "helloWorld": "¡Hola Mundo!", "welcomeMessage": "¡Bienvenido a mi increíble aplicación, {userName}!" }

Easy, right? You just create one of these files for every language you want to support.

Step 3: The Magic Wand (gen-l10n)

Now we need to tell Flutter about our new dictionary files and ask it to generate some magic code for us. Back in pubspec.yaml, add this section at the bottom:

yaml
# Add this at the root level of the file, same level as 'dependencies' flutter: generate: true

This single line tells Flutter to automatically look for a special configuration file or use its defaults to generate localization files. To be more explicit, you can create a file named l10n.yaml in your project's root directory:

yaml
# l10n.yaml arb-dir: lib/l10n template-arb-file: app_en.arb output-localization-file: app_localizations.dart
  • arb-dir: "Hey Flutter, my dictionaries are in lib/l10n."
  • template-arb-file: "Use the English file as the master copy."
  • output-localization-file: "Please create a helper file for me here."

Now, run this command in your terminal:

bash
flutter gen-l10n

Poof! Flutter will read your .arb files and generate all the boilerplate code for you. You'll see a new app_localizations.dart file and some other generated files. You should never edit these files by hand. They are your loyal, code-generating servants.

Step 4: Wiring It All Up

Okay, the backstage work is done. Let's tell our MaterialApp to use all this cool stuff. Go to your main.dart file.

dart
import 'package:flutter/material.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; // This is the magic file Flutter generated for us! import 'package:flutter_gen/gen_l10n/app_localizations.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); Widget build(BuildContext context) { return MaterialApp( title: 'Localization Demo', // 1. Add the localization delegates localizationsDelegates: const [ AppLocalizations.delegate, GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, GlobalCupertinoLocalizations.delegate, ], // 2. Add the supported locales supportedLocales: const [ Locale('en'), // English Locale('es'), // Spanish ], home: const HomeScreen(), ); } }

Whoa, what are localizationsDelegates? Think of them as a committee of translators.

  • AppLocalizations.delegate: This is our translator, for the text we wrote (helloWorld, etc.).
  • The Global... delegates: These are Flutter's built-in translators. They handle translating things like 'OK', 'Cancel', date pickers, and other default widgets for you. So generous!

supportedLocales is just the list of languages our app officially speaks.

Step 5: Using Your Translations!

This is the best part. It's so simple. In any widget that has access to the context, you can now grab your translated text.

dart
import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; class HomeScreen extends StatelessWidget { const HomeScreen({super.key}); Widget build(BuildContext context) { // This is how you access your translations! final l10n = AppLocalizations.of(context)!; String userName = "Maria"; return Scaffold( appBar: AppBar( // Use the getter for 'helloWorld' title: Text(l10n.helloWorld), ), body: Center( child: Text( // Use the method for the message with a placeholder l10n.welcomeMessage(userName), style: Theme.of(context).textTheme.headlineMedium, ), ), ); } }

That's it! AppLocalizations.of(context)!.helloWorld will automatically show "Hello World!" if the device language is English, and "¡Hola Mundo!" if it's Spanish. Flutter handles the logic for you.

What's Next?

You've just conquered the basics of Flutter localization! You can now build apps that feel welcoming to people all over the globe. From here, you can explore more advanced topics like:

  • Dynamic Language Switching: Adding a dropdown in your app to let users change the language on the fly (often done with a state management solution like Provider or BLoC).
  • Formatting: Using the intl package to correctly format dates, times, and numbers (e.g., 1,000.00 in the US vs. 1.000,00 in Germany).
  • Right-to-Left (RTL) Languages: Supporting languages like Arabic or Hebrew, where the UI layout needs to be flipped.

Go forth and build apps that the whole world can enjoy. Happy coding!

Related Articles