我是 Flutter 本地化新手。我正在尝试在我的 flutter IOS 应用程序中实现它,但出现以下错误:
在 null 上调用了“translate”方法。 接收者:空 尝试调用:翻译(“菜单”)
我希望有人能帮我解决这个问题 - 我已经尝试了几个小时但无法解决问题 - 希望你能帮忙
主要:
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:xobrew_app/services/app_localizations.dart';
class forside extends StatefulWidget {
@override
_forsideState createState() => _forsideState();
}
class _forsideState extends State<forside> {
@override
Widget build(BuildContext context) {
return MaterialApp(
supportedLocales: [
Locale('en', 'US'),
Locale('da', 'DK'),
],
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
localeResolutionCallback: (locale, supportedLocales) {
if (locale == null) {
return supportedLocales.first;
}
for (var supportedLocales in supportedLocales ) {
if(supportedLocales.languageCode == locale.languageCode &&
supportedLocales.countryCode == locale.countryCode) {
return supportedLocales;
}
}
return supportedLocales.first;
},
home: Scaffold(
appBar: AppBar(
title: Text('Brew App'),
backgroundColor: Colors.green[800],
),
backgroundColor: Colors.green[100],
body: Container(
padding: EdgeInsets.all(30.0),
child: GridView.count(
crossAxisCount: 2,
children: <Widget>[
Card(
margin: EdgeInsets.all(8.0),
child: InkWell(
onTap: (){},
splashColor: Colors.green,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(Icons.add_box, size: 70.0, color: Colors.orange,),
Text("Calculator", style: TextStyle(fontSize: 17.0)),
],
),
),
)
),
Card(
margin: EdgeInsets.all(8.0),
child: InkWell(
onTap: (){},
splashColor: Colors.green,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(Icons.local_library, size: 70.0, color: Colors.blue,),
Text(
//"Knowledge",
AppLocalizations.of(context).translate('menu'),
style: TextStyle(fontSize: 17.0)),
],
),
),
)
),
],
),
),
),
);
}
}
应用程序本地化
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class AppLocalizations {
final Locale locale;
AppLocalizations(this.locale);
static AppLocalizations of (BuildContext context) {
return Localizations.of<AppLocalizations>(context, AppLocalizations);
}
static const LocalizationsDelegate<AppLocalizations> delegate =
_AppLocalizationsDelegate();
Map<String, String> _localizedStrings;
Future<bool> load() async {
// Load the language JSON file from the "lang" folder
String jsonString =
await rootBundle.loadString('lang/${locale.languageCode}.json');
//print(locale.languageCode);
//await rootBundle.loadString('lang/${locale.languageCode}.yaml');
Map<String, dynamic> jsonMap = json.decode(jsonString);
_localizedStrings = jsonMap.map((key, value){
//print (_localizedStrings);
//print (locale);
return MapEntry(key, value.toString());
});
return true;
}
String translate(String key) {
return _localizedStrings[key];
}
}
//LocalizationsDelegate is a factory for a set of localized resources
class _AppLocalizationsDelegate
extends LocalizationsDelegate<AppLocalizations> {
const _AppLocalizationsDelegate();
@override
bool isSupported(Locale locale) {
//Include all of your supported language codes here
return ['en', 'da'].contains(locale.languageCode);
}
@override
Future<AppLocalizations> load(Locale locale) async {
AppLocalizations localizations = AppLocalizations(locale);
await localizations.load();
return localizations;
}
@override
bool shouldReload(_AppLocalizationsDelegate old) => false;
}
您需要首先构建应用程序,即尝试运行该应用程序。这应该会在
{FLUTTER_PROJECT}/.dart_tool/flutter_gen/gen_l10n
中生成本地化字符串,如本 guide 中所述。如果没有生成的文件,应用程序将无法访问这些本地化字符串。
https://youtu.be/RAHiMQW7Jso?si=q07VQig45WFBkKmm 以上视频创建于 2024 年 这是一个在 flutter 应用程序中进行本地化的教程视频。它帮助我尝试了