localizations.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. import 'dart:async';
  5. import 'package:flutter/foundation.dart';
  6. import 'package:flutter/widgets.dart';
  7. /// Determines the order of the columns inside [CupertinoDatePicker] in
  8. /// time and date time mode.
  9. enum DatePickerDateTimeOrder {
  10. /// Order of the columns, from left to right: date, hour, minute, am/pm.
  11. ///
  12. /// Example: [Fri Aug 31 | 02 | 08 | PM].
  13. date_time_dayPeriod,
  14. /// Order of the columns, from left to right: date, am/pm, hour, minute.
  15. ///
  16. /// Example: [Fri Aug 31 | PM | 02 | 08].
  17. date_dayPeriod_time,
  18. /// Order of the columns, from left to right: hour, minute, am/pm, date.
  19. ///
  20. /// Example: [02 | 08 | PM | Fri Aug 31].
  21. time_dayPeriod_date,
  22. /// Order of the columns, from left to right: am/pm, hour, minute, date.
  23. ///
  24. /// Example: [PM | 02 | 08 | Fri Aug 31].
  25. dayPeriod_time_date,
  26. }
  27. /// Determines the order of the columns inside [CupertinoDatePicker] in date mode.
  28. enum DatePickerDateOrder {
  29. /// Order of the columns, from left to right: day, month, year.
  30. ///
  31. /// Example: [12 | March | 1996]
  32. dmy,
  33. /// Order of the columns, from left to right: month, day, year.
  34. ///
  35. /// Example: [March | 12 | 1996]
  36. mdy,
  37. /// Order of the columns, from left to right: year, month, day.
  38. ///
  39. /// Example: [1996 | March | 12]
  40. ymd,
  41. /// Order of the columns, from left to right: year, day, month.
  42. ///
  43. /// Example: [1996 | 12 | March]
  44. ydm,
  45. }
  46. /// Defines the localized resource values used by the Cupertino widgets.
  47. ///
  48. /// See also:
  49. ///
  50. /// * [DefaultCupertinoLocalizations], the default, English-only, implementation
  51. /// of this interface.
  52. // TODO(xster): Supply non-english strings.
  53. abstract class CupertinoLocalizations {
  54. /// Year that is shown in [CupertinoDatePicker] spinner corresponding to the
  55. /// given year index.
  56. ///
  57. /// Examples: datePickerYear(1) in:
  58. ///
  59. /// - US English: 2018
  60. /// - Korean: 2018년
  61. String datePickerYear(int yearIndex);
  62. /// Month that is shown in [CupertinoDatePicker] spinner corresponding to
  63. /// the given month index.
  64. ///
  65. /// Examples: datePickerMonth(1) in:
  66. ///
  67. /// - US English: January
  68. /// - Korean: 1월
  69. String datePickerMonth(int monthIndex);
  70. /// Day of month that is shown in [CupertinoDatePicker] spinner corresponding
  71. /// to the given day index.
  72. ///
  73. /// Examples: datePickerDayOfMonth(1) in:
  74. ///
  75. /// - US English: 1
  76. /// - Korean: 1일
  77. String datePickerDayOfMonth(int dayIndex);
  78. /// The medium-width date format that is shown in [CupertinoDatePicker]
  79. /// spinner. Abbreviates month and days of week.
  80. ///
  81. /// Examples:
  82. ///
  83. /// - US English: Wed Sep 27
  84. /// - Russian: ср сент. 27
  85. String datePickerMediumDate(DateTime date);
  86. /// Hour that is shown in [CupertinoDatePicker] spinner corresponding
  87. /// to the given hour value.
  88. ///
  89. /// Examples: datePickerHour(1) in:
  90. ///
  91. /// - US English: 1
  92. /// - Arabic: ٠١
  93. String datePickerHour(int hour);
  94. /// Semantics label for the given hour value in [CupertinoDatePicker].
  95. String datePickerHourSemanticsLabel(int hour);
  96. /// Minute that is shown in [CupertinoDatePicker] spinner corresponding
  97. /// to the given minute value.
  98. ///
  99. /// Examples: datePickerMinute(1) in:
  100. ///
  101. /// - US English: 01
  102. /// - Arabic: ٠١
  103. String datePickerMinute(int minute);
  104. /// Semantics label for the given minute value in [CupertinoDatePicker].
  105. String datePickerMinuteSemanticsLabel(int minute);
  106. /// The order of the date elements that will be shown in [CupertinoDatePicker].
  107. DatePickerDateOrder get datePickerDateOrder;
  108. /// The order of the time elements that will be shown in [CupertinoDatePicker].
  109. DatePickerDateTimeOrder get datePickerDateTimeOrder;
  110. /// The abbreviation for ante meridiem (before noon) shown in the time picker.
  111. String get anteMeridiemAbbreviation;
  112. /// The abbreviation for post meridiem (after noon) shown in the time picker.
  113. String get postMeridiemAbbreviation;
  114. /// The term used by the system to announce dialog alerts.
  115. String get alertDialogLabel;
  116. /// Hour that is shown in [CupertinoCountdownTimerPicker] corresponding to
  117. /// the given hour value.
  118. ///
  119. /// Examples: timerPickerHour(1) in:
  120. ///
  121. /// - US English: 1
  122. /// - Arabic: ١
  123. String timerPickerHour(int hour);
  124. /// Minute that is shown in [CupertinoCountdownTimerPicker] corresponding to
  125. /// the given minute value.
  126. ///
  127. /// Examples: timerPickerMinute(1) in:
  128. ///
  129. /// - US English: 1
  130. /// - Arabic: ١
  131. String timerPickerMinute(int minute);
  132. /// Second that is shown in [CupertinoCountdownTimerPicker] corresponding to
  133. /// the given second value.
  134. ///
  135. /// Examples: timerPickerSecond(1) in:
  136. ///
  137. /// - US English: 1
  138. /// - Arabic: ١
  139. String timerPickerSecond(int second);
  140. /// Label that appears next to the hour picker in
  141. /// [CupertinoCountdownTimerPicker] when selected hour value is `hour`.
  142. /// This function will deal with pluralization based on the `hour` parameter.
  143. String timerPickerHourLabel(int hour);
  144. /// Label that appears next to the minute picker in
  145. /// [CupertinoCountdownTimerPicker] when selected minute value is `minute`.
  146. /// This function will deal with pluralization based on the `minute` parameter.
  147. String timerPickerMinuteLabel(int minute);
  148. /// Label that appears next to the minute picker in
  149. /// [CupertinoCountdownTimerPicker] when selected minute value is `second`.
  150. /// This function will deal with pluralization based on the `second` parameter.
  151. String timerPickerSecondLabel(int second);
  152. /// The term used for cutting
  153. String get cutButtonLabel;
  154. /// The term used for copying
  155. String get copyButtonLabel;
  156. /// The term used for pasting
  157. String get pasteButtonLabel;
  158. /// The term used for selecting everything
  159. String get selectAllButtonLabel;
  160. /// The `CupertinoLocalizations` from the closest [Localizations] instance
  161. /// that encloses the given context.
  162. ///
  163. /// This method is just a convenient shorthand for:
  164. /// `Localizations.of<CupertinoLocalizations>(context, CupertinoLocalizations)`.
  165. ///
  166. /// References to the localized resources defined by this class are typically
  167. /// written in terms of this method. For example:
  168. ///
  169. /// ```dart
  170. /// CupertinoLocalizations.of(context).anteMeridiemAbbreviation;
  171. /// ```
  172. static CupertinoLocalizations of(BuildContext context) {
  173. return Localizations.of<CupertinoLocalizations>(context, CupertinoLocalizations);
  174. }
  175. }
  176. class _CupertinoLocalizationsDelegate extends LocalizationsDelegate<CupertinoLocalizations> {
  177. const _CupertinoLocalizationsDelegate();
  178. @override
  179. bool isSupported(Locale locale) => locale.languageCode == 'en';
  180. @override
  181. Future<CupertinoLocalizations> load(Locale locale) => DefaultCupertinoLocalizations.load(locale);
  182. @override
  183. bool shouldReload(_CupertinoLocalizationsDelegate old) => false;
  184. @override
  185. String toString() => 'DefaultCupertinoLocalizations.delegate(en_US)';
  186. }
  187. /// US English strings for the cupertino widgets.
  188. class DefaultCupertinoLocalizations implements CupertinoLocalizations {
  189. /// Constructs an object that defines the cupertino widgets' localized strings
  190. /// for US English (only).
  191. ///
  192. /// [LocalizationsDelegate] implementations typically call the static [load]
  193. /// function, rather than constructing this class directly.
  194. const DefaultCupertinoLocalizations();
  195. static const List<String> _shortWeekdays = <String>[
  196. 'Mon',
  197. 'Tue',
  198. 'Wed',
  199. 'Thu',
  200. 'Fri',
  201. 'Sat',
  202. 'Sun',
  203. ];
  204. static const List<String> _shortMonths = <String>[
  205. 'Jan',
  206. 'Feb',
  207. 'Mar',
  208. 'Apr',
  209. 'May',
  210. 'Jun',
  211. 'Jul',
  212. 'Aug',
  213. 'Sep',
  214. 'Oct',
  215. 'Nov',
  216. 'Dec',
  217. ];
  218. static const List<String> _months = <String>[
  219. 'January',
  220. 'February',
  221. 'March',
  222. 'April',
  223. 'May',
  224. 'June',
  225. 'July',
  226. 'August',
  227. 'September',
  228. 'October',
  229. 'November',
  230. 'December',
  231. ];
  232. @override
  233. String datePickerYear(int yearIndex) => yearIndex.toString();
  234. @override
  235. String datePickerMonth(int monthIndex) => _months[monthIndex - 1];
  236. @override
  237. String datePickerDayOfMonth(int dayIndex) => dayIndex.toString();
  238. @override
  239. String datePickerHour(int hour) => hour.toString();
  240. @override
  241. String datePickerHourSemanticsLabel(int hour) => hour.toString() + " o'clock";
  242. @override
  243. String datePickerMinute(int minute) => minute.toString().padLeft(2, '0');
  244. @override
  245. String datePickerMinuteSemanticsLabel(int minute) {
  246. if (minute == 1)
  247. return '1 minute';
  248. return minute.toString() + ' minutes';
  249. }
  250. @override
  251. String datePickerMediumDate(DateTime date) {
  252. return '${_shortWeekdays[date.weekday - DateTime.monday]} '
  253. '${_shortMonths[date.month - DateTime.january]} '
  254. '${date.day.toString().padRight(2)}';
  255. }
  256. @override
  257. DatePickerDateOrder get datePickerDateOrder => DatePickerDateOrder.mdy;
  258. @override
  259. DatePickerDateTimeOrder get datePickerDateTimeOrder => DatePickerDateTimeOrder.date_time_dayPeriod;
  260. @override
  261. String get anteMeridiemAbbreviation => 'AM';
  262. @override
  263. String get postMeridiemAbbreviation => 'PM';
  264. @override
  265. String get alertDialogLabel => 'Alert';
  266. @override
  267. String timerPickerHour(int hour) => hour.toString();
  268. @override
  269. String timerPickerMinute(int minute) => minute.toString();
  270. @override
  271. String timerPickerSecond(int second) => second.toString();
  272. @override
  273. String timerPickerHourLabel(int hour) => hour == 1 ? 'hour' : 'hours';
  274. @override
  275. String timerPickerMinuteLabel(int minute) => 'min';
  276. @override
  277. String timerPickerSecondLabel(int second) => 'sec';
  278. @override
  279. String get cutButtonLabel => 'Cut';
  280. @override
  281. String get copyButtonLabel => 'Copy';
  282. @override
  283. String get pasteButtonLabel => 'Paste';
  284. @override
  285. String get selectAllButtonLabel => 'Select All';
  286. /// Creates an object that provides US English resource values for the
  287. /// cupertino library widgets.
  288. ///
  289. /// The [locale] parameter is ignored.
  290. ///
  291. /// This method is typically used to create a [LocalizationsDelegate].
  292. static Future<CupertinoLocalizations> load(Locale locale) {
  293. return SynchronousFuture<CupertinoLocalizations>(const DefaultCupertinoLocalizations());
  294. }
  295. /// A [LocalizationsDelegate] that uses [DefaultCupertinoLocalizations.load]
  296. /// to create an instance of this class.
  297. static const LocalizationsDelegate<CupertinoLocalizations> delegate = _CupertinoLocalizationsDelegate();
  298. }