Tweaks to `intl` null safe API based on experimentation with `intl_translation`.
PiperOrigin-RevId: 328897810
diff --git a/lib/intl.dart b/lib/intl.dart
index fc52acb..17c3c14 100644
--- a/lib/intl.dart
+++ b/lib/intl.dart
@@ -196,9 +196,12 @@
///
/// Note that null is interpreted as meaning the default locale, so if
/// [newLocale] is null the default locale will be returned.
- static String verifiedLocale(
+ ///
+ /// Can return `null` only if verification fails and `onFailure` returns
+ /// null. Otherwise, throws instead.
+ static String? verifiedLocale(
String? newLocale, bool Function(String) localeExists,
- {String Function(String)? onFailure}) =>
+ {String? Function(String)? onFailure}) =>
helpers.verifiedLocale(newLocale, localeExists, onFailure);
/// Return the short version of a locale name, e.g. 'en_US' => 'en'
diff --git a/lib/message_lookup_by_library.dart b/lib/message_lookup_by_library.dart
index 734033c..ce35041 100644
--- a/lib/message_lookup_by_library.dart
+++ b/lib/message_lookup_by_library.dart
@@ -114,7 +114,7 @@
}
/// Evaluate the translated message and return the translated string.
- String evaluateMessage(translation, List<dynamic> args) {
+ String? evaluateMessage(translation, List<dynamic> args) {
return Function.apply(translation, args);
}
diff --git a/lib/src/intl/compact_number_format.dart b/lib/src/intl/compact_number_format.dart
index 03108d3..32c94ab 100644
--- a/lib/src/intl/compact_number_format.dart
+++ b/lib/src/intl/compact_number_format.dart
@@ -188,7 +188,7 @@
bool isForCurrency = false}) {
// Initialization copied from `NumberFormat` constructor.
// TODO(davidmorgan): deduplicate.
- locale = helpers.verifiedLocale(locale, NumberFormat.localeExists, null);
+ locale = helpers.verifiedLocale(locale, NumberFormat.localeExists, null)!;
var symbols = numberFormatSymbols[locale] as NumberSymbols;
var localeZero = symbols.ZERO_DIGIT.codeUnitAt(0);
var zeroOffset = localeZero - constants.asciiZeroCodeUnit;
diff --git a/lib/src/intl/date_format.dart b/lib/src/intl/date_format.dart
index 8065b4e..4ba4137 100644
--- a/lib/src/intl/date_format.dart
+++ b/lib/src/intl/date_format.dart
@@ -263,7 +263,7 @@
/// If [locale] does not exist in our set of supported locales then an
/// [ArgumentError] is thrown.
DateFormat([String? newPattern, String? locale])
- : _locale = helpers.verifiedLocale(locale, localeExists, null) {
+ : _locale = helpers.verifiedLocale(locale, localeExists, null)! {
// TODO(alanknight): It should be possible to specify multiple skeletons eg
// date, time, timezone all separately. Adding many or named parameters to
// the constructor seems awkward, especially with the possibility of
diff --git a/lib/src/intl/number_format.dart b/lib/src/intl/number_format.dart
index 811646d..9a3a8f2 100644
--- a/lib/src/intl/number_format.dart
+++ b/lib/src/intl/number_format.dart
@@ -309,7 +309,7 @@
int? decimalDigits,
bool lookupSimpleCurrencySymbol = false,
bool isForCurrency = false}) {
- locale = helpers.verifiedLocale(locale, localeExists, null);
+ locale = helpers.verifiedLocale(locale, localeExists, null)!;
var symbols = numberFormatSymbols[locale] as NumberSymbols;
var localeZero = symbols.ZERO_DIGIT.codeUnitAt(0);
var zeroOffset = localeZero - constants.asciiZeroCodeUnit;
diff --git a/lib/src/intl_helpers.dart b/lib/src/intl_helpers.dart
index dfc6c63..542c730 100644
--- a/lib/src/intl_helpers.dart
+++ b/lib/src/intl_helpers.dart
@@ -145,8 +145,8 @@
return '${aLocale[0]}${aLocale[1]}_$region';
}
-String verifiedLocale(String? newLocale, bool Function(String) localeExists,
- String Function(String)? onFailure) {
+String? verifiedLocale(String? newLocale, bool Function(String) localeExists,
+ String? Function(String)? onFailure) {
// TODO(alanknight): Previously we kept a single verified locale on the Intl
// object, but with different verification for different uses, that's more
// difficult. As a result, we call this more often. Consider keeping