material_localizations.dart 28.9 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart' as intl;

9
import 'cupertino_localizations.dart';
10
import 'l10n/generated_material_localizations.dart';
11
import 'utils/date_localizations.dart' as util;
12 13
import 'widgets_localizations.dart';

14 15 16 17 18 19 20
/// Implementation of localized strings for the material widgets using the
/// `intl` package for date and time formatting.
///
/// ## Supported languages
///
/// This class supports locales with the following [Locale.languageCode]s:
///
21
/// {@macro flutter.localizations.material.languages}
22
///
23
/// This list is available programmatically via [kMaterialSupportedLanguages].
24 25
///
/// ## Sample code
26 27 28 29 30 31 32 33 34 35
///
/// To include the localizations provided by this class in a [MaterialApp],
/// add [GlobalMaterialLocalizations.delegates] to
/// [MaterialApp.localizationsDelegates], and specify the locales your
/// app supports with [MaterialApp.supportedLocales]:
///
/// ```dart
/// new MaterialApp(
///   localizationsDelegates: GlobalMaterialLocalizations.delegates,
///   supportedLocales: [
36 37
///     const Locale('en', 'US'), // American English
///     const Locale('he', 'IL'), // Israeli Hebrew
38 39 40 41 42 43
///     // ...
///   ],
///   // ...
/// )
/// ```
///
44 45 46 47 48 49
/// ## Overriding translations
///
/// To create a translation that's similar to an existing language's translation
/// but has slightly different strings, subclass the relevant translation
/// directly and then create a [LocalizationsDelegate<MaterialLocalizations>]
/// subclass to define how to load it.
50
///
51 52 53 54 55 56 57 58
/// Avoid subclassing an unrelated language (for example, subclassing
/// [MaterialLocalizationEn] and then passing a non-English `localeName` to the
/// constructor). Doing so will cause confusion for locale-specific behaviors;
/// in particular, translations that use the `localeName` for determining how to
/// pluralize will end up doing invalid things. Subclassing an existing
/// language's translations is only suitable for making small changes to the
/// existing strings. For providing a new language entirely, implement
/// [MaterialLocalizations] directly.
59 60 61 62
///
/// See also:
///
///  * The Flutter Internationalization Tutorial,
63
///    <https://flutter.dev/tutorials/internationalization/>.
64
///  * [DefaultMaterialLocalizations], which only provides US English translations.
65 66
abstract class GlobalMaterialLocalizations implements MaterialLocalizations {
  /// Initializes an object that defines the material widgets' localized strings
67 68
  /// for the given `locale`.
  ///
69 70 71 72 73 74
  /// The arguments are used for further runtime localization of data,
  /// specifically for selecting plurals, date and time formatting, and number
  /// formatting. They correspond to the following values:
  ///
  ///  1. The string that would be returned by [Intl.canonicalizedLocale] for
  ///     the locale.
75 76 77 78 79 80
  ///  2. The [DateFormat] for [formatYear].
  ///  3. The [DateFormat] for [formatShortDate].
  ///  4. The [DateFormat] for [formatMediumDate].
  ///  5. The [DateFormat] for [formatFullDate].
  ///  6. The [DateFormat] for [formatMonthYear].
  ///  7. The [DateFormat] for [formatShortMonthDay].
81
  ///  8. The [NumberFormat] for [formatDecimal] (also used by [formatHour] and
82
  ///     [formatTimeOfDay] when [timeOfDayFormat] doesn't use [HourFormat.HH]).
83
  ///  9. The [NumberFormat] for [formatHour] and the hour part of
84 85 86 87 88 89
  ///     [formatTimeOfDay] when [timeOfDayFormat] uses [HourFormat.HH], and for
  ///     [formatMinute] and the minute part of [formatTimeOfDay].
  ///
  /// The [narrowWeekdays] and [firstDayOfWeekIndex] properties use the values
  /// from the [intl.DateFormat] used by [formatFullDate].
  const GlobalMaterialLocalizations({
90 91 92 93 94 95 96 97 98 99
    required String localeName,
    required intl.DateFormat fullYearFormat,
    required intl.DateFormat compactDateFormat,
    required intl.DateFormat shortDateFormat,
    required intl.DateFormat mediumDateFormat,
    required intl.DateFormat longDateFormat,
    required intl.DateFormat yearMonthFormat,
    required intl.DateFormat shortMonthDayFormat,
    required intl.NumberFormat decimalFormat,
    required intl.NumberFormat twoDigitZeroPaddedFormat,
100
  }) : assert(localeName != null),
101
       _localeName = localeName,
102
       assert(fullYearFormat != null),
103
       _fullYearFormat = fullYearFormat,
104 105 106 107
       assert(compactDateFormat != null),
       _compactDateFormat = compactDateFormat,
       assert(shortDateFormat != null),
       _shortDateFormat = shortDateFormat,
108
       assert(mediumDateFormat != null),
109
       _mediumDateFormat = mediumDateFormat,
110
       assert(longDateFormat != null),
111
       _longDateFormat = longDateFormat,
112
       assert(yearMonthFormat != null),
113
       _yearMonthFormat = yearMonthFormat,
114 115
       assert(shortMonthDayFormat != null),
       _shortMonthDayFormat = shortMonthDayFormat,
116
       assert(decimalFormat != null),
117
       _decimalFormat = decimalFormat,
118
       assert(twoDigitZeroPaddedFormat != null),
119
       _twoDigitZeroPaddedFormat = twoDigitZeroPaddedFormat;
120 121

  final String _localeName;
122
  final intl.DateFormat _fullYearFormat;
123 124
  final intl.DateFormat _compactDateFormat;
  final intl.DateFormat _shortDateFormat;
125 126 127
  final intl.DateFormat _mediumDateFormat;
  final intl.DateFormat _longDateFormat;
  final intl.DateFormat _yearMonthFormat;
128
  final intl.DateFormat _shortMonthDayFormat;
129 130
  final intl.NumberFormat _decimalFormat;
  final intl.NumberFormat _twoDigitZeroPaddedFormat;
131 132

  @override
133
  String formatHour(TimeOfDay timeOfDay, { bool alwaysUse24HourFormat = false }) {
134
    switch (hourFormat(of: timeOfDayFormat(alwaysUse24HourFormat: alwaysUse24HourFormat))) {
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
      case HourFormat.HH:
        return _twoDigitZeroPaddedFormat.format(timeOfDay.hour);
      case HourFormat.H:
        return formatDecimal(timeOfDay.hour);
      case HourFormat.h:
        final int hour = timeOfDay.hourOfPeriod;
        return formatDecimal(hour == 0 ? 12 : hour);
    }
  }

  @override
  String formatMinute(TimeOfDay timeOfDay) {
    return _twoDigitZeroPaddedFormat.format(timeOfDay.minute);
  }

  @override
  String formatYear(DateTime date) {
    return _fullYearFormat.format(date);
  }

155 156 157 158 159 160 161 162 163 164
  @override
  String formatCompactDate(DateTime date) {
    return _compactDateFormat.format(date);
  }

  @override
  String formatShortDate(DateTime date) {
    return _shortDateFormat.format(date);
  }

165 166 167 168 169
  @override
  String formatMediumDate(DateTime date) {
    return _mediumDateFormat.format(date);
  }

170 171 172 173 174
  @override
  String formatFullDate(DateTime date) {
    return _longDateFormat.format(date);
  }

175 176 177 178 179
  @override
  String formatMonthYear(DateTime date) {
    return _yearMonthFormat.format(date);
  }

180 181 182 183 184 185
  @override
  String formatShortMonthDay(DateTime date) {
    return _shortMonthDayFormat.format(date);
  }

  @override
186
  DateTime? parseCompactDate(String? inputString) {
187
    try {
188
      return inputString != null ? _compactDateFormat.parseStrict(inputString) : null;
189 190 191 192 193
    } on FormatException {
      return null;
    }
  }

194 195
  @override
  List<String> get narrowWeekdays {
196
    return _longDateFormat.dateSymbols.NARROWWEEKDAYS;
197 198 199
  }

  @override
200
  int get firstDayOfWeekIndex => (_longDateFormat.dateSymbols.FIRSTDAYOFWEEK + 1) % 7;
201

202
  @override
203 204 205 206 207
  String formatDecimal(int number) {
    return _decimalFormat.format(number);
  }

  @override
208
  String formatTimeOfDay(TimeOfDay timeOfDay, { bool alwaysUse24HourFormat = false }) {
209 210 211 212 213 214 215 216
    // Not using intl.DateFormat for two reasons:
    //
    // - DateFormat supports more formats than our material time picker does,
    //   and we want to be consistent across time picker format and the string
    //   formatting of the time of day.
    // - DateFormat operates on DateTime, which is sensitive to time eras and
    //   time zones, while here we want to format hour and minute within one day
    //   no matter what date the day falls on.
217 218 219
    final String hour = formatHour(timeOfDay, alwaysUse24HourFormat: alwaysUse24HourFormat);
    final String minute = formatMinute(timeOfDay);
    switch (timeOfDayFormat(alwaysUse24HourFormat: alwaysUse24HourFormat)) {
220
      case TimeOfDayFormat.h_colon_mm_space_a:
221
        return '$hour:$minute ${_formatDayPeriod(timeOfDay)!}';
222 223
      case TimeOfDayFormat.H_colon_mm:
      case TimeOfDayFormat.HH_colon_mm:
224
        return '$hour:$minute';
225
      case TimeOfDayFormat.HH_dot_mm:
226
        return '$hour.$minute';
227
      case TimeOfDayFormat.a_space_h_colon_mm:
228
        return '${_formatDayPeriod(timeOfDay)!} $hour:$minute';
229
      case TimeOfDayFormat.frenchCanadian:
230
        return '$hour h $minute';
231 232 233
    }
  }

234
  String? _formatDayPeriod(TimeOfDay timeOfDay) {
235 236 237 238 239 240 241 242
    switch (timeOfDay.period) {
      case DayPeriod.am:
        return anteMeridiemAbbreviation;
      case DayPeriod.pm:
        return postMeridiemAbbreviation;
    }
  }

243 244 245 246 247 248
  /// The raw version of [dateRangeStartDateSemanticLabel], with `$formattedDate` verbatim
  /// in the string.
  @protected
  String get dateRangeStartDateSemanticLabelRaw;

  @override
249 250
  String dateRangeStartDateSemanticLabel(String formattedDate) {
    return dateRangeStartDateSemanticLabelRaw.replaceFirst(r'$fullDate', formattedDate);
251 252 253 254 255 256 257 258
  }

  /// The raw version of [dateRangeEndDateSemanticLabel], with `$fullDate` verbatim
  /// in the string.
  @protected
  String get dateRangeEndDateSemanticLabelRaw;

  @override
259 260
  String dateRangeEndDateSemanticLabel(String formattedDate) {
    return dateRangeEndDateSemanticLabelRaw.replaceFirst(r'$fullDate', formattedDate);
261 262
  }

263 264 265 266
  /// The raw version of [aboutListTileTitle], with `$applicationName` verbatim
  /// in the string.
  @protected
  String get aboutListTileTitleRaw;
267

268 269
  @override
  String aboutListTileTitle(String applicationName) {
270
    final String text = aboutListTileTitleRaw;
271 272 273
    return text.replaceFirst(r'$applicationName', applicationName);
  }

274 275 276 277 278 279 280 281 282 283 284
  /// The raw version of [pageRowsInfoTitle], with `$firstRow`, `$lastRow`' and
  /// `$rowCount` verbatim in the string, for the case where the value is
  /// approximate.
  @protected
  String get pageRowsInfoTitleApproximateRaw;

  /// The raw version of [pageRowsInfoTitle], with `$firstRow`, `$lastRow`' and
  /// `$rowCount` verbatim in the string, for the case where the value is
  /// precise.
  @protected
  String get pageRowsInfoTitleRaw;
285 286 287

  @override
  String pageRowsInfoTitle(int firstRow, int lastRow, int rowCount, bool rowCountIsApproximate) {
288
    String? text = rowCountIsApproximate ? pageRowsInfoTitleApproximateRaw : null;
289 290
    text ??= pageRowsInfoTitleRaw;
    assert(text != null, 'A $_localeName localization was not found for pageRowsInfoTitle or pageRowsInfoTitleApproximate');
291 292 293 294 295 296
    return text
      .replaceFirst(r'$firstRow', formatDecimal(firstRow))
      .replaceFirst(r'$lastRow', formatDecimal(lastRow))
      .replaceFirst(r'$rowCount', formatDecimal(rowCount));
  }

297 298 299 300
  /// The raw version of [tabLabel], with `$tabIndex` and `$tabCount` verbatim
  /// in the string.
  @protected
  String get tabLabelRaw;
301

302
  @override
303
  String tabLabel({ required int tabIndex, required int tabCount }) {
304 305
    assert(tabIndex >= 1);
    assert(tabCount >= 1);
306
    final String template = tabLabelRaw;
307 308
    return template
      .replaceFirst(r'$tabIndex', formatDecimal(tabIndex))
309
      .replaceFirst(r'$tabCount', formatDecimal(tabCount));
310 311
  }

312 313 314 315 316 317 318 319 320 321 322 323 324
  /// The "zero" form of [selectedRowCountTitle].
  ///
  /// This form is optional.
  ///
  /// See also:
  ///
  ///  * [Intl.plural], to which this form is passed.
  ///  * [selectedRowCountTitleOne], the "one" form
  ///  * [selectedRowCountTitleTwo], the "two" form
  ///  * [selectedRowCountTitleFew], the "few" form
  ///  * [selectedRowCountTitleMany], the "many" form
  ///  * [selectedRowCountTitleOther], the "other" form
  @protected
325
  String? get selectedRowCountTitleZero => null;
326 327 328 329 330 331 332 333 334 335 336 337 338 339

  /// The "one" form of [selectedRowCountTitle].
  ///
  /// This form is optional.
  ///
  /// See also:
  ///
  ///  * [Intl.plural], to which this form is passed.
  ///  * [selectedRowCountTitleZero], the "zero" form
  ///  * [selectedRowCountTitleTwo], the "two" form
  ///  * [selectedRowCountTitleFew], the "few" form
  ///  * [selectedRowCountTitleMany], the "many" form
  ///  * [selectedRowCountTitleOther], the "other" form
  @protected
340
  String? get selectedRowCountTitleOne => null;
341 342 343 344 345 346 347 348 349 350 351 352 353 354

  /// The "two" form of [selectedRowCountTitle].
  ///
  /// This form is optional.
  ///
  /// See also:
  ///
  ///  * [Intl.plural], to which this form is passed.
  ///  * [selectedRowCountTitleZero], the "zero" form
  ///  * [selectedRowCountTitleOne], the "one" form
  ///  * [selectedRowCountTitleFew], the "few" form
  ///  * [selectedRowCountTitleMany], the "many" form
  ///  * [selectedRowCountTitleOther], the "other" form
  @protected
355
  String? get selectedRowCountTitleTwo => null;
356 357 358 359 360 361 362 363 364 365 366 367 368 369

  /// The "few" form of [selectedRowCountTitle].
  ///
  /// This form is optional.
  ///
  /// See also:
  ///
  ///  * [Intl.plural], to which this form is passed.
  ///  * [selectedRowCountTitleZero], the "zero" form
  ///  * [selectedRowCountTitleOne], the "one" form
  ///  * [selectedRowCountTitleTwo], the "two" form
  ///  * [selectedRowCountTitleMany], the "many" form
  ///  * [selectedRowCountTitleOther], the "other" form
  @protected
370
  String? get selectedRowCountTitleFew => null;
371 372 373 374 375 376 377 378 379 380 381 382 383 384

  /// The "many" form of [selectedRowCountTitle].
  ///
  /// This form is optional.
  ///
  /// See also:
  ///
  ///  * [Intl.plural], to which this form is passed.
  ///  * [selectedRowCountTitleZero], the "zero" form
  ///  * [selectedRowCountTitleOne], the "one" form
  ///  * [selectedRowCountTitleTwo], the "two" form
  ///  * [selectedRowCountTitleFew], the "few" form
  ///  * [selectedRowCountTitleOther], the "other" form
  @protected
385
  String? get selectedRowCountTitleMany => null;
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401

  /// The "other" form of [selectedRowCountTitle].
  ///
  /// This form is required.
  ///
  /// See also:
  ///
  ///  * [Intl.plural], to which this form is passed.
  ///  * [selectedRowCountTitleZero], the "zero" form
  ///  * [selectedRowCountTitleOne], the "one" form
  ///  * [selectedRowCountTitleTwo], the "two" form
  ///  * [selectedRowCountTitleFew], the "few" form
  ///  * [selectedRowCountTitleMany], the "many" form
  @protected
  String get selectedRowCountTitleOther;

402 403
  @override
  String selectedRowCountTitle(int selectedRowCount) {
404
    return intl.Intl.pluralLogic(
405 406 407 408 409 410 411 412 413
      selectedRowCount,
      zero: selectedRowCountTitleZero,
      one: selectedRowCountTitleOne,
      two: selectedRowCountTitleTwo,
      few: selectedRowCountTitleFew,
      many: selectedRowCountTitleMany,
      other: selectedRowCountTitleOther,
      locale: _localeName,
    ).replaceFirst(r'$selectedRowCount', formatDecimal(selectedRowCount));
414 415
  }

416 417 418
  /// The format to use for [timeOfDayFormat].
  @protected
  TimeOfDayFormat get timeOfDayFormatRaw;
419

420 421 422 423 424 425 426 427 428 429 430 431 432 433
  /// The [TimeOfDayFormat] corresponding to one of the following supported
  /// patterns:
  ///
  ///  * `HH:mm`
  ///  * `HH.mm`
  ///  * `HH 'h' mm`
  ///  * `HH:mm น.`
  ///  * `H:mm`
  ///  * `h:mm a`
  ///  * `a h:mm`
  ///  * `ah:mm`
  ///
  /// See also:
  ///
434 435
  ///  * <http://demo.icu-project.org/icu-bin/locexp?d_=en&_=en_US>, which shows
  ///    the short time pattern used in the `en_US` locale.
436
  @override
437
  TimeOfDayFormat timeOfDayFormat({ bool alwaysUse24HourFormat = false }) {
438
    assert(alwaysUse24HourFormat != null);
439
    if (alwaysUse24HourFormat)
440 441
      return _get24HourVersionOf(timeOfDayFormatRaw);
    return timeOfDayFormatRaw;
442 443
  }

444 445 446 447 448 449 450 451 452 453 454 455 456 457
  /// The "zero" form of [licensesPackageDetailText].
  ///
  /// This form is optional.
  ///
  /// See also:
  ///
  ///  * [Intl.plural], to which this form is passed.
  ///  * [licensesPackageDetailTextZero], the "zero" form
  ///  * [licensesPackageDetailTextOne], the "one" form
  ///  * [licensesPackageDetailTextTwo], the "two" form
  ///  * [licensesPackageDetailTextFew], the "few" form
  ///  * [licensesPackageDetailTextMany], the "many" form
  ///  * [licensesPackageDetailTextOther], the "other" form
  @protected
458
  String? get licensesPackageDetailTextZero => null;
459 460 461 462 463 464 465 466 467 468 469 470 471 472

  /// The "one" form of [licensesPackageDetailText].
  ///
  /// This form is optional.
  ///
  /// See also:
  ///
  ///  * [licensesPackageDetailTextZero], the "zero" form
  ///  * [licensesPackageDetailTextOne], the "one" form
  ///  * [licensesPackageDetailTextTwo], the "two" form
  ///  * [licensesPackageDetailTextFew], the "few" form
  ///  * [licensesPackageDetailTextMany], the "many" form
  ///  * [licensesPackageDetailTextOther], the "other" form
  @protected
473
  String? get licensesPackageDetailTextOne => null;
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488

  /// The "two" form of [licensesPackageDetailText].
  ///
  /// This form is optional.
  ///
  /// See also:
  ///
  ///  * [Intl.plural], to which this form is passed.
  ///  * [licensesPackageDetailTextZero], the "zero" form
  ///  * [licensesPackageDetailTextOne], the "one" form
  ///  * [licensesPackageDetailTextTwo], the "two" form
  ///  * [licensesPackageDetailTextFew], the "few" form
  ///  * [licensesPackageDetailTextMany], the "many" form
  ///  * [licensesPackageDetailTextOther], the "other" form
  @protected
489
  String? get licensesPackageDetailTextTwo => null;
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

  /// The "many" form of [licensesPackageDetailText].
  ///
  /// This form is optional.
  ///
  /// See also:
  ///
  ///  * [Intl.plural], to which this form is passed.
  ///  * [licensesPackageDetailTextZero], the "zero" form
  ///  * [licensesPackageDetailTextOne], the "one" form
  ///  * [licensesPackageDetailTextTwo], the "two" form
  ///  * [licensesPackageDetailTextFew], the "few" form
  ///  * [licensesPackageDetailTextMany], the "many" form
  ///  * [licensesPackageDetailTextOther], the "other" form
  @protected
505
  String? get licensesPackageDetailTextMany => null;
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

  /// The "few" form of [licensesPackageDetailText].
  ///
  /// This form is optional.
  ///
  /// See also:
  ///
  ///  * [Intl.plural], to which this form is passed.
  ///  * [licensesPackageDetailTextZero], the "zero" form
  ///  * [licensesPackageDetailTextOne], the "one" form
  ///  * [licensesPackageDetailTextTwo], the "two" form
  ///  * [licensesPackageDetailTextFew], the "few" form
  ///  * [licensesPackageDetailTextMany], the "many" form
  ///  * [licensesPackageDetailTextOther], the "other" form
  @protected
521
  String? get licensesPackageDetailTextFew => null;
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552

  /// The "other" form of [licensesPackageDetailText].
  ///
  /// This form is required.
  ///
  /// See also:
  ///
  ///  * [Intl.plural], to which this form is passed.
  ///  * [licensesPackageDetailTextZero], the "zero" form
  ///  * [licensesPackageDetailTextOne], the "one" form
  ///  * [licensesPackageDetailTextTwo], the "two" form
  ///  * [licensesPackageDetailTextFew], the "few" form
  ///  * [licensesPackageDetailTextMany], the "many" form
  ///  * [licensesPackageDetailTextOther], the "other" form
  @protected
  String get licensesPackageDetailTextOther;

  @override
  String licensesPackageDetailText(int licenseCount) {
    return intl.Intl.pluralLogic(
      licenseCount,
      zero: licensesPackageDetailTextZero,
      one: licensesPackageDetailTextOne,
      two: licensesPackageDetailTextTwo,
      many: licensesPackageDetailTextMany,
      few: licensesPackageDetailTextFew,
      other: licensesPackageDetailTextOther,
      locale: _localeName,
    ).replaceFirst(r'$licenseCount', formatDecimal(licenseCount));
  }

553 554
  /// The "zero" form of [remainingTextFieldCharacterCount].
  ///
555
  /// This form is optional.
556 557 558 559 560 561
  ///
  /// See also:
  ///
  ///  * [Intl.plural], to which this form is passed.
  ///  * [remainingTextFieldCharacterCountZero], the "zero" form
  ///  * [remainingTextFieldCharacterCountOne], the "one" form
562 563 564
  ///  * [remainingTextFieldCharacterCountTwo], the "two" form
  ///  * [remainingTextFieldCharacterCountFew], the "few" form
  ///  * [remainingTextFieldCharacterCountMany], the "many" form
565 566
  ///  * [remainingTextFieldCharacterCountOther], the "other" form
  @protected
567
  String? get remainingTextFieldCharacterCountZero => null;
568 569 570 571 572 573 574 575 576

  /// The "one" form of [remainingTextFieldCharacterCount].
  ///
  /// This form is optional.
  ///
  /// See also:
  ///
  ///  * [remainingTextFieldCharacterCountZero], the "zero" form
  ///  * [remainingTextFieldCharacterCountOne], the "one" form
577 578 579
  ///  * [remainingTextFieldCharacterCountTwo], the "two" form
  ///  * [remainingTextFieldCharacterCountFew], the "few" form
  ///  * [remainingTextFieldCharacterCountMany], the "many" form
580 581
  ///  * [remainingTextFieldCharacterCountOther], the "other" form
  @protected
582
  String? get remainingTextFieldCharacterCountOne => null;
583

584 585 586 587 588 589 590 591 592 593 594 595 596 597
  /// The "two" form of [remainingTextFieldCharacterCount].
  ///
  /// This form is optional.
  ///
  /// See also:
  ///
  ///  * [Intl.plural], to which this form is passed.
  ///  * [remainingTextFieldCharacterCountZero], the "zero" form
  ///  * [remainingTextFieldCharacterCountOne], the "one" form
  ///  * [remainingTextFieldCharacterCountTwo], the "two" form
  ///  * [remainingTextFieldCharacterCountFew], the "few" form
  ///  * [remainingTextFieldCharacterCountMany], the "many" form
  ///  * [remainingTextFieldCharacterCountOther], the "other" form
  @protected
598
  String? get remainingTextFieldCharacterCountTwo => null;
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613

  /// The "many" form of [remainingTextFieldCharacterCount].
  ///
  /// This form is optional.
  ///
  /// See also:
  ///
  ///  * [Intl.plural], to which this form is passed.
  ///  * [remainingTextFieldCharacterCountZero], the "zero" form
  ///  * [remainingTextFieldCharacterCountOne], the "one" form
  ///  * [remainingTextFieldCharacterCountTwo], the "two" form
  ///  * [remainingTextFieldCharacterCountFew], the "few" form
  ///  * [remainingTextFieldCharacterCountMany], the "many" form
  ///  * [remainingTextFieldCharacterCountOther], the "other" form
  @protected
614
  String? get remainingTextFieldCharacterCountMany => null;
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629

