Flutter 国际化,国际代码生成不起作用
Flutter Internationalization, intl code generation not working
我用 Android Studio 创建了一个新的 Flutter 项目。我正在使用 null-safety 并且我是 运行 物理设备上的应用程序,但 code-generation 不工作, flutter_gen 文件夹里面没有 .dart_tool 文件夹只有 flutter_build文件夹。
我遵循了Flutter's official Internationalizing documentation
中的步骤
我没有使用 Flutter Intl 插件,因为它不支持 null-safety(我知道你可以制作一个脚本来添加旧的 dart 版本每个生成文件的顶部作为解决方法)
flutter doctor命令输出:
[✓] Flutter (Channel beta, 1.26.0-17.5.pre, on Microsoft Windows [Versión 10.0.19042.804], locale es-ES)
• Flutter version 1.26.0-17.5.pre at <PATH>
• Framework revision 1fe38dcb5f (3 days ago), 2021-02-10 16:25:47 -0800
• Engine revision d4453f6018
• Dart version 2.12.0 (build 2.12.0-259.9.beta)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
• Android SDK at <PATH>
• Platform android-30, build-tools 30.0.2
• Java binary at: <PATH>
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
• All Android licenses accepted.
[✓] Android Studio (version 4.1.0)
• Android Studio at <PATH>
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
[✓] VS Code (version 1.53.2)
• VS Code at <PATH>
• Flutter extension version 3.19.0
[✓] Connected device (1 available)
• <DEVICE> • android-arm • Android 8.1.0 (API 27)
• No issues found!
/pubscpec.yaml:
name: example_app
description: A new Flutter application.
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: ">=2.12.0-0 <3.0.0"
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl: ^0.17.0-nullsafety.2
cupertino_icons: ^1.0.1
dev_dependencies:
flutter_test:
sdk: flutter
integration_test:
sdk: flutter
flutter:
uses-material-design: true
generate: true
/lib/main.飞镖:
import 'package:example_app/ui/screen/home_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
// TODO: uncomment the line below after codegen
// import 'package:flutter_gen/gen_l10n/app_localizations.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage('Flutter Demo Home Page'),
localizationsDelegates: [
// ... app-specific localization delegate[s] here
// TODO: uncomment the line below after codegen
// AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', ''), // English, no country code
const Locale('es', ''), // Spanish, no country code
],
);
}
}
/lib/l10n.yaml:
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
lib/l10n/app_en.arb
{
"helloWorld": "Hello World!"
}
lib/l10n/app_es.arb
{
"helloWorld": "Hola mundo!"
}
这是我的错误,将 /lib/l10n.yaml/
移动到 /l10n.yaml/
解决了问题。
已回答perqin。
我用 Android Studio 创建了一个新的 Flutter 项目。我正在使用 null-safety 并且我是 运行 物理设备上的应用程序,但 code-generation 不工作, flutter_gen 文件夹里面没有 .dart_tool 文件夹只有 flutter_build文件夹。
我遵循了Flutter's official Internationalizing documentation
中的步骤我没有使用 Flutter Intl 插件,因为它不支持 null-safety(我知道你可以制作一个脚本来添加旧的 dart 版本每个生成文件的顶部作为解决方法)
flutter doctor命令输出:
[✓] Flutter (Channel beta, 1.26.0-17.5.pre, on Microsoft Windows [Versión 10.0.19042.804], locale es-ES)
• Flutter version 1.26.0-17.5.pre at <PATH>
• Framework revision 1fe38dcb5f (3 days ago), 2021-02-10 16:25:47 -0800
• Engine revision d4453f6018
• Dart version 2.12.0 (build 2.12.0-259.9.beta)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
• Android SDK at <PATH>
• Platform android-30, build-tools 30.0.2
• Java binary at: <PATH>
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
• All Android licenses accepted.
[✓] Android Studio (version 4.1.0)
• Android Studio at <PATH>
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
[✓] VS Code (version 1.53.2)
• VS Code at <PATH>
• Flutter extension version 3.19.0
[✓] Connected device (1 available)
• <DEVICE> • android-arm • Android 8.1.0 (API 27)
• No issues found!
/pubscpec.yaml:
name: example_app
description: A new Flutter application.
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: ">=2.12.0-0 <3.0.0"
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl: ^0.17.0-nullsafety.2
cupertino_icons: ^1.0.1
dev_dependencies:
flutter_test:
sdk: flutter
integration_test:
sdk: flutter
flutter:
uses-material-design: true
generate: true
/lib/main.飞镖:
import 'package:example_app/ui/screen/home_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
// TODO: uncomment the line below after codegen
// import 'package:flutter_gen/gen_l10n/app_localizations.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage('Flutter Demo Home Page'),
localizationsDelegates: [
// ... app-specific localization delegate[s] here
// TODO: uncomment the line below after codegen
// AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', ''), // English, no country code
const Locale('es', ''), // Spanish, no country code
],
);
}
}
/lib/l10n.yaml:
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
lib/l10n/app_en.arb
{
"helloWorld": "Hello World!"
}
lib/l10n/app_es.arb
{
"helloWorld": "Hola mundo!"
}
这是我的错误,将 /lib/l10n.yaml/
移动到 /l10n.yaml/
解决了问题。
已回答perqin。