material_localizations.dart 19.5 KB
Newer Older
1 2 3 4
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:async';
6

7 8
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
9

10
import 'time.dart';
11
import 'typography.dart';
12

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// ADDING A NEW STRING
//
// If you (someone contributing to the Flutter framework) want to add a new
// string to the MaterialLocalizations object (e.g. because you've added a new
// widget and it has a tooltip), follow these steps:
//
// 1. Add the new getter to MaterialLocalizations below.
//
// 2. Implement a default value in DefaultMaterialLocalizations below.
//
// 3. Add a test to test/material/localizations_test.dart that verifies that
//    this new value is implemented.
//
// 4. Update the flutter_localizations package. To add a new string to the
//    flutter_localizations package, you must first add it to the English
//    translations (lib/src/l10n/material_en.arb), including a description, then
//    you must add it to every other language (all the other *.arb files in that
//    same directory), including a best guess as to the translation, e.g.
//    obtained by optimistic use of Google Translate
32 33 34 35
//    (https://translate.google.com/). After that you have to re-generate
//    lib/src/l10n/localizaions.dart by running
//    `dart dev/tools/gen_localizations.dart --overwrite`. There is a README
//    file with further information in the lib/src/l10n/ directory.
36 37 38 39
//
// 5. If you are a Google employee, you should then also follow the instructions
//    at go/flutter-l10n. If you're not, don't worry about it.

Yegor's avatar
Yegor committed
40
/// Defines the localized resource values used by the Material widgets.
41 42 43
///
/// See also:
///
44 45 46 47
///  * [DefaultMaterialLocalizations], the default, English-only, implementation
///    of this interface.
///  * [GlobalMaterialLocalizations], which provides material localizations for
///    many languages.
48
abstract class MaterialLocalizations {
49
  /// The tooltip for the leading [AppBar] menu (a.k.a. 'hamburger') button.
50
  String get openAppDrawerTooltip;
51 52

  /// The [BackButton]'s tooltip.
53
  String get backButtonTooltip;
54 55

  /// The [CloseButton]'s tooltip.
56
  String get closeButtonTooltip;
57

58 59 60
  /// The tooltip for the delete button on a [Chip].
  String get deleteButtonTooltip;

61
  /// The tooltip for the [MonthPicker]'s "next month" button.
62
  String get nextMonthTooltip;
63 64

  /// The tooltip for the [MonthPicker]'s "previous month" button.
65
  String get previousMonthTooltip;
66

67
  /// The tooltip for the [PaginatedDataTable]'s "next page" button.
68 69
  String get nextPageTooltip;

70
  /// The tooltip for the [PaginatedDataTable]'s "previous page" button.
71 72 73 74 75
  String get previousPageTooltip;

  /// The default [PopupMenuButton] tooltip.
  String get showMenuTooltip;

Hans Muller's avatar
Hans Muller committed
76 77 78
  /// The default title for [AboutListTile].
  String aboutListTileTitle(String applicationName);

79 80 81
  /// Title for the [LicensePage] widget.
  String get licensesPageTitle;

82 83 84 85 86 87
  /// Title for the [PaginatedDataTable]'s row info footer.
  String pageRowsInfoTitle(int firstRow, int lastRow, int rowCount, bool rowCountIsApproximate);

  /// Title for the [PaginatedDataTable]'s "rows per page" footer.
  String get rowsPerPageTitle;

88 89 90 91 92 93 94 95
  /// The accessibility label used on a tab in a [TabBar].
  ///
  /// This message describes the index of the selected tab and how many tabs
  /// there are, e.g. 'Tab 1 of 2' in United States English.
  ///
  /// `tabIndex` and `tabCount` must be greater than or equal to one.
  String tabLabel({int tabIndex, int tabCount});

96
  /// Title for the [PaginatedDataTable]'s selected row count header.
97 98
  String selectedRowCountTitle(int selectedRowCount);

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  /// Label for "cancel" buttons and menu items.
  String get cancelButtonLabel;

  /// Label for "close" buttons and menu items.
  String get closeButtonLabel;

  /// Label for "continue" buttons and menu items.
  String get continueButtonLabel;

  /// Label for "copy" edit buttons and menu items.
  String get copyButtonLabel;

  /// Label for "cut" edit buttons and menu items.
  String get cutButtonLabel;

  /// Label for OK buttons and menu items.
  String get okButtonLabel;

  /// Label for "paste" edit buttons and menu items.
  String get pasteButtonLabel;