  /// The "few" form of [remainingTextFieldCharacterCount].
  ///
  /// This form is optional.
  ///
  /// See also:
  ///
  ///  * [Intl.plural], to which this form is passed.
  ///  * [remainingTextFieldCharacterCountZero], the "zero" form
  ///  * [remainingTextFieldCharacterCountOne], the "one" form
  ///  * [remainingTextFieldCharacterCountTwo], the "two" form
  ///  * [remainingTextFieldCharacterCountFew], the "few" form
  ///  * [remainingTextFieldCharacterCountMany], the "many" form
  ///  * [remainingTextFieldCharacterCountOther], the "other" form
  @protected
630
  String? get remainingTextFieldCharacterCountFew => null;
631

632 633 634 635 636 637 638 639 640
  /// The "other" form of [remainingTextFieldCharacterCount].
  ///
  /// This form is required.
  ///
  /// See also:
  ///
  ///  * [Intl.plural], to which this form is passed.
  ///  * [remainingTextFieldCharacterCountZero], the "zero" form
  ///  * [remainingTextFieldCharacterCountOne], the "one" form
641 642 643
  ///  * [remainingTextFieldCharacterCountTwo], the "two" form
  ///  * [remainingTextFieldCharacterCountFew], the "few" form
  ///  * [remainingTextFieldCharacterCountMany], the "many" form
644 645 646 647 648
  ///  * [remainingTextFieldCharacterCountOther], the "other" form
  @protected
  String get remainingTextFieldCharacterCountOther;

  @override
649
  String remainingTextFieldCharacterCount(int remaining) {
650
    return intl.Intl.pluralLogic(
651
      remaining,
652 653
      zero: remainingTextFieldCharacterCountZero,
      one: remainingTextFieldCharacterCountOne,
654 655 656
      two: remainingTextFieldCharacterCountTwo,
      many: remainingTextFieldCharacterCountMany,
      few: remainingTextFieldCharacterCountFew,
657 658
      other: remainingTextFieldCharacterCountOther,
      locale: _localeName,
659
    ).replaceFirst(r'$remainingCount', formatDecimal(remaining));
660 661
  }

662
  @override
663
  ScriptCategory get scriptCategory;
664

665
  /// A [LocalizationsDelegate] for [MaterialLocalizations].
666
  ///
667
  /// Most internationalized apps will use [GlobalMaterialLocalizations.delegates]
668 669
  /// as the value of [MaterialApp.localizationsDelegates] to include
  /// the localizations for both the material and widget libraries.
670
  static const LocalizationsDelegate<MaterialLocalizations> delegate = _MaterialLocalizationsDelegate();
671 672 673 674

  /// A value for [MaterialApp.localizationsDelegates] that's typically used by
  /// internationalized apps.
  ///
675 676
  /// ## Sample code
  ///
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
  /// To include the localizations provided by this class and by
  /// [GlobalWidgetsLocalizations] in a [MaterialApp],
  /// use [GlobalMaterialLocalizations.delegates] as the value of
  /// [MaterialApp.localizationsDelegates], and specify the locales your
  /// app supports with [MaterialApp.supportedLocales]:
  ///
  /// ```dart
  /// new MaterialApp(
  ///   localizationsDelegates: GlobalMaterialLocalizations.delegates,
  ///   supportedLocales: [
  ///     const Locale('en', 'US'), // English
  ///     const Locale('he', 'IL'), // Hebrew
  ///   ],
  ///   // ...
  /// )
  /// ```
693
  static const List<LocalizationsDelegate<dynamic>> delegates = <LocalizationsDelegate<dynamic>>[
694
    GlobalCupertinoLocalizations.delegate,
695 696 697 698 699
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
  ];
}

700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
/// Finds the [TimeOfDayFormat] to use instead of the `original` when the
/// `original` uses 12-hour format and [MediaQueryData.alwaysUse24HourFormat]
/// is true.
TimeOfDayFormat _get24HourVersionOf(TimeOfDayFormat original) {
  switch (original) {
    case TimeOfDayFormat.HH_colon_mm:
    case TimeOfDayFormat.HH_dot_mm:
    case TimeOfDayFormat.frenchCanadian:
    case TimeOfDayFormat.H_colon_mm:
      return original;
    case TimeOfDayFormat.h_colon_mm_space_a:
    case TimeOfDayFormat.a_space_h_colon_mm:
      return TimeOfDayFormat.HH_colon_mm;
  }
}

