Cha*_*lie 5 localization flutter
起初我将本教程中的l10n 实现到 Flutter 的模板项目文件中,并且成功了。之后,我尝试将这些MyHomePage类移动到一个名为home.dart. 它停止工作,因为当我调用Translations.of(context)它时返回null. BuildContextwhen insidemain.dart和 有home.dart什么区别?
本地化.dart
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class Translations {
final Locale locale;
Map<String, dynamic> _messages;
Translations(this.locale);
static Translations of(BuildContext context) => Localizations.of<Translations>(context, Translations);
Future<bool> load() async {
String fileName = 'lang/${locale.languageCode}.json';
String data = await rootBundle.loadString(fileName);
_messages = json.decode(data);
return true;
}
String get(String key) => _messages[key] ?? "** $key not found";
}
class TranslationsDelegate extends LocalizationsDelegate<Translations> {
@override
bool isSupported(Locale locale) => ['en', 'id'].contains(locale.languageCode);
@override
Future<Translations> load(Locale locale) async {
Translations translations = new Translations(locale);
await translations.load();
return translations;
}
@override
bool shouldReload(LocalizationsDelegate<Translations> old) => false;
}
Run Code Online (Sandbox Code Playgroud)
main.dart
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'components/localization.dart';
import 'components/theme.dart';
import 'views/home.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
onGenerateTitle: (BuildContext context) {
print(context);
return Translations.of(context).get('app_name');
},
theme: appTheme,
home: MyHomePage(
title: "Coba",
),
debugShowCheckedModeBanner: false,
localizationsDelegates: [
TranslationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
supportedLocales: [
Locale("en", ""),
Locale("id", ""),
],
);
}
}
Run Code Online (Sandbox Code Playgroud)
家.dart
import 'package:flutter/material.dart';
import 'package:kpop_idol/components/localization.dart';
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
Translations.of(context).get('app_name'),
),
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
不要在文件中使用相对导入 lib/main.dart
import 'components/localization.dart';
import 'components/theme.dart';
import 'views/home.dart';
Run Code Online (Sandbox Code Playgroud)
应该
import 'package:my_package/components/localization.dart';
import 'package:my_package/components/theme.dart';
import 'package:my_package/views/home.dart';
Run Code Online (Sandbox Code Playgroud)
您可以点赞并关注https://github.com/dart-lang/sdk/issues/33076
| 归档时间: |
|
| 查看次数: |
3430 次 |
| 最近记录: |