|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:flutter/foundation.dart'; |
| 4 | +import 'package:flutter/widgets.dart'; |
| 5 | +import 'package:flutter_localizations/flutter_localizations.dart'; |
| 6 | +import 'package:intl/intl.dart' as intl; |
| 7 | + |
| 8 | +import 'deer_localizations_en.dart'; |
| 9 | +import 'deer_localizations_zh.dart'; |
| 10 | + |
| 11 | +// ignore_for_file: type=lint |
| 12 | + |
| 13 | +/// Callers can lookup localized strings with an instance of DeerLocalizations |
| 14 | +/// returned by `DeerLocalizations.of(context)`. |
| 15 | +/// |
| 16 | +/// Applications need to include `DeerLocalizations.delegate()` in their app's |
| 17 | +/// `localizationDelegates` list, and the locales they support in the app's |
| 18 | +/// `supportedLocales` list. For example: |
| 19 | +/// |
| 20 | +/// ```dart |
| 21 | +/// import 'l10n/deer_localizations.dart'; |
| 22 | +/// |
| 23 | +/// return MaterialApp( |
| 24 | +/// localizationsDelegates: DeerLocalizations.localizationsDelegates, |
| 25 | +/// supportedLocales: DeerLocalizations.supportedLocales, |
| 26 | +/// home: MyApplicationHome(), |
| 27 | +/// ); |
| 28 | +/// ``` |
| 29 | +/// |
| 30 | +/// ## Update pubspec.yaml |
| 31 | +/// |
| 32 | +/// Please make sure to update your pubspec.yaml to include the following |
| 33 | +/// packages: |
| 34 | +/// |
| 35 | +/// ```yaml |
| 36 | +/// dependencies: |
| 37 | +/// # Internationalization support. |
| 38 | +/// flutter_localizations: |
| 39 | +/// sdk: flutter |
| 40 | +/// intl: any # Use the pinned version from flutter_localizations |
| 41 | +/// |
| 42 | +/// # Rest of dependencies |
| 43 | +/// ``` |
| 44 | +/// |
| 45 | +/// ## iOS Applications |
| 46 | +/// |
| 47 | +/// iOS applications define key application metadata, including supported |
| 48 | +/// locales, in an Info.plist file that is built into the application bundle. |
| 49 | +/// To configure the locales supported by your app, you’ll need to edit this |
| 50 | +/// file. |
| 51 | +/// |
| 52 | +/// First, open your project’s ios/Runner.xcworkspace Xcode workspace file. |
| 53 | +/// Then, in the Project Navigator, open the Info.plist file under the Runner |
| 54 | +/// project’s Runner folder. |
| 55 | +/// |
| 56 | +/// Next, select the Information Property List item, select Add Item from the |
| 57 | +/// Editor menu, then select Localizations from the pop-up menu. |
| 58 | +/// |
| 59 | +/// Select and expand the newly-created Localizations item then, for each |
| 60 | +/// locale your application supports, add a new item and select the locale |
| 61 | +/// you wish to add from the pop-up menu in the Value field. This list should |
| 62 | +/// be consistent with the languages listed in the DeerLocalizations.supportedLocales |
| 63 | +/// property. |
| 64 | +abstract class DeerLocalizations { |
| 65 | + DeerLocalizations(String locale) : localeName = intl.Intl.canonicalizedLocale(locale.toString()); |
| 66 | + |
| 67 | + final String localeName; |
| 68 | + |
| 69 | + static DeerLocalizations? of(BuildContext context) { |
| 70 | + return Localizations.of<DeerLocalizations>(context, DeerLocalizations); |
| 71 | + } |
| 72 | + |
| 73 | + static const LocalizationsDelegate<DeerLocalizations> delegate = _DeerLocalizationsDelegate(); |
| 74 | + |
| 75 | + /// A list of this localizations delegate along with the default localizations |
| 76 | + /// delegates. |
| 77 | + /// |
| 78 | + /// Returns a list of localizations delegates containing this delegate along with |
| 79 | + /// GlobalMaterialLocalizations.delegate, GlobalCupertinoLocalizations.delegate, |
| 80 | + /// and GlobalWidgetsLocalizations.delegate. |
| 81 | + /// |
| 82 | + /// Additional delegates can be added by appending to this list in |
| 83 | + /// MaterialApp. This list does not have to be used at all if a custom list |
| 84 | + /// of delegates is preferred or required. |
| 85 | + static const List<LocalizationsDelegate<dynamic>> localizationsDelegates = <LocalizationsDelegate<dynamic>>[ |
| 86 | + delegate, |
| 87 | + GlobalMaterialLocalizations.delegate, |
| 88 | + GlobalCupertinoLocalizations.delegate, |
| 89 | + GlobalWidgetsLocalizations.delegate, |
| 90 | + ]; |
| 91 | + |
| 92 | + /// A list of this localizations delegate's supported locales. |
| 93 | + static const List<Locale> supportedLocales = <Locale>[ |
| 94 | + Locale('en'), |
| 95 | + Locale('zh') |
| 96 | + ]; |
| 97 | + |
| 98 | + /// Title for the application |
| 99 | + /// |
| 100 | + /// In en, this message translates to: |
| 101 | + /// **'Flutter Deer'** |
| 102 | + String get title; |
| 103 | + |
| 104 | + /// Title for the Login page |
| 105 | + /// |
| 106 | + /// In en, this message translates to: |
| 107 | + /// **'Verification Code Login'** |
| 108 | + String get verificationCodeLogin; |
| 109 | + |
| 110 | + /// Password Login |
| 111 | + /// |
| 112 | + /// In en, this message translates to: |
| 113 | + /// **'Password Login'** |
| 114 | + String get passwordLogin; |
| 115 | + |
| 116 | + /// Login |
| 117 | + /// |
| 118 | + /// In en, this message translates to: |
| 119 | + /// **'Login'** |
| 120 | + String get login; |
| 121 | + |
| 122 | + /// Forgot Password |
| 123 | + /// |
| 124 | + /// In en, this message translates to: |
| 125 | + /// **'Forgot Password'** |
| 126 | + String get forgotPasswordLink; |
| 127 | + |
| 128 | + /// Please enter the password |
| 129 | + /// |
| 130 | + /// In en, this message translates to: |
| 131 | + /// **'Please enter the password'** |
| 132 | + String get inputPasswordHint; |
| 133 | + |
| 134 | + /// Please input username |
| 135 | + /// |
| 136 | + /// In en, this message translates to: |
| 137 | + /// **'Please input username'** |
| 138 | + String get inputUsernameHint; |
| 139 | + |
| 140 | + /// No account yet? Register now |
| 141 | + /// |
| 142 | + /// In en, this message translates to: |
| 143 | + /// **'No account yet? Register now'** |
| 144 | + String get noAccountRegisterLink; |
| 145 | + |
| 146 | + /// Register |
| 147 | + /// |
| 148 | + /// In en, this message translates to: |
| 149 | + /// **'Register'** |
| 150 | + String get register; |
| 151 | + |
| 152 | + /// Open your account |
| 153 | + /// |
| 154 | + /// In en, this message translates to: |
| 155 | + /// **'Open your account'** |
| 156 | + String get openYourAccount; |
| 157 | + |
| 158 | + /// Please enter phone number |
| 159 | + /// |
| 160 | + /// In en, this message translates to: |
| 161 | + /// **'Please enter phone number'** |
| 162 | + String get inputPhoneHint; |
| 163 | + |
| 164 | + /// Please enter verification code |
| 165 | + /// |
| 166 | + /// In en, this message translates to: |
| 167 | + /// **'Please enter verification code'** |
| 168 | + String get inputVerificationCodeHint; |
| 169 | + |
| 170 | + /// Please input valid mobile phone number |
| 171 | + /// |
| 172 | + /// In en, this message translates to: |
| 173 | + /// **'Please input valid mobile phone number'** |
| 174 | + String get inputPhoneInvalid; |
| 175 | + |
| 176 | + /// Not really sent, just log in! |
| 177 | + /// |
| 178 | + /// In en, this message translates to: |
| 179 | + /// **'Not really sent, just log in!'** |
| 180 | + String get verificationButton; |
| 181 | + |
| 182 | + /// Get verification code |
| 183 | + /// |
| 184 | + /// In en, this message translates to: |
| 185 | + /// **'Get code'** |
| 186 | + String get getVerificationCode; |
| 187 | + |
| 188 | + /// No description provided for @confirm. |
| 189 | + /// |
| 190 | + /// In en, this message translates to: |
| 191 | + /// **'Confirm'** |
| 192 | + String get confirm; |
| 193 | + |
| 194 | + /// Reset login password |
| 195 | + /// |
| 196 | + /// In en, this message translates to: |
| 197 | + /// **'Reset Login Password'** |
| 198 | + String get resetLoginPassword; |
| 199 | + |
| 200 | + /// Registered Tips |
| 201 | + /// |
| 202 | + /// In en, this message translates to: |
| 203 | + /// **'Unregistered mobile phone number, please '** |
| 204 | + String get registeredTips; |
| 205 | +} |
| 206 | + |
| 207 | +class _DeerLocalizationsDelegate extends LocalizationsDelegate<DeerLocalizations> { |
| 208 | + const _DeerLocalizationsDelegate(); |
| 209 | + |
| 210 | + @override |
| 211 | + Future<DeerLocalizations> load(Locale locale) { |
| 212 | + return SynchronousFuture<DeerLocalizations>(lookupDeerLocalizations(locale)); |
| 213 | + } |
| 214 | + |
| 215 | + @override |
| 216 | + bool isSupported(Locale locale) => <String>['en', 'zh'].contains(locale.languageCode); |
| 217 | + |
| 218 | + @override |
| 219 | + bool shouldReload(_DeerLocalizationsDelegate old) => false; |
| 220 | +} |
| 221 | + |
| 222 | +DeerLocalizations lookupDeerLocalizations(Locale locale) { |
| 223 | + |
| 224 | + |
| 225 | + // Lookup logic when only language code is specified. |
| 226 | + switch (locale.languageCode) { |
| 227 | + case 'en': return DeerLocalizationsEn(); |
| 228 | + case 'zh': return DeerLocalizationsZh(); |
| 229 | + } |
| 230 | + |
| 231 | + throw FlutterError( |
| 232 | + 'DeerLocalizations.delegate failed to load unsupported locale "$locale". This is likely ' |
| 233 | + 'an issue with the localizations generation tool. Please file an issue ' |
| 234 | + 'on GitHub with a reproducible sample app and the gen-l10n configuration ' |
| 235 | + 'that was used.' |
| 236 | + ); |
| 237 | +} |
0 commit comments