Cannot Generate Localizations In A Workspace

by ADMIN 45 views

Introduction

In this article, we will explore the issue of generating localizations in a Flutter workspace. We will go through the steps to reproduce the problem, the expected results, and the actual results. We will also discuss the possible solutions and the code sample provided.

Steps to Reproduce

To reproduce this issue, follow these steps:

  1. Create a new Flutter project using the command flutter create my_app.
  2. Navigate to the my_app directory and create a new package using the command flutter create -p packages/my_package my_package.
  3. In the my_package directory, create a new file called l10n.yaml with the following content:
arb-dir: lib/l10n
template-arb-dir: lib/l10n/templates
output-localization-file: lib/l10n/localizations.dart
  1. In the lib/l10n directory, create a new file called messages.arb with the following content:
en:
  hello: Hello, world!
  1. Run the command flutter gen-l10n inside the my_app directory.

Expected Results

The expected result is that the command flutter gen-l10n generates the app_localizations.dart and app_localizations_en.dart files correctly.

Actual Results

However, when running the command flutter gen-l10n inside the my_app directory, the actual result is that the command fails with the following error message:

Because l10n.yaml exists, the options defined there will be used instead.
To use the command line arguments, delete the l10n.yaml file in the Flutter project.


Attempted to generate localizations code without having the flutter: generate flag turned on.
Check pubspec.yaml and ensure that flutter: generate: true has been added and rebuild the project. Otherwise, the localizations source code will not be importable.

Possible Solutions

One possible solution is to add the generate: true flag to the pubspec.yaml file in the my_app directory. However, this will cause all other Flutter commands requiring pub get to fail.

Another possible solution is to use the flutter: generate flag in the pubspec.yaml file in the my_package directory. However, this will not generate the localizations files correctly.

Code Sample

A repository with the sample code can be found here: https://github.com/PiotrRogulski/l10n_bug

Flutter Doctor Output

The Flutter Doctor output is as follows:

[✓] Flutter (Channel master, 3.30.0-1.0.pre.523, on Manjaro Linux 6.12.16-1-MANJARO, locale en_US.UTF-8) [3.5s]
    • Flutter version 3.30.0-1.0.pre.523 on channel master at /home/piotr/fvm/versions/master
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision b30fe22b98 (20 hours ago), 2025-03-08 11:44:26 -0500
    • Engine revision b30fe22b98
    • Dart version 3.8.0 (build 3.8.0-149.0.dev)
    • DevTools version 2.43.0

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.1) [3.3s]
    • Android SDK at /home/piotr/Android/Sdk
    • Platform android-35, build-tools 35.0.1
    • ANDROID_HOME = /home/piotr/Android/Sdk
    • Java binary at: /home/piotr/.local/share/JetBrains/Toolbox/apps/android-studio-2/jbr/bin/java
      This is the JDK bundled with the latest Android Studio installation on this machine.
      To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 21.0.5+-12771278-b631.28)
    • All Android licenses accepted.

[✓] Chrome - develop for the web [139ms]
    • CHROME_EXECUTABLE = google-chrome-unstable

[✓] Linux toolchain - develop for Linux desktop [1,722ms]
    • clang version 19.1.7
    • cmake version 3.31.6
    • ninja version 1.12.1
    • pkg-config version 2.3.0
    • OpenGL core renderer: Mesa Intel(R) UHD Graphics (CML GT2)
    • OpenGL core version: 4.6 (Core Profile) Mesa 24.3.4-arch1.1
    • OpenGL core shading language version: 4.60
    • OpenGL ES renderer: Mesa Intel(R) UHD Graphics (CML GT2)
    • OpenGL ES version: OpenGL ES 3.2 Mesa 24.3.4-arch1.1
    • OpenGL ES shading language version: OpenGL ES GLSL ES 3.20
    • GL_EXT_framebuffer_blit: yes
    • GL_EXT_texture_format_BGRA8888: yes

[✓] Android Studio (version 2024.3) [136ms]
    • Android Studio at /home/piotr/.local/share/JetBrains/Toolbox/apps/android-studio-2
    • Flutter plugin version 83.0.4
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 21.0.5+-12771278-b631.28)

[✓] Connected device (2 available) [246ms]
    • Linux (desktop) • linux  • linux-x64      • Manjaro Linux 6.12.16-1-MANJARO
    • Chrome (web)    • chrome • web-javascript • Google Chrome 135.0.7039.0 dev

[✓] Network resources [324ms]
    • All expected network resources are available.

• No issues found!

Conclusion

Q: What is the issue with generating localizations in a workspace?

A: The issue is that the flutter gen-l10n command fails with an error message indicating that the flutter: generate flag is not turned on. This flag is required to generate localizations code.

Q: Why is the flutter: generate flag required?

A: The flutter: generate flag is required because it tells Flutter to generate the localizations code based on the l10n.yaml file. Without this flag, Flutter will not generate the localizations code.

Q: How do I add the flutter: generate flag to my project?

A: To add the flutter: generate flag to your project, you need to add the following line to your pubspec.yaml file:

flutter:
  generate: true

Q: What happens if I add the flutter: generate flag to my project?

A: If you add the flutter: generate flag to your project, you will be able to generate localizations code using the flutter gen-l10n command. However, this will also cause all other Flutter commands requiring pub get to fail.

Q: Why do all other Flutter commands requiring pub get fail if I add the flutter: generate flag?

A: All other Flutter commands requiring pub get fail because the flutter: generate flag is not supported within workspaces. This means that if you add the flutter: generate flag to your project, you will not be able to use other Flutter commands that require pub get.

Q: What is the alternative to adding the flutter: generate flag to my project?

A: The alternative to adding the flutter: generate flag to your project is to use the flutter: generate flag in the pubspec.yaml file of the package that contains the localizations code. This will allow you to generate localizations code without causing all other Flutter commands requiring pub get to fail.

Q: How do I use the flutter: generate flag in the pubspec.yaml file of the package that contains the localizations code?

A: To use the flutter: generate flag in the pubspec.yaml file of the package that contains the localizations code, you need to add the following line to the pubspec.yaml file:

flutter:
  generate: true

Q: What are the benefits of using the flutter: generate flag in the pubspec.yaml file of the package that contains the localizations code?

A: The benefits of using the flutter: generate flag in the pubspec.yaml file of the package that contains the localizations code are that you will be able to generate localizations code without causing all other Flutter commands requiring pub get to fail.

Q: What are the limitations of using the flutter: generate flag in the pubspec.yaml file of the package that contains the localizations code?

A: The limitations of using the flutter: generate flag in the pubspec.yaml file of the package that contains the localizations code are that you will need to manually add the flutter: generate flag to the pubspec.yaml file of each package that contains localizations code.

Q: How do I troubleshoot issues with generating localizations in a workspace?

A: To troubleshoot issues with generating localizations in a workspace, you can try the following:

  • Check the pubspec.yaml file to ensure that the flutter: generate flag is set to true.
  • Check the l10n.yaml file to ensure that it is correctly configured.
  • Run the flutter gen-l10n command with the --verbose flag to get more detailed output.
  • Check the Flutter Doctor output to ensure that there are no other issues with your project.

Q: Where can I find more information about generating localizations in a workspace?

A: You can find more information about generating localizations in a workspace in the official Flutter documentation. Specifically, you can check out the following resources: