简单本地化] [警告]未找到本地化密钥<…
我正在尝试将简单的本地化软件包添加到我的Flutter应用程序中。我已经遵循所有步骤,一一我不知道为什么会遇到错误: 简单本地化] [警告]未找到本地化密钥<…>
我在main.dart
中运行代码,并且我有3个按钮可以在我的Lanagingscreen.dart.dart
中选择一种语言。 >
Future <void> function() async {
WidgetsFlutterBinding.ensureInitialized();
await EasyLocalization.ensureInitialized();
runApp(
EasyLocalization(
supportedLocales: [Locale('en', 'US'), Locale('it', 'IT'), Locale('fr', 'FR')],
path: 'assets/translations', // <-- change the path of the translation files
fallbackLocale: Locale('en', 'US'),
assetLoader: CodegenLoader(),
child: MyLangApp(),
),
);
}
这是mylangapp:
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'LanguagesScreen.dart';
class MyLangApp extends StatefulWidget {
const MyLangApp({Key? key}) : super(key: key);
@override
State<MyLangApp> createState() => _MyLangAppState();
}
class _MyLangAppState extends State<MyLangApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
home: LanguagesScreen(),
);
}
}
这是我的语言
child: CupertinoFormSection.insetGrouped(
header: Text('choose_a_language'.tr().toString()),
children: [
TextButton(onPressed: () async {
await context.setLocale(Locale('en','US'));
print(context.supportedLocales);
},
child: Text('English', style: TextStyle(color: Colors.black87),))),
TextButton(onPressed: () async {
await context.setLocale(Locale('fr','FR'));
},
child: Text('French', style: TextStyle(color: Colors.black87),)),
TextButton(onPressed: () async {
await context.setLocale(Locale('it','IT'));
},
child: Text('Italian', style: TextStyle(color: Colors.black87),)),
]
这是我的语言文件:
{
"calculator": "Calculateur",
"flour": "Farine",
"settings": "Reglages",
"change_language": "Changer de Langue",
"choose_a_language": "Choisir une Langue",
"flour_achieve": "Flour Strenght to achieve",
"enter_W": "Entrez (W)"
}
I am trying to add Easy Localization package to my Flutter app. I have followed all steps, one by one, by i don't know why i get the error:
Easy Localization] [WARNING] Localization key not found<…>
I run the code in main.dart
, and i have 3 buttons to choose a language in my LanguageScreen.dart
this is my main.dart:
Future <void> function() async {
WidgetsFlutterBinding.ensureInitialized();
await EasyLocalization.ensureInitialized();
runApp(
EasyLocalization(
supportedLocales: [Locale('en', 'US'), Locale('it', 'IT'), Locale('fr', 'FR')],
path: 'assets/translations', // <-- change the path of the translation files
fallbackLocale: Locale('en', 'US'),
assetLoader: CodegenLoader(),
child: MyLangApp(),
),
);
}
this is MyLangApp:
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'LanguagesScreen.dart';
class MyLangApp extends StatefulWidget {
const MyLangApp({Key? key}) : super(key: key);
@override
State<MyLangApp> createState() => _MyLangAppState();
}
class _MyLangAppState extends State<MyLangApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
home: LanguagesScreen(),
);
}
}
This is my LanguageScrenn.dart
child: CupertinoFormSection.insetGrouped(
header: Text('choose_a_language'.tr().toString()),
children: [
TextButton(onPressed: () async {
await context.setLocale(Locale('en','US'));
print(context.supportedLocales);
},
child: Text('English', style: TextStyle(color: Colors.black87),))),
TextButton(onPressed: () async {
await context.setLocale(Locale('fr','FR'));
},
child: Text('French', style: TextStyle(color: Colors.black87),)),
TextButton(onPressed: () async {
await context.setLocale(Locale('it','IT'));
},
child: Text('Italian', style: TextStyle(color: Colors.black87),)),
]
This is my language file:
{
"calculator": "Calculateur",
"flour": "Farine",
"settings": "Reglages",
"change_language": "Changer de Langue",
"choose_a_language": "Choisir une Langue",
"flour_achieve": "Flour Strenght to achieve",
"enter_W": "Entrez (W)"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你错过了一些步骤...
首先,您需要创建一个具有名称翻译的资产文件夹,并使用.json扩展名来创建您的目标翻译语言,如下面
的
示例
。 en-us.json:
和fr-fr.json:
最后,您的文字小部件看起来像这样:
I think you missed some steps...
first you need to create an assets folder with name translations and create your target translation languages with .json extension like the example below
Declare your assets localization directory in pubspec.yaml:
every json file must contain fixed key and value contain translated words :
for example en-US.json :
and fr-FR.json :
and finally your text widget will look like this: