简单本地化] [警告]未找到本地化密钥<…

发布于 2025-02-12 07:34:00 字数 2491 浏览 1 评论 0原文

我正在尝试将简单的本地化软件包添加到我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

源来凯始玺欢你 2025-02-19 07:34:00

我想你错过了一些步骤...
首先,您需要创建一个具有名称翻译的资产文件夹,并使用.json扩展名来创建您的目标翻译语言,如下面

assets
└── translations
    ├── fr-FR.json
    └── en-US.json
    └── it-IT.json

flutter:
  assets:
    - assets/translations/

示例

。 en-us.json:

{
"calculator": "Calculator",
"flour": "Flour",
"settings": "Settings",
"change_language": "Change language",
"choose_a_language": "Choose a language",
"flour_achieve": "Flour Strength to achieve",
"enter_W": "Enter (W)"
}

和fr-fr.json:

{
"calculator": "Calculatrice",
"flour": "Farine",
"settings": "Reglages",
"change_language": "Changer de Langue",
"choose_a_language": "Choisir une Langue",
"flour_achieve": "Farine Force à atteindre",
"enter_W": "Entrez (W)"
}

最后,您的文字小部件看起来像这样:

header:  Text('choose_a_language').tr(),

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

assets
└── translations
    ├── fr-FR.json
    └── en-US.json
    └── it-IT.json

Declare your assets localization directory in pubspec.yaml:

flutter:
  assets:
    - assets/translations/

every json file must contain fixed key and value contain translated words :

for example en-US.json :

{
"calculator": "Calculator",
"flour": "Flour",
"settings": "Settings",
"change_language": "Change language",
"choose_a_language": "Choose a language",
"flour_achieve": "Flour Strength to achieve",
"enter_W": "Enter (W)"
}

and fr-FR.json :

{
"calculator": "Calculatrice",
"flour": "Farine",
"settings": "Reglages",
"change_language": "Changer de Langue",
"choose_a_language": "Choisir une Langue",
"flour_achieve": "Farine Force à atteindre",
"enter_W": "Entrez (W)"
}

and finally your text widget will look like this:

header:  Text('choose_a_language').tr(),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文