localizations.dart 12.1 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 9
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';

10 11
import 'date_picker.dart';

12 13 14 15 16
/// Determines the order of the columns inside [CupertinoDatePicker] in
/// time and date time mode.
enum DatePickerDateTimeOrder {
  /// Order of the columns, from left to right: date, hour, minute, am/pm.
  ///
17
  /// Example: Fri Aug 31 | 02 | 08 | PM.
18 19 20
  date_time_dayPeriod,
  /// Order of the columns, from left to right: date, am/pm, hour, minute.
  ///
21
  /// Example: Fri Aug 31 | PM | 02 | 08.
22 23 24
  date_dayPeriod_time,
  /// Order of the columns, from left to right: hour, minute, am/pm, date.
  ///
25
  /// Example: 02 | 08 | PM | Fri Aug 31.
26 27 28
  time_dayPeriod_date,
  /// Order of the columns, from left to right: am/pm, hour, minute, date.
  ///
29
  /// Example: PM | 02 | 08 | Fri Aug 31.
30 31 32 33 34 35 36
  dayPeriod_time_date,
}

/// Determines the order of the columns inside [CupertinoDatePicker] in date mode.
enum DatePickerDateOrder {
  /// Order of the columns, from left to right: day, month, year.
  ///
37
  /// Example: 12 | March | 1996
38 39 40
  dmy,
  /// Order of the columns, from left to right: month, day, year.
  ///
41
  /// Example: March | 12 | 1996
42 43 44
  mdy,
  /// Order of the columns, from left to right: year, month, day.
  ///
45
  /// Example: 1996 | March | 12
46 47 48
  ymd,
  /// Order of the columns, from left to right: year, day, month.
  ///
49
  /// Example: 1996 | 12 | March
50 51
  ydm,
}
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

/// Defines the localized resource values used by the Cupertino widgets.
///
/// See also:
///
///  * [DefaultCupertinoLocalizations], the default, English-only, implementation
///    of this interface.
// TODO(xster): Supply non-english strings.
abstract class CupertinoLocalizations {
  /// Year that is shown in [CupertinoDatePicker] spinner corresponding to the
  /// given year index.
  ///
  /// Examples: datePickerYear(1) in:
  ///
  ///  - US English: 2018
  ///  - Korean: 2018년
68
  // The global version uses date symbols data from the intl package.
69 70 71 72 73 74 75 76 77
  String datePickerYear(int yearIndex);

  /// Month that is shown in [CupertinoDatePicker] spinner corresponding to
  /// the given month index.
  ///
  /// Examples: datePickerMonth(1) in:
  ///
  ///  - US English: January
  ///  - Korean: 1월
78
  // The global version uses date symbols data from the intl package.
79 80 81 82 83 84 85 86 87
  String datePickerMonth(int monthIndex);

  /// Day of month that is shown in [CupertinoDatePicker] spinner corresponding
  /// to the given day index.
  ///
  /// Examples: datePickerDayOfMonth(1) in:
  ///
  ///  - US English: 1
  ///  - Korean: 1일
88
  // The global version uses date symbols data from the intl package.
89 90 91 92 93 94 95 96 97
  String datePickerDayOfMonth(int dayIndex);

  /// The medium-width date format that is shown in [CupertinoDatePicker]
  /// spinner. Abbreviates month and days of week.
  ///
  /// Examples:
  ///
  /// - US English: Wed Sep 27
  /// - Russian: ср сент. 27
98
  // The global version is based on intl package's DateFormat.MMMEd.
99 100 101 102 103 104 105
  String datePickerMediumDate(DateTime date);

