为什么 Flutter 不生成国际化文件?

Why is Flutter not generating the internationalization files?

我正在尝试遵循 https://flutter.dev/docs/development/accessibility-and-localization/internationalization#dart-tools and https://docs.google.com/document/d/10e0saTfAv32OZLRmONy866vnaw0I2jwL8zukykpgWBc/edit#heading=h.upcu5w85cvc2 中的国际化文档,但它没有生成任何文件。

基本上它说要将这些 mod 制作到 pub spec.yaml 文件:

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

然后创建一个 <project-root>/l10n.yaml 文件包含:

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

最后用这样的东西创建 app_en.arb

{
  "@@locale": "en",

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

然后指南说会自动生成 flutter_gen/gen_l10n/app_localizations.dart 文件。

只是没有任何反应。我在 Android Studio 工作并做了 pub get,并尝试了 flutter cleanflutter build ios 以及我想不到的所有其他内容,但没有构建该文件。

有什么想法吗?

我现在 运行 遇到了同样的问题。我不知道为什么会这样,但我发现这个 post 提供了相同的信息并决定尝试使用它提到的插件。

该插件名为“Flutter Intl”,可用于 Android Studio and VSCode。您需要将其安装在 IDE 和 运行 的“Flutter Intl: initialize”命令中。此操作应创建包含所有所需样板的“lib/generated”文件夹。

好的。做了更多的挖掘,我已经解决了。基本上 Flutter 文档有点过时了。

首先,正在生成生成的文件,但它们在<project_dir>.dart_tools/flutter_gen/genl10n中。生成器会创建一个自动可供项目使用的合成包,因此无需进行任何进一步的 pubspec.yaml 更改。

其次,您的 main.dart 必须如下所示:

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: ... ,
    );
  }
}

这里有两件事:

  1. 导入 app_localizations.dart 生成的文件(文档确实提到但可能没有很好地解释)和...
  2. 正在更改 localizationsDelegatessupportedLocales。您不需要列出文档中提到的所有代表和语言环境,因为生成的本地化文件会自动包含它们。只需要切换到AppLocalizations.
  3. 的两个属性

PS

写完以上内容后,我尝试将应用程序的标题国际化:

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

Epic 失败 - 原因是在解析标题时,代理和语言环境尚未设置,因此 AppLocalizations.of(context) 返回的是 null。相反,您需要像这样更改为 onGeneratedTitle

  @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.

@drekka 回答的补充,

你需要运行

flutter gen-l10n

如果不是自动生成。

当需要通过 CLI 生成这些文件时遇到类似的问题,根本没有 IDE(在 CircleCI 上)。

首先,您应该 intl_utils 作为项目依赖项或全局激活。

要将其作为依赖项安装(并管理每个项目的版本)- 只需将 intl_utils: ^2.1.0 添加到 pubspec.yamldependencies 部分(不要忘记设置你需要的版本)。之后,从项目目录运行:

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

(将 intl_en.arb 更改为您实际的 .arb 文件名或省略整个参数,以防它与默认值 app_en.arb 匹配)

要全局激活 intl_utils(并在所有项目中使用单一版本的 intl_utils),请执行以下操作:

dart pub global activate intl_utils 2.1.0

然后 运行 来自项目目录的这个:

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

在我的例子中,由于项目尚未迁移到使用空安全,将 intl_utils 作为项目依赖项会导致空安全问题,因此技巧是全局使用 intl_utils 1.9.0

有同样的问题。无意中发现我在 lib 目录而不是基本目录中创建了 l10n.yaml。把它移到它应该在的地方,一切正常!

运行 在您的终端或命令行上:
dart pub 全局激活 intl_utils 2.1.0
或者
dart pub global 运行 intl_utils:generate