flutter 国际化(组件英文转中文)

ios 必须:
flutter 国际化(组件英文转中文)_第1张图片

然后:
flutter 国际化(组件英文转中文)_第2张图片
flutter_localizations:
sdk: flutter

MaterialApp中 增加如下代码:
localizationsDelegates: [
ChineseCupertinoLocalizations.delegate, // 这里加上这个,是自定义的delegate
DefaultCupertinoLocalizations.delegate, // 这个截止目前只包含英文
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale(‘en’, ‘US’),
const Locale(‘zh’, ‘CH’),
],

ChineseCupertinoLocalizations代码如下:

import ‘package:flutter/cupertino.dart’;
import ‘package:flutter/foundation.dart’;
import ‘package:flutter/material.dart’;
import ‘package:flutter_localizations/flutter_localizations.dart’;

class ChineseCupertinoLocalizations implements CupertinoLocalizations {
final materialDelegate = GlobalMaterialLocalizations.delegate;
final widgetsDelegate = GlobalWidgetsLocalizations.delegate;
final local = const Locale(‘zh’);

MaterialLocalizations ml;

Future init() async {
ml = await materialDelegate.load(local);
print(ml.pasteButtonLabel);
}

@override
String get alertDialogLabel => ml.alertDialogLabel;

@override
String get anteMeridiemAbbreviation => ml.anteMeridiemAbbreviation;

@override
String get copyButtonLabel => ml.copyButtonLabel;

@override
String get cutButtonLabel => ml.cutButtonLabel;

@override
DatePickerDateOrder get datePickerDateOrder => DatePickerDateOrder.mdy;

@override
DatePickerDateTimeOrder get datePickerDateTimeOrder =>
DatePickerDateTimeOrder.date_time_dayPeriod;

@override
String datePickerDayOfMonth(int dayIndex) {
return dayIndex.toString();
}

@override
String datePickerHour(int hour) {
return hour.toString().padLeft(2, “0”);
}

@override
String datePickerHourSemanticsLabel(int hour) {
return “$hour” + “时”;
}

@override
String datePickerMediumDate(DateTime date) {
return ml.formatMediumDate(date);
}

@override
String datePickerMinute(int minute) {
return minute.toString().padLeft(2, ‘0’);
}

@override
String datePickerMinuteSemanticsLabel(int minute) {
return “$minute” + “分”;
}

@override
String datePickerMonth(int monthIndex) {
return “$monthIndex”;
}

@override
String datePickerYear(int yearIndex) {
return yearIndex.toString();
}

@override
String get pasteButtonLabel => ml.pasteButtonLabel;

@override
String get postMeridiemAbbreviation => ml.postMeridiemAbbreviation;

@override
String get selectAllButtonLabel => ml.selectAllButtonLabel;

@override
String timerPickerHour(int hour) {
return hour.toString().padLeft(2, “0”);
}

@override
String timerPickerHourLabel(int hour) {
return “$hour”.toString().padLeft(2, “0”) + “时”;
}

@override
String timerPickerMinute(int minute) {
return minute.toString().padLeft(2, “0”);
}

@override
String timerPickerMinuteLabel(int minute) {
return minute.toString().padLeft(2, “0”) + “分”;
}

@override
String timerPickerSecond(int second) {
return second.toString().padLeft(2, “0”);
}

@override
String timerPickerSecondLabel(int second) {
return second.toString().padLeft(2, “0”) + “秒”;
}

static const LocalizationsDelegate delegate =
_ChineseDelegate();

static Future load(Locale locale) async {
var localizaltions = ChineseCupertinoLocalizations();
await localizaltions.init();
return SynchronousFuture(localizaltions);
}
}

class _ChineseDelegate extends LocalizationsDelegate {
const _ChineseDelegate();

@override
bool isSupported(Locale locale) {
return locale.languageCode == ‘zh’;
}

@override
Future load(Locale locale) {
return ChineseCupertinoLocalizations.load(locale);
}

@override
bool shouldReload(LocalizationsDelegate old) {
return false;
}
}

详细错误分析以及过程请见 :
https://www.kikt.top/posts/flutter/framework/cupertino-paste-tooltip/

你可能感兴趣的:(flutter成长)