  /// Hour that is shown in [CupertinoDatePicker] spinner corresponding
  /// to the given hour value.
  ///
  /// Examples: datePickerHour(1) in:
  ///
106
  ///  - US English: 1
107
  ///  - Arabic: ٠١
108
  // The global version uses date symbols data from the intl package.
109 110
  String datePickerHour(int hour);

111
  /// Semantics label for the given hour value in [CupertinoDatePicker].
112
  // The global version uses the translated string from the arb file.
113 114
  String datePickerHourSemanticsLabel(int hour);

115 116 117 118 119 120 121
  /// Minute that is shown in [CupertinoDatePicker] spinner corresponding
  /// to the given minute value.
  ///
  /// Examples: datePickerMinute(1) in:
  ///
  ///  - US English: 01
  ///  - Arabic: ٠١
122
  // The global version uses date symbols data from the intl package.
123 124
  String datePickerMinute(int minute);

125
  /// Semantics label for the given minute value in [CupertinoDatePicker].
126
  // The global version uses the translated string from the arb file.
127 128
  String datePickerMinuteSemanticsLabel(int minute);

129
  /// The order of the date elements that will be shown in [CupertinoDatePicker].
130
  // The global version uses the translated string from the arb file.
131 132 133
  DatePickerDateOrder get datePickerDateOrder;

  /// The order of the time elements that will be shown in [CupertinoDatePicker].
134
  // The global version uses the translated string from the arb file.
135
  DatePickerDateTimeOrder get datePickerDateTimeOrder;
136 137

  /// The abbreviation for ante meridiem (before noon) shown in the time picker.
138
  // The global version uses the translated string from the arb file.
139 140 141
  String get anteMeridiemAbbreviation;

  /// The abbreviation for post meridiem (after noon) shown in the time picker.
142
  // The global version uses the translated string from the arb file.
143 144
  String get postMeridiemAbbreviation;

145 146 147 148
  /// Label shown in date pickers when the date is today.
  // The global version uses the translated string from the arb file.
  String get todayLabel;

149
  /// The term used by the system to announce dialog alerts.
150
  // The global version uses the translated string from the arb file.
151 152
  String get alertDialogLabel;

153
  /// Hour that is shown in [CupertinoTimerPicker] corresponding to
154 155 156 157 158 159
  /// the given hour value.
  ///
  /// Examples: timerPickerHour(1) in:
  ///
  ///  - US English: 1
  ///  - Arabic: ١
160
  // The global version uses date symbols data from the intl package.
161 162
  String timerPickerHour(int hour);

163
  /// Minute that is shown in [CupertinoTimerPicker] corresponding to
164 165 166 167 168 169
  /// the given minute value.
  ///
  /// Examples: timerPickerMinute(1) in:
  ///
  ///  - US English: 1
  ///  - Arabic: ١
170
  // The global version uses date symbols data from the intl package.
171 172
  String timerPickerMinute(int minute);

173
  /// Second that is shown in [CupertinoTimerPicker] corresponding to
174 175 176 177 178 179
  /// the given second value.
  ///
  /// Examples: timerPickerSecond(1) in:
  ///
  ///  - US English: 1
  ///  - Arabic: ١
180
  // The global version uses date symbols data from the intl package.
181 182 183
  String timerPickerSecond(int second);

  /// Label that appears next to the hour picker in
184
  /// [CupertinoTimerPicker] when selected hour value is `hour`.
185
  /// This function will deal with pluralization based on the `hour` parameter.
186
  // The global version uses the translated string from the arb file.
187 188 189
  String timerPickerHourLabel(int hour);

  /// Label that appears next to the minute picker in
190
  /// [CupertinoTimerPicker] when selected minute value is `minute`.
191
  /// This function will deal with pluralization based on the `minute` parameter.
192
  // The global version uses the translated string from the arb file.
193 194 195
  String timerPickerMinuteLabel(int minute);

  /// Label that appears next to the minute picker in
196
  /// [CupertinoTimerPicker] when selected minute value is `second`.
197
  /// This function will deal with pluralization based on the `second` parameter.
198
  // The global version uses the translated string from the arb file.
199 200
  String timerPickerSecondLabel(int second);

201
  /// The term used for cutting
202
  // The global version uses the translated string from the arb file.
203 204 205
  String get cutButtonLabel;

  /// The term used for copying
206
  // The global version uses the translated string from the arb file.
207 208 209
  String get copyButtonLabel;

  /// The term used for pasting
210
  // The global version uses the translated string from the arb file.
211 212 213
  String get pasteButtonLabel;

