'Why is Flutter not generating the internationalization files?

I'm trying to follow the internationalization documentation in https://flutter.dev/docs/development/accessibility-and-localization/internationalization#dart-tools and https://docs.google.com/document/d/10e0saTfAv32OZLRmONy866vnaw0I2jwL8zukykpgWBc/edit#heading=h.upcu5w85cvc2 but it's not generating any files.

Basically it says to make these mods to the pub spec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  intl: ^0.17.0-nullsafety.2
flutter:
  generate: true

Then create a <project-root>/l10n.yaml file containing:

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

And finally to create the app_en.arb with something like this:

{
  "@@locale": "en",

  "helloWorld": "Hello World!",
  "@helloWorld": {
    "description": "The conventional newborn programmer greeting"
  }
}

And from there the guides say that a flutter_gen/gen_l10n/app_localizations.dart file will be automatically generated.

Except that nothing happens. I'm working in Android Studio and did a pub get, and tried a flutter clean and flutter build ios and everything else I can't think of but nothing is building that file.

Any ideas?



Solution 1:[1]

Ok. done some more digging and I've solved it. Basically the Flutter documentation is slightly out of date.

Firstly, the generated files are being generated, but they're in <project_dir>.dart_tools/flutter_gen/genl10n. The generator creates a synthetic package that is automatically available to the project so there is no need for any further pubspec.yaml changes.

Secondly, your main.dart has to look like this:

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: AppLocalizations.localizationsDelegates,
      supportedLocales: AppLocalizations.supportedLocales,
      title: 'My app',
      home: ... ,
    );
  }
}

Two things here:

  1. The importing of the app_localizations.dart generated file (which the docs do mention but perhaps not explain well) and ...
  2. Changing the localizationsDelegates and supportedLocales. You don't need to list all the delegates and locales mentioned in the docs as the generated localisation files automatically include them. Just switch to the two properties of AppLocalizations.

PS

After writing the above I attempted to internationalise the app's title:

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: AppLocalizations.localizationsDelegates,
      supportedLocales: AppLocalizations.supportedLocales,
      title: AppLocalizations.of(context).applicationTitle,
      home: ... ,
    );
  }

Epic fail - The reason is that at the time it goes to resolve the title, the delegates and locales have not yet been set so what comes back from AppLocalizations.of(context) is a null. Instead you need to change to the onGeneratedTitle like this:

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: AppLocalizations.localizationsDelegates,
      supportedLocales: AppLocalizations.supportedLocales,
      onGenerateTitle: (context) => AppLocalizations.of(context).applicationTitle,
      home: ... ,
    );
  }
```.  

`onGeneratedTitle` is called after the widget is setup which means localisation is available.

Solution 2:[2]

Addition to @drekka answer,

You need to run

flutter gen-l10n

If it's not generated automatically.

Solution 3:[3]

Faced similar issue when needed to generate these files through CLI without IDE at all (on CircleCI).

First, you should have intl_utils either as project dependency or activated globally.

To install it as a dependency (and manage its version per project) - just add intl_utils: ^2.1.0 to the dependencies section of your pubspec.yaml (don't forget to set the version you need). After that, from the project directory run:

flutter gen-l10n --template-arb-file=intl_en.arb
flutter pub run intl_utils:generate

(change intl_en.arb to your actual .arb file name or omit the whole parameter in case it matches the default app_en.arb)

To activate intl_utils globally (and use a single version of intl_utils on all your projects), do the following:

dart pub global activate intl_utils 2.1.0

And then run this from the project directory:

flutter gen-l10n --template-arb-file=intl_en.arb
dart pub global run intl_utils:generate

In my case, since the project hasn't yet migrated to use null safety, having intl_utils as project dependency led to null safety issues, so the trick was to use intl_utils 1.9.0 globally.

Solution 4:[4]

I've run into this same issue right now. I don't know why this is happening, but I've found this post providing the same information and decided to try using the plugin it mentions.

The plugin is called "Flutter Intl" and is available for Android Studio and VSCode. You'll need to install it in your IDE and run the "Flutter Intl: initialize" command. This action should create the "lib/generated" folder with all the needed boilerplate.

Solution 5:[5]

Run this on your terminal or command-line:
dart pub global activate intl_utils 2.1.0
OR
dart pub global run intl_utils:generate

Solution 6:[6]

Had the same problem. Accidentally found out that I had created l10n.yaml in the lib dir instead of base dir. Moved it where it should be, and everything worked!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ray Jasson
Solution 2 Simon Pham
Solution 3 DIESEL
Solution 4 Manuel
Solution 5
Solution 6