  /// Label for "select all" edit buttons and menu items.
  String get selectAllButtonLabel;

123
  /// Label for the [AboutDialog] button that shows the [LicensePage].
124 125
  String get viewLicensesButtonLabel;

Yegor's avatar
Yegor committed
126 127 128 129 130 131
  /// The abbreviation for ante meridiem (before noon) shown in the time picker.
  String get anteMeridiemAbbreviation;

  /// The abbreviation for post meridiem (after noon) shown in the time picker.
  String get postMeridiemAbbreviation;

132 133 134 135 136 137 138 139
  /// The text-to-speech announcement made when a time picker invoked using
  /// [showTimePicker] is set to the hour picker mode.
  String get timePickerHourModeAnnouncement;

  /// The text-to-speech announcement made when a time picker invoked using
  /// [showTimePicker] is set to the minute picker mode.
  String get timePickerMinuteModeAnnouncement;

140 141 142 143 144 145 146
  /// Label read out by accessibility tools (TalkBack or VocieOver) for a modal
  /// barrier to indicate that a tap dismisses the barrier.
  ///
  /// A modal barrier can for example be found behind a alert or popup to block
  /// user interaction with elements behind it.
  String get modalBarrierDismissLabel;

Yegor's avatar
Yegor committed
147 148 149 150
  /// The format used to lay out the time picker.
  ///
  /// The documentation for [TimeOfDayFormat] enum values provides details on
  /// each supported layout.
151
  TimeOfDayFormat timeOfDayFormat({ bool alwaysUse24HourFormat: false });
Yegor's avatar
Yegor committed
152

153 154 155 156
  /// Provides geometric text preferences for the current locale.
  ///
  /// This text theme is incomplete. For example, it lacks text color
  /// information. This theme must be merged with another text theme that
157
  /// provides the missing values.
158 159 160 161
  ///
  /// Typically a complete theme is obtained via [Theme.of], which can be
  /// localized using the [Localizations] widget.
  ///
162 163 164 165 166
  /// The text styles provided by this theme are expected to have their
  /// [TextStyle.inherit] property set to false, so that the [ThemeData]
  /// obtained from [Theme.of] no longer inherits text style properties and
  /// contains a complete set of properties needed to style a [Text] widget.
  ///
167 168 169
  /// See also: https://material.io/guidelines/style/typography.html
  TextTheme get localTextGeometry;

170 171 172 173
  /// Formats [number] as a decimal, inserting locale-appropriate thousands
  /// separators as necessary.
  String formatDecimal(int number);

174 175
  /// Formats [TimeOfDay.hour] in the given time of day according to the value
  /// of [timeOfDayFormat].
176 177 178 179
  ///
  /// If [alwaysUse24HourFormat] is true, formats hour using [HourFormat.HH]
  /// rather than the default for the current locale.
  String formatHour(TimeOfDay timeOfDay, { bool alwaysUse24HourFormat: false });
180 181 182 183 184 185

  /// Formats [TimeOfDay.minute] in the given time of day according to the value
  /// of [timeOfDayFormat].
  String formatMinute(TimeOfDay timeOfDay);

  /// Formats [timeOfDay] according to the value of [timeOfDayFormat].
186 187 188 189 190 191
  ///
  /// If [alwaysUse24HourFormat] is true, formats hour using [HourFormat.HH]
  /// rather than the default for the current locale. This value is usually
  /// passed from [MediaQueryData.alwaysUse24HourFormat], which has platform-
  /// specific behavior.
  String formatTimeOfDay(TimeOfDay timeOfDay, { bool alwaysUse24HourFormat: false });
192

Yegor's avatar
Yegor committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206
  /// Full unabbreviated year format, e.g. 2017 rather than 17.
  String formatYear(DateTime date);

  /// Formats the date using a medium-width format.
  ///
  /// Abbreviates month and days of week. This appears in the header of the date
  /// picker invoked using [showDatePicker].
  ///
  /// Examples:
  ///
  /// - US English: Wed, Sep 27
  /// - Russian: ср, сент. 27
  String formatMediumDate(DateTime date);

207 208 209 210 211 212 213 214 215 216 217
  /// Formats day of week, month, day of month and year in a long-width format.
  ///
  /// Does not abbreviate names. Appears in spoken announcements of the date
  /// picker invoked using [showDatePicker], when accessibility mode is on.
  ///
  /// Examples:
  ///
  /// - US English: Wednesday, September 27, 2017
  /// - Russian: Среда, Сентябрь 27, 2017
  String formatFullDate(DateTime date);

Yegor's avatar
Yegor committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
  /// Formats the month and the year of the given [date].
  ///
  /// The returned string does not contain the day of the month. This appears
  /// in the date picker invoked using [showDatePicker].
  String formatMonthYear(DateTime date);