  /// The term used for selecting everything
214
  // The global version uses the translated string from the arb file.
215 216
  String get selectAllButtonLabel;

217 218 219 220 221 222 223 224 225 226 227 228
  /// The `CupertinoLocalizations` from the closest [Localizations] instance
  /// that encloses the given context.
  ///
  /// This method is just a convenient shorthand for:
  /// `Localizations.of<CupertinoLocalizations>(context, CupertinoLocalizations)`.
  ///
  /// References to the localized resources defined by this class are typically
  /// written in terms of this method. For example:
  ///
  /// ```dart
  /// CupertinoLocalizations.of(context).anteMeridiemAbbreviation;
  /// ```
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
  static CupertinoLocalizations of(BuildContext context) {
    return Localizations.of<CupertinoLocalizations>(context, CupertinoLocalizations);
  }
}

class _CupertinoLocalizationsDelegate extends LocalizationsDelegate<CupertinoLocalizations> {
  const _CupertinoLocalizationsDelegate();

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

  @override
  Future<CupertinoLocalizations> load(Locale locale) => DefaultCupertinoLocalizations.load(locale);

  @override
  bool shouldReload(_CupertinoLocalizationsDelegate old) => false;
245 246 247

  @override
  String toString() => 'DefaultCupertinoLocalizations.delegate(en_US)';
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
}

/// US English strings for the cupertino widgets.
class DefaultCupertinoLocalizations 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.
  const DefaultCupertinoLocalizations();

  static const List<String> _shortWeekdays = <String>[
    'Mon',
    'Tue',
    'Wed',
    'Thu',
    'Fri',
    'Sat',
    'Sun',
  ];

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

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

299 300


301 302 303 304 305 306 307 308 309 310
  @override
  String datePickerYear(int yearIndex) => yearIndex.toString();

  @override
  String datePickerMonth(int monthIndex) => _months[monthIndex - 1];

  @override
  String datePickerDayOfMonth(int dayIndex) => dayIndex.toString();

  @override
311
  String datePickerHour(int hour) => hour.toString();
312

313 314 315
  @override
  String datePickerHourSemanticsLabel(int hour) => hour.toString() + " o'clock";

316 317 318
  @override
  String datePickerMinute(int minute) => minute.toString().padLeft(2, '0');

319 320 321
  @override
  String datePickerMinuteSemanticsLabel(int minute) {
    if (minute == 1)
322
      return '1 minute';
323 324 325
    return minute.toString() + ' minutes';
  }

326 327 328 329
  @override
  String datePickerMediumDate(DateTime date) {
    return '${_shortWeekdays[date.weekday - DateTime.monday]} '
      '${_shortMonths[date.month - DateTime.january]} '
330
      '${date.day.toString().padRight(2)}';
331 332 333
  }

  @override
334 335 336 337
  DatePickerDateOrder get datePickerDateOrder => DatePickerDateOrder.mdy;

  @override
  DatePickerDateTimeOrder get datePickerDateTimeOrder => DatePickerDateTimeOrder.date_time_dayPeriod;
338 339 340 341 342 343 344

  @override
  String get anteMeridiemAbbreviation => 'AM';

  @override
  String get postMeridiemAbbreviation => 'PM';

345 346 347
  @override
  String get todayLabel => 'Today';

348 349 350
  @override
  String get alertDialogLabel => 'Alert';

351 352 353 354 355 356 357 358 359 360 361 362 363
  @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) => hour == 1 ? 'hour' : 'hours';

  @override
364
  String timerPickerMinuteLabel(int minute) => 'min.';
365 366

  @override
367
  String timerPickerSecondLabel(int second) => 'sec.';
368

369 370 371 372 373 374 375 376 377 378 379 380
  @override
  String get cutButtonLabel => 'Cut';

  @override
  String get copyButtonLabel => 'Copy';

  @override
  String get pasteButtonLabel => 'Paste';

  @override
  String get selectAllButtonLabel => 'Select All';

381 382 383 384 385 386 387
  /// 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) {
388
    return SynchronousFuture<CupertinoLocalizations>(const DefaultCupertinoLocalizations());
389 390 391 392 393 394
  }

  /// A [LocalizationsDelegate] that uses [DefaultCupertinoLocalizations.load]
  /// to create an instance of this class.
  static const LocalizationsDelegate<CupertinoLocalizations> delegate = _CupertinoLocalizationsDelegate();
}