主要用到3個庫flutter_localizations
intl
intl_translation
ios
dependencies:
flutter_localizations:
sdk: flutter
intl: 0.15.7
dev_dependencies:
intl_translation: 0.17.4
複製代碼
return MaterialApp(
onGenerateTitle: (BuildContext context) =>
AppLocalizations.of(context).title,
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: routes.buildPage('login', null),
localizationsDelegates: [
// ... app-specific localization delegate[s] her
AppLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
// 支持語言設置
const Locale('en', 'US'), // English
const Locale('zh', 'CH'), // Chinese
// ... other locales the app supports
],
onGenerateRoute: (RouteSettings settings) {
return MaterialPageRoute<Object>(builder: (BuildContext context) {
return routes.buildPage(settings.name, settings.arguments);
});
},
);
複製代碼
Lib/localizations.dart
git
import 'package:flutter/material.dart';
// import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:intl/intl.dart';
import 'l10n/messages_all.dart';
class AppLocalizations {
static Future<AppLocalizations> load(Locale locale) {
final String name =
locale.countryCode.isEmpty ? locale.languageCode : locale.toString();
final String localeName = Intl.canonicalizedLocale(name);
return initializeMessages(localeName).then((_) {
Intl.defaultLocale = localeName;
print(
'the local code of $localeName ${locale.countryCode.isEmpty} ${locale}');
return AppLocalizations();
});
}
static AppLocalizations of(BuildContext context) {
return Localizations.of<AppLocalizations>(context, AppLocalizations);
}
String get title {
return Intl.message(
'Hello World',
name: 'title',
desc: 'Title for the Demo application',
);
}
}
class AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
const AppLocalizationsDelegate();
@override
bool isSupported(Locale locale) => ['en', 'zh'].contains(locale.languageCode);
@override
Future<AppLocalizations> load(Locale locale) => AppLocalizations.load(locale);
@override
bool shouldReload(AppLocalizationsDelegate old) => false;
}
複製代碼
導出命令github
flutter pub pub run intl_translation:extract_to_arb --output-dir=lib/l10n lib/localizations.dart
複製代碼
導出的文件 l10n/intl_messages.arbjson
{
"@@last_modified": "2019-04-28T15:58:14.536252",
"title": "Hello World",
"@title": {
"description": "Title for the Demo application",
"type": "text",
"placeholders": {}
}
}
複製代碼
l10n/intl_en.arbbash
{
"@@last_modified": "2019-04-28T15:58:14.536252",
"title": "Hello World",
"@title": {
"description": "Title for the application",
"type": "text",
"placeholders": {}
}
}
複製代碼
l10n/intl_zh.arbapp
{
"@@last_modified": "2019-04-28T15:58:14.536252",
"title": "你好世界",
"@title": {
"description": "Title for the application",
"type": "text",
"placeholders": {}
}
}
複製代碼
命令dom
flutter pub pub run intl_translation:generate_from_arb --output-dir=lib/l10n \
--no-use-deferred-loading lib/localizations.dart lib/l10n/intl_*.arb
複製代碼
import 'localizations.dart';
...
AppLocalizations.of(context).title
複製代碼
打開ios/Runner.xcworkspace
找到Runner的Info.plist
ide
添加一個key爲Localizations
value爲你支持的語言ui
good luckspa