  /// List of week day names in narrow format, usually 1- or 2-letter
  /// abbreviations of full names.
  ///
  /// The list begins with the value corresponding to Sunday and ends with
  /// Saturday. Use [firstDayOfWeekIndex] to find the first day of week in this
  /// list.
  ///
  /// Examples:
  ///
  /// - US English: S, M, T, W, T, F, S
  /// - Russian: вс, пн, вт, ср, чт, пт, сб - notice that the list begins with
  ///   вс (Sunday) even though the first day of week for Russian is Monday.
236
  List<String> get narrowWeekdays;
Yegor's avatar
Yegor committed
237 238 239 240

  /// Index of the first day of week, where 0 points to Sunday, and 6 points to
  /// Saturday.
  ///
241
  /// This getter is compatible with [narrowWeekdays]. For example:
Yegor's avatar
Yegor committed
242 243 244 245
  ///
  /// ```dart
  /// var localizations = MaterialLocalizations.of(context);
  /// // The name of the first day of week for the current locale.
246
  /// var firstDayOfWeek = localizations.narrowWeekdays[localizations.firstDayOfWeekIndex];
Yegor's avatar
Yegor committed
247 248 249
  /// ```
  int get firstDayOfWeekIndex;

250 251 252 253 254 255 256 257 258 259 260 261
  /// The semantics label used to indicate which account is signed in in the
  /// [UserAccountsDrawerHeader] widget.
  String get signedInLabel;

  /// The semantics label used for the button on [UserAccountsDrawerHeader] that
  /// hides the list of accounts.
  String get hideAccountsLabel;

  /// The semantics label used for the button on [UserAccountsDrawerHeader] that
  /// shows the list of accounts.
  String get showAccountsLabel;

262 263 264 265 266 267 268 269
  /// The `MaterialLocalizations` from the closest [Localizations] instance
  /// that encloses the given context.
  ///
  /// This method is just a convenient shorthand for:
  /// `Localizations.of<MaterialLocalizations>(context, MaterialLocalizations)`.
  ///
  /// References to the localized resources defined by this class are typically
  /// written in terms of this method. For example:
270
  ///
271 272 273 274 275 276 277
  /// ```dart
  /// tooltip: MaterialLocalizations.of(context).backButtonTooltip,
  /// ```
  static MaterialLocalizations of(BuildContext context) {
    return Localizations.of<MaterialLocalizations>(context, MaterialLocalizations);
  }
}
278

279 280 281
class _MaterialLocalizationsDelegate extends LocalizationsDelegate<MaterialLocalizations> {
  const _MaterialLocalizationsDelegate();

282 283 284
  @override
  bool isSupported(Locale locale) => locale.languageCode == 'en';

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
  @override
  Future<MaterialLocalizations> load(Locale locale) => DefaultMaterialLocalizations.load(locale);

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

/// US English strings for the material widgets.
///
/// See also:
///
///  * [GlobalMaterialLocalizations], which provides material localizations for
///    many languages.
///  * [MaterialApp.delegates], which automatically includes
///    [DefaultMaterialLocalizations.delegate] by default.
300
class DefaultMaterialLocalizations implements MaterialLocalizations {
Yegor's avatar
Yegor committed
301
  /// Constructs an object that defines the material widgets' localized strings
302
  /// for US English (only).
303 304 305
  ///
  /// [LocalizationsDelegate] implementations typically call the static [load]
  /// function, rather than constructing this class directly.
306 307 308
  const DefaultMaterialLocalizations();

  // Ordered to match DateTime.MONDAY=1, DateTime.SUNDAY=6
309
  static const List<String> _shortWeekdays = const <String>[
310 311 312 313 314 315 316 317 318
    'Mon',
    'Tue',
    'Wed',
    'Thu',
    'Fri',
    'Sat',
    'Sun',
  ];

319 320 321 322 323 324 325 326 327 328 329
  // Ordered to match DateTime.MONDAY=1, DateTime.SUNDAY=6
  static const List<String> _weekdays = const <String>[
    'Monday',
    'Tuesday',
    'Wednesday',
    'Thursday',
    'Friday',
    'Saturday',
    'Sunday',
  ];

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
  static const List<String> _narrowWeekdays = const <String>[
    'S',
    'M',
    'T',
    'W',
    'T',
    'F',
    'S',
  ];

  static const List<String> _shortMonths = const <String>[
    'Jan',
    'Feb',
    'Mar',
    'Apr',
    'May',
    'Jun',
    'Jul',
    'Aug',
    'Sep',
    'Oct',
    'Nov',
    'Dec',
  ];

  static const List<String> _months = const <String>[
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November',
    'December',
  ];
369

370
  @override
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
  String formatHour(TimeOfDay timeOfDay, { bool alwaysUse24HourFormat: false }) {
    final TimeOfDayFormat format = timeOfDayFormat(alwaysUse24HourFormat: alwaysUse24HourFormat);
    switch (format) {
      case TimeOfDayFormat.h_colon_mm_space_a:
        return formatDecimal(timeOfDay.hourOfPeriod == 0 ? 12 : timeOfDay.hourOfPeriod);
      case TimeOfDayFormat.HH_colon_mm:
        return _formatTwoDigitZeroPad(timeOfDay.hour);
      default:
        throw new AssertionError('$runtimeType does not support $format.');
    }
  }

  /// Formats [number] using two digits, assuming it's in the 0-99 inclusive
  /// range. Not designed to format values outside this range.
  String _formatTwoDigitZeroPad(int number) {
    assert(0 <= number && number < 100);

    if (number < 10)
      return '0$number';

    return '$number';
392 393 394 395
  }

  @override
  String formatMinute(TimeOfDay timeOfDay) {
396 397
    final int minute = timeOfDay.minute;
    return minute < 10 ? '0$minute' : minute.toString();
398 399
  }

Yegor's avatar
Yegor committed
400
  @override
401
  String formatYear(DateTime date) => date.year.toString();
Yegor's avatar
Yegor committed
402 403 404

  @override
  String formatMediumDate(DateTime date) {
405 406 407
    final String day = _shortWeekdays[date.weekday - DateTime.MONDAY];
    final String month = _shortMonths[date.month - DateTime.JANUARY];
    return '$day, $month ${date.day}';
Yegor's avatar
Yegor committed
408 409
  }

410 411 412 413 414 415
  @override
  String formatFullDate(DateTime date) {
    final String month = _months[date.month - DateTime.JANUARY];
    return '${_weekdays[date.weekday - DateTime.MONDAY]}, $month ${date.day}, ${date.year}';
  }

Yegor's avatar
Yegor committed
416 417
  @override
  String formatMonthYear(DateTime date) {
418 419 420
    final String year = formatYear(date);
    final String month = _months[date.month - DateTime.JANUARY];
    return '$month $year';
Yegor's avatar
Yegor committed
421 422 423
  }

  @override
424
  List<String> get narrowWeekdays => _narrowWeekdays;
Yegor's avatar
Yegor committed
425 426

  @override
427
  int get firstDayOfWeekIndex => 0; // narrowWeekdays[0] is 'S' for Sunday
Yegor's avatar
Yegor committed
428

429 430 431 432 433 434 435 436 437 438
  String _formatDayPeriod(TimeOfDay timeOfDay) {
    switch (timeOfDay.period) {
      case DayPeriod.am:
        return anteMeridiemAbbreviation;
      case DayPeriod.pm:
        return postMeridiemAbbreviation;
    }
    return null;
  }

439
  @override
440
  String formatDecimal(int number) {
441 442 443 444 445 446 447 448 449 450 451 452
    if (number > -1000 && number < 1000)
      return number.toString();

    final String digits = number.abs().toString();
    final StringBuffer result = new StringBuffer(number < 0 ? '-' : '');
    final int maxDigitIndex = digits.length - 1;
    for (int i = 0; i <= maxDigitIndex; i += 1) {
      result.write(digits[i]);
      if (i < maxDigitIndex && (maxDigitIndex - i) % 3 == 0)
        result.write(',');
    }
    return result.toString();
453 454 455
  }

  @override
456
  String formatTimeOfDay(TimeOfDay timeOfDay, { bool alwaysUse24HourFormat: false }) {
457 458 459 460 461 462 463 464
    // 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.
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
    final StringBuffer buffer = new StringBuffer();

    // Add hour:minute.
    buffer
      ..write(formatHour(timeOfDay, alwaysUse24HourFormat: alwaysUse24HourFormat))
      ..write(':')
      ..write(formatMinute(timeOfDay));

    if (alwaysUse24HourFormat) {
      // There's no AM/PM indicator in 24-hour format.
      return '$buffer';
    }

    // Add AM/PM indicator.
    buffer
      ..write(' ')
      ..write(_formatDayPeriod(timeOfDay));
    return '$buffer';
483 484
  }

485
  @override
486
  String get openAppDrawerTooltip => 'Open navigation menu';
487 488

  @override
489
  String get backButtonTooltip => 'Back';
490 491

  @override
492
  String get closeButtonTooltip => 'Close';
493

494 495 496
  @override
  String get deleteButtonTooltip => 'Delete';

497
  @override
498
  String get nextMonthTooltip => 'Next month';
499 500

  @override
501
  String get previousMonthTooltip => 'Previous month';
502

503
  @override
504
  String get nextPageTooltip => 'Next page';
505 506

  @override
507
  String get previousPageTooltip => 'Previous page';
508 509

  @override
510
  String get showMenuTooltip => 'Show menu';
511

Hans Muller's avatar
Hans Muller committed
512
  @override
513
  String aboutListTileTitle(String applicationName) => 'About $applicationName';
Hans Muller's avatar
Hans Muller committed
514

515
  @override
516
  String get licensesPageTitle => 'Licenses';
517 518 519

  @override
  String pageRowsInfoTitle(int firstRow, int lastRow, int rowCount, bool rowCountIsApproximate) {
520 521 522
    return rowCountIsApproximate
      ? '$firstRow$lastRow of about $rowCount'
      : '$firstRow$lastRow of $rowCount';
523 524 525
  }

  @override
526
  String get rowsPerPageTitle => 'Rows per page:';
527

528 529 530 531 532 533 534
  @override
  String tabLabel({int tabIndex, int tabCount}) {
    assert(tabIndex >= 1);
    assert(tabCount >= 1);
    return 'Tab $tabIndex of $tabCount';
  }

535 536
  @override
  String selectedRowCountTitle(int selectedRowCount) {
537 538 539 540 541 542 543 544
    switch (selectedRowCount) {
      case 0:
        return 'No items selected';
      case 1:
        return '1 item selected';
      default:
        return '$selectedRowCount items selected';
    }
545
  }
546 547

  @override
548
  String get cancelButtonLabel => 'CANCEL';
549 550

  @override
551
  String get closeButtonLabel => 'CLOSE';
552 553

  @override
554
  String get continueButtonLabel => 'CONTINUE';
555 556

  @override
557
  String get copyButtonLabel => 'COPY';
558 559

  @override
560
  String get cutButtonLabel => 'CUT';
561 562

  @override
563
  String get okButtonLabel => 'OK';
564 565

  @override
566
  String get pasteButtonLabel => 'PASTE';
567 568

  @override
569
  String get selectAllButtonLabel => 'SELECT ALL';
570 571

  @override
572
  String get viewLicensesButtonLabel => 'VIEW LICENSES';
573

Yegor's avatar
Yegor committed
574
  @override
575
  String get anteMeridiemAbbreviation => 'AM';
Yegor's avatar
Yegor committed
576 577

  @override
578
  String get postMeridiemAbbreviation => 'PM';
Yegor's avatar
Yegor committed
579

580 581 582 583 584 585
  @override
  String get timePickerHourModeAnnouncement => 'Select hours';

  @override
  String get timePickerMinuteModeAnnouncement => 'Select minutes';

586 587 588
  @override
  String get modalBarrierDismissLabel => 'Dismiss';

589
  @override
590 591 592 593 594
  TimeOfDayFormat timeOfDayFormat({ bool alwaysUse24HourFormat: false }) {
    return alwaysUse24HourFormat
      ? TimeOfDayFormat.HH_colon_mm
      : TimeOfDayFormat.h_colon_mm_space_a;
  }
Yegor's avatar
Yegor committed
595

596 597
  /// Looks up text geometry defined in [MaterialTextGeometry].
  @override
598
  TextTheme get localTextGeometry => MaterialTextGeometry.englishLike;
599

600 601 602 603 604 605 606 607 608
  @override
  String get signedInLabel => 'Signed in';

  @override
  String get hideAccountsLabel => 'Hide accounts';

  @override
  String get showAccountsLabel => 'Show accounts';

609 610 611 612
  /// Creates an object that provides US English resource values for the material
  /// library widgets.
  ///
  /// The [locale] parameter is ignored.
613 614 615 616
  ///
  /// This method is typically used to create a [LocalizationsDelegate].
  /// The [MaterialApp] does so by default.
  static Future<MaterialLocalizations> load(Locale locale) {
617
    return new SynchronousFuture<MaterialLocalizations>(const DefaultMaterialLocalizations());
618
  }
Yegor's avatar
Yegor committed
619

620 621 622 623 624
  /// A [LocalizationsDelegate] that uses [DefaultMaterialLocalizations.load]
  /// to create an instance of this class.
  ///
  /// [MaterialApp] automatically adds this value to [MaterialApp.localizationsDelegates].
  static const LocalizationsDelegate<MaterialLocalizations> delegate = const _MaterialLocalizationsDelegate();
Yegor's avatar
Yegor committed
625
}