Localization

suggest change

Internationalization (i18n) and Localization (l10n) adapt software applications for different languages, geographic regions, and cultural conventions (including number formatting, currency symbols, date styles, and pluralization rules).

In Dart and Flutter, localization is powered by the official intl package.


Installing package:intl

Add intl to your pubspec.yaml:

dependencies:
  intl: ^0.20.2

Run dart pub get to download the package.


Setting the Active Locale

Set Intl.defaultLocale to configure the default locale for formatting operations:

import 'package:intl/intl.dart';

void main() {
  Intl.defaultLocale = 'fr_FR'; // French (France)
  print('Current locale: ${Intl.getCurrentLocale()}');
}

Formatting Numbers and Currencies

Different locales format decimal points and thousands separators differently (e.g. 1,234.56 in the US vs 1.234,56 in Germany):

import 'package:intl/intl.dart';

void main() {
  final amount = 12450.75;

  // Standard Decimal Formatting
  final usFormat = NumberFormat.decimalPattern('en_US');
  final deFormat = NumberFormat.decimalPattern('de_DE');

  print('US Number: ${usFormat.format(amount)}'); // "12,450.75"
  print('DE Number: ${deFormat.format(amount)}'); // "12.450,75"

  // Currency Formatting
  final usCurrency = NumberFormat.currency(locale: 'en_US', symbol: '\$');
  final deCurrency = NumberFormat.currency(locale: 'de_DE', symbol: '€');

  print('US Price: ${usCurrency.format(amount)}'); // "$12,450.75"
  print('DE Price: ${deCurrency.format(amount)}'); // "12.450,75 €"

  // Compact Number Representation (e.g., 1.2M)
  final compact = NumberFormat.compact(locale: 'en_US');
  print('Compact: ${compact.format(1500000)}'); // "1.5M"
}

Date and Time Formatting

Dates should always be formatted to match the user's regional expectations:

import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';

void main() async {
  // Initialize locale data for non-English dates
  await initializeDateFormatting('es_ES', null);

  final now = DateTime(2026, 10, 17, 14, 30);

  // US Format: October 17, 2026
  final usDate = DateFormat.yMMMMd('en_US').format(now);
  print('US Date: $usDate');

  // Spanish Format: 17 de octubre de 2026
  final esDate = DateFormat.yMMMMd('es_ES').format(now);
  print('ES Date: $esDate');

  // Time format: 2:30 PM
  final timeFormat = DateFormat.jm('en_US').format(now);
  print('Time: $timeFormat');
}

Pluralization and Gender Rules

Grammatical plural rules vary wildly across languages (e.g. Arabic has six plural forms, while English has two). Use Intl.plural:

import 'package:intl/intl.dart';

String unreadMessagesCount(int count) {
  return Intl.plural(
    count,
    zero: 'You have no unread messages.',
    one: 'You have 1 unread message.',
    other: 'You have $count unread messages.',
    name: 'unreadMessagesCount',
    args: [count],
  );
}

void main() {
  print(unreadMessagesCount(0)); // "You have no unread messages."
  print(unreadMessagesCount(1)); // "You have 1 unread message."
  print(unreadMessagesCount(5)); // "You have 5 unread messages."
}

Gender Agreement

String membershipWelcome(String gender, String name) {
  return Intl.gender(
    gender,
    male: 'Mr. $name, welcome to the club!',
    female: 'Ms. $name, welcome to the club!',
    other: '$name, welcome to the club!',
    name: 'membershipWelcome',
    args: [gender, name],
  );
}

Application Resource Bundles (ARB Files)

In production Flutter applications, messages are extracted into .arb JSON files:

// lib/l10n/app_en.arb
{
  "welcomeTitle": "Welcome back, {username}!",
  "@welcomeTitle": {
    "description": "Greeting message shown on home dashboard",
    "placeholders": {
      "username": {
        "type": "String",
        "example": "Alice"
      }
    }
  }
}
// lib/l10n/app_es.arb
{
  "welcomeTitle": "¡Bienvenido de nuevo, {username}!"
}

Enable code generation in l10n.yaml:

# l10n.yaml
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart

In Dart/Flutter code:

// Access generated localized strings type-safely:
// Text(AppLocalizations.of(context)!.welcomeTitle('Alice'))

Best Practices

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents