Flutter开发之——国际化支持,flutter面试题2020

static AppLocalizations? of(BuildContext context) {

return Localizations.of(context, AppLocalizations);

}

static Map> _localizedValues = {

‘en’: {

‘title’: ‘Hello World’,

},

‘zh’: {

‘title’: ‘你好’,

},

};

String? get title {

return _localizedValues[locale.languageCode]?[‘title’];

}

}

说明:这里只是演示了title的国际化

2.4 设置用于加载语言的Delegate

class AppLocalizationsDelegate extends LocalizationsDelegate {

const AppLocalizationsDelegate();

@override

bool isSupported(Locale locale) => [‘en’, ‘zh’].contains(locale.languageCode);

@override

Future load(Locale locale) {

return SynchronousFuture(AppLocalizations(locale));

}

@override

bool shouldReload(AppLocalizationsDelegate old) => false;

}

2.5 将此 Delegate 添加到 MaterialApp

MaterialApp(

title: ‘Flutter IntlApp’,

localizationsDelegates: [

AppLocalizationsDelegate(),

],

supportedLocales: [

const Locale(‘zh’),

const Locale(‘en’),

],

home: _HomePage(),

)

2.6 整体的代码

import ‘package:flutter/foun

你可能感兴趣的:(程序员,面试,移动开发,android)