The first step in the ios project is to set the locale in the project info

  • Open Xcode for Mac and set it in info:

  • In Windows, open the project ios => Runner => info. plist and set (develop_language) to zh_CN

The second step is first added under Pubspec.yaml dependencies

Support flutter_localizations: SDKCopy the code

The third step to write a class inherits CupertinoLocalizations, I this class in the project named ChineseCupertinoLocalizations (that is, the third localizationsDelegates)

import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; class _CupertinoLocalizationsDelegate extends LocalizationsDelegate<CupertinoLocalizations> { const _CupertinoLocalizationsDelegate(); @override bool isSupported(Locale locale) => locale.languageCode == 'zh'; @override Future<CupertinoLocalizations> load(Locale locale) => ChineseCupertinoLocalizations.load(locale); @override bool shouldReload(_CupertinoLocalizationsDelegate old) => false; @override String toString() => 'DefaultCupertinoLocalizations.delegate(zh_CH)'; } /// US English strings for the cupertino widgets. class ChineseCupertinoLocalizations implements CupertinoLocalizations { /// Constructs an object that defines the cupertino widgets' localized strings /// for US English (only). /// /// [LocalizationsDelegate] implementations typically call the static [load] /// function, rather than constructing this class directly. ChineseCupertinoLocalizations(Locale local); The static const a List < String > _shortWeekdays = < String > [' Monday ', 'on Tuesday, Wednesday, Thursday, Friday, Saturday,' on Sunday,]; static const List<String> _shortMonths = <String>[ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', ]; Static const a List < String > _months = < String > [' 01 month ', '02 month', '03 month', '04 month', 'may' and '6 months', 'July', 'August', '09', 'in October, 'November ',' December ',]; @override String datePickerYear(int yearIndex) => yearIndex.toString() + "yearIndex "; @override String datePickerMonth(int monthIndex) => _months[monthIndex - 1]; @override String datePickerDayOfMonth(int dayIndex) => dayindex. toString() + "day "; @override String datePickerHour(int hour) => hour.toString(); @ override String datePickerHourSemanticsLabel (int hour) = > hour. The toString () + "hour"; @override String datePickerMinute(int minute) => minute.toString().padLeft(2, '0'); @ override String datePickerMinuteSemanticsLabel (int minute) {return '1 minute; } @override String datePickerMediumDate(DateTime date) { return '${_shortWeekdays[date.weekday - DateTime.monday]} ' '${_shortMonths[date.month - DateTime.january]} ' '${date.day.toString().padRight(2)}'; } @override DatePickerDateOrder get datePickerDateOrder => DatePickerDateOrder.ymd; @override DatePickerDateTimeOrder get datePickerDateTimeOrder => DatePickerDateTimeOrder.date_time_dayPeriod; @override String get anteMeridiemAbbreviation => 'AM'; @override String get postMeridiemAbbreviation => 'PM'; @override String get alertDialogLabel => 'Prompt message '; @override String timerPickerHour(int hour) => hour.toString(); @override String timerPickerMinute(int minute) => minute.toString(); @override String timerPickerSecond(int second) => second.toString(); @override String timerPickerHourLabel(int hour) => 'when '; @override String timerPickerMinuteLabel(int minute) => '分'; @override String timerPickerSecondLabel(int second) => 'second '; @override String get cutButtonLabel => 'cut '; @override String get copyButtonLabel => 'copy '; @override String get pasteButtonLabel => 'paste '; @override String get selectAllButtonLabel => 'all '; /// Creates an object that provides US English resource values for the /// cupertino library widgets. /// /// The [locale] parameter is ignored. /// /// This method is typically used to create a [LocalizationsDelegate]. static Future<CupertinoLocalizations> load(Locale locale) { return SynchronousFuture<CupertinoLocalizations>(ChineseCupertinoLocalizations(locale)); } /// A [LocalizationsDelegate] that uses [DefaultCupertinoLocalizations.load] /// to create an instance of this class. static const LocalizationsDelegate<CupertinoLocalizations> delegate = _CupertinoLocalizationsDelegate(); @override // TODO: implement todayLabel String get todayLabel => null; }Copy the code

Step 4 Add it to the file in main

List<Locale> an = [ const Locale('zh', 'CH'), const Locale('en', 'US'), ]; List<Locale> ios = [ const Locale('en', 'US'), const Locale('zh', 'CH'), ]; Void main() {runApp(MaterialApp(title: 'Like !!!! ', // Internationalization supports localizationsDelegates: [ GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, ChineseCupertinoLocalizations.delegate, ], supportedLocales: Platform.isIOS ? ios : an,Copy the code

Restart your project here! Complete the cough up!

Easy to like 👍 good habit!! Thanks for your support!