716 717 718
class _MaterialLocalizationsDelegate extends LocalizationsDelegate<MaterialLocalizations> {
  const _MaterialLocalizationsDelegate();

719
  @override
720
  bool isSupported(Locale locale) => kMaterialSupportedLanguages.contains(locale.languageCode);
721

722 723 724 725 726 727
  static final Map<Locale, Future<MaterialLocalizations>> _loadedTranslations = <Locale, Future<MaterialLocalizations>>{};

  @override
  Future<MaterialLocalizations> load(Locale locale) {
    assert(isSupported(locale));
    return _loadedTranslations.putIfAbsent(locale, () {
728
      util.loadDateIntlDataIfNotLoaded();
729 730

      final String localeName = intl.Intl.canonicalizedLocale(locale.toString());
731 732 733 734 735
      assert(
        locale.toString() == localeName,
        'Flutter does not support the non-standard locale form $locale (which '
        'might be $localeName',
      );
736 737

      intl.DateFormat fullYearFormat;
738 739
      intl.DateFormat compactDateFormat;
      intl.DateFormat shortDateFormat;
740 741 742
      intl.DateFormat mediumDateFormat;
      intl.DateFormat longDateFormat;
      intl.DateFormat yearMonthFormat;
743
      intl.DateFormat shortMonthDayFormat;
744
      if (intl.DateFormat.localeExists(localeName)) {
745
        fullYearFormat = intl.DateFormat.y(localeName);
746 747
        compactDateFormat = intl.DateFormat.yMd(localeName);
        shortDateFormat = intl.DateFormat.yMMMd(localeName);
748 749 750
        mediumDateFormat = intl.DateFormat.MMMEd(localeName);
        longDateFormat = intl.DateFormat.yMMMMEEEEd(localeName);
        yearMonthFormat = intl.DateFormat.yMMMM(localeName);
751
        shortMonthDayFormat = intl.DateFormat.MMMd(localeName);
752
      } else if (intl.DateFormat.localeExists(locale.languageCode)) {
753
        fullYearFormat = intl.DateFormat.y(locale.languageCode);
754 755
        compactDateFormat = intl.DateFormat.yMd(locale.languageCode);
        shortDateFormat = intl.DateFormat.yMMMd(locale.languageCode);
756 757 758
        mediumDateFormat = intl.DateFormat.MMMEd(locale.languageCode);
        longDateFormat = intl.DateFormat.yMMMMEEEEd(locale.languageCode);
        yearMonthFormat = intl.DateFormat.yMMMM(locale.languageCode);
759
        shortMonthDayFormat = intl.DateFormat.MMMd(locale.languageCode);
760
      } else {
761
        fullYearFormat = intl.DateFormat.y();
762 763
        compactDateFormat = intl.DateFormat.yMd();
        shortDateFormat = intl.DateFormat.yMMMd();
764 765 766
        mediumDateFormat = intl.DateFormat.MMMEd();
        longDateFormat = intl.DateFormat.yMMMMEEEEd();
        yearMonthFormat = intl.DateFormat.yMMMM();
767
        shortMonthDayFormat = intl.DateFormat.MMMd();
768 769 770 771 772
      }

      intl.NumberFormat decimalFormat;
      intl.NumberFormat twoDigitZeroPaddedFormat;
      if (intl.NumberFormat.localeExists(localeName)) {
773 774
        decimalFormat = intl.NumberFormat.decimalPattern(localeName);
        twoDigitZeroPaddedFormat = intl.NumberFormat('00', localeName);
775
      } else if (intl.NumberFormat.localeExists(locale.languageCode)) {
776 777
        decimalFormat = intl.NumberFormat.decimalPattern(locale.languageCode);
        twoDigitZeroPaddedFormat = intl.NumberFormat('00', locale.languageCode);
778
      } else {
779 780
        decimalFormat = intl.NumberFormat.decimalPattern();
        twoDigitZeroPaddedFormat = intl.NumberFormat('00');
781 782
      }

783
      return SynchronousFuture<MaterialLocalizations>(getMaterialTranslation(
784 785
        locale,
        fullYearFormat,
786 787
        compactDateFormat,
        shortDateFormat,
788 789 790
        mediumDateFormat,
        longDateFormat,
        yearMonthFormat,
791
        shortMonthDayFormat,
792 793
        decimalFormat,
        twoDigitZeroPaddedFormat,
794
      )!);
795 796
    });
  }
797 798 799

  @override
  bool shouldReload(_MaterialLocalizationsDelegate old) => false;
800 801

  @override
802
  String toString() => 'GlobalMaterialLocalizations.delegate(${kMaterialSupportedLanguages.length} locales)';
803
}