Unverified Commit d6c77320 authored by Jenn Magder's avatar Jenn Magder Committed by GitHub

Migrate gen_l10n_types to null safety (#79108)

parent 5231a7dc
...@@ -2,15 +2,10 @@ ...@@ -2,15 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
import 'package:intl/locale.dart'; import 'package:intl/locale.dart';
import '../base/file_system.dart'; import '../base/file_system.dart';
import '../convert.dart'; import '../convert.dart';
import '../globals.dart' as globals;
import 'localizations_utils.dart'; import 'localizations_utils.dart';
// The set of date formats that can be automatically localized. // The set of date formats that can be automatically localized.
...@@ -202,9 +197,9 @@ class Placeholder { ...@@ -202,9 +197,9 @@ class Placeholder {
final String resourceId; final String resourceId;
final String name; final String name;
final String example; final String? example;
final String type; final String? type;
final String format; final String? format;
final List<OptionalParameter> optionalParameters; final List<OptionalParameter> optionalParameters;
bool get requiresFormatting => <String>['DateTime', 'double', 'int', 'num'].contains(type); bool get requiresFormatting => <String>['DateTime', 'double', 'int', 'num'].contains(type);
...@@ -214,7 +209,7 @@ class Placeholder { ...@@ -214,7 +209,7 @@ class Placeholder {
bool get isDate => 'DateTime' == type; bool get isDate => 'DateTime' == type;
bool get hasValidDateFormat => _validDateFormats.contains(format); bool get hasValidDateFormat => _validDateFormats.contains(format);
static String _stringAttribute( static String? _stringAttribute(
String resourceId, String resourceId,
String name, String name,
Map<String, dynamic> attributes, Map<String, dynamic> attributes,
...@@ -224,13 +219,13 @@ class Placeholder { ...@@ -224,13 +219,13 @@ class Placeholder {
if (value == null) { if (value == null) {
return null; return null;
} }
if (value is! String || (value as String).isEmpty) { if (value is! String || value.isEmpty) {
throw L10nException( throw L10nException(
'The "$attributeName" value of the "$name" placeholder in message $resourceId ' 'The "$attributeName" value of the "$name" placeholder in message $resourceId '
'must be a non-empty string.', 'must be a non-empty string.',
); );
} }
return value as String; return value;
} }
static List<OptionalParameter> _optionalParameters( static List<OptionalParameter> _optionalParameters(
...@@ -249,9 +244,9 @@ class Placeholder { ...@@ -249,9 +244,9 @@ class Placeholder {
'with keys that are strings.' 'with keys that are strings.'
); );
} }
final Map<String, dynamic> optionalParameterMap = value as Map<String, dynamic>; final Map<String, Object> optionalParameterMap = value;
return optionalParameterMap.keys.map<OptionalParameter>((String parameterName) { return optionalParameterMap.keys.map<OptionalParameter>((String parameterName) {
return OptionalParameter(parameterName, optionalParameterMap[parameterName]); return OptionalParameter(parameterName, optionalParameterMap[parameterName]!);
}).toList(); }).toList();
} }
} }
...@@ -284,17 +279,17 @@ class Message { ...@@ -284,17 +279,17 @@ class Message {
final String resourceId; final String resourceId;
final String value; final String value;
final String description; final String? description;
final List<Placeholder> placeholders; final List<Placeholder> placeholders;
final RegExpMatch _pluralMatch; final RegExpMatch? _pluralMatch;
bool get isPlural => _pluralMatch != null && _pluralMatch.groupCount == 1; bool get isPlural => _pluralMatch != null && _pluralMatch!.groupCount == 1;
bool get placeholdersRequireFormatting => placeholders.any((Placeholder p) => p.requiresFormatting); bool get placeholdersRequireFormatting => placeholders.any((Placeholder p) => p.requiresFormatting);
Placeholder getCountPlaceholder() { Placeholder getCountPlaceholder() {
assert(isPlural); assert(isPlural);
final String countPlaceholderName = _pluralMatch[1]; final String countPlaceholderName = _pluralMatch![1]!;
return placeholders.firstWhere( return placeholders.firstWhere(
(Placeholder p) => p.name == countPlaceholderName, (Placeholder p) => p.name == countPlaceholderName,
orElse: () { orElse: () {
...@@ -336,7 +331,7 @@ class Message { ...@@ -336,7 +331,7 @@ class Message {
); );
} }
final RegExpMatch pluralRegExp = _pluralRE.firstMatch(_value(bundle, resourceId)); final RegExpMatch? pluralRegExp = _pluralRE.firstMatch(_value(bundle, resourceId));
final bool isPlural = pluralRegExp != null && pluralRegExp.groupCount == 1; final bool isPlural = pluralRegExp != null && pluralRegExp.groupCount == 1;
if (attributes == null && isPlural) { if (attributes == null && isPlural) {
throw L10nException( throw L10nException(
...@@ -348,7 +343,7 @@ class Message { ...@@ -348,7 +343,7 @@ class Message {
return attributes as Map<String, dynamic>; return attributes as Map<String, dynamic>;
} }
static String _description( static String? _description(
Map<String, dynamic> bundle, Map<String, dynamic> bundle,
String resourceId, String resourceId,
bool isResourceAttributeRequired, bool isResourceAttributeRequired,
...@@ -367,7 +362,7 @@ class Message { ...@@ -367,7 +362,7 @@ class Message {
'The description for "@$resourceId" is not a properly formatted String.' 'The description for "@$resourceId" is not a properly formatted String.'
); );
} }
return value as String; return value;
} }
static List<Placeholder> _placeholders( static List<Placeholder> _placeholders(
...@@ -379,17 +374,16 @@ class Message { ...@@ -379,17 +374,16 @@ class Message {
if (resourceAttributes == null) { if (resourceAttributes == null) {
return <Placeholder>[]; return <Placeholder>[];
} }
final dynamic value = resourceAttributes['placeholders']; final dynamic allPlaceholdersMap = resourceAttributes['placeholders'];
if (value == null) { if (allPlaceholdersMap == null) {
return <Placeholder>[]; return <Placeholder>[];
} }
if (value is! Map<String, dynamic>) { if (allPlaceholdersMap is! Map<String, dynamic>) {
throw L10nException( throw L10nException(
'The "placeholders" attribute for message $resourceId, is not ' 'The "placeholders" attribute for message $resourceId, is not '
'properly formatted. Ensure that it is a map with string valued keys.' 'properly formatted. Ensure that it is a map with string valued keys.'
); );
} }
final Map<String, dynamic> allPlaceholdersMap = value as Map<String, dynamic>;
return allPlaceholdersMap.keys.map<Placeholder>((String placeholderName) { return allPlaceholdersMap.keys.map<Placeholder>((String placeholderName) {
final dynamic value = allPlaceholdersMap[placeholderName]; final dynamic value = allPlaceholdersMap[placeholderName];
if (value is! Map<String, dynamic>) { if (value is! Map<String, dynamic>) {
...@@ -399,7 +393,7 @@ class Message { ...@@ -399,7 +393,7 @@ class Message {
'with string valued keys.' 'with string valued keys.'
); );
} }
return Placeholder(resourceId, placeholderName, value as Map<String, dynamic>); return Placeholder(resourceId, placeholderName, value);
}).toList(); }).toList();
} }
} }
...@@ -422,13 +416,13 @@ class AppResourceBundle { ...@@ -422,13 +416,13 @@ class AppResourceBundle {
String localeString = resources['@@locale'] as String; String localeString = resources['@@locale'] as String;
// Look for the first instance of an ISO 639-1 language code, matching exactly. // Look for the first instance of an ISO 639-1 language code, matching exactly.
final String fileName = globals.fs.path.basenameWithoutExtension(file.path); final String fileName = file.fileSystem.path.basenameWithoutExtension(file.path);
for (int index = 0; index < fileName.length; index += 1) { for (int index = 0; index < fileName.length; index += 1) {
// If an underscore was found, check if locale string follows. // If an underscore was found, check if locale string follows.
if (fileName[index] == '_' && fileName[index + 1] != null) { if (fileName[index] == '_' && fileName[index + 1] != null) {
// If Locale.tryParse fails, it returns null. // If Locale.tryParse fails, it returns null.
final Locale parserResult = Locale.tryParse(fileName.substring(index + 1)); final Locale? parserResult = Locale.tryParse(fileName.substring(index + 1));
// If the parserResult is not an actual locale identifier, end the loop. // If the parserResult is not an actual locale identifier, end the loop.
if (parserResult != null && _iso639Languages.contains(parserResult.languageCode)) { if (parserResult != null && _iso639Languages.contains(parserResult.languageCode)) {
// The parsed result uses dashes ('-'), but we want underscores ('_'). // The parsed result uses dashes ('-'), but we want underscores ('_').
...@@ -508,7 +502,7 @@ class AppResourceBundleCollection { ...@@ -508,7 +502,7 @@ class AppResourceBundleCollection {
} }
localeToBundle[bundle.locale] = bundle; localeToBundle[bundle.locale] = bundle;
languageToLocales[bundle.locale.languageCode] ??= <LocaleInfo>[]; languageToLocales[bundle.locale.languageCode] ??= <LocaleInfo>[];
languageToLocales[bundle.locale.languageCode].add(bundle.locale); languageToLocales[bundle.locale.languageCode]!.add(bundle.locale);
} }
} }
...@@ -539,7 +533,7 @@ class AppResourceBundleCollection { ...@@ -539,7 +533,7 @@ class AppResourceBundleCollection {
Iterable<LocaleInfo> get locales => _localeToBundle.keys; Iterable<LocaleInfo> get locales => _localeToBundle.keys;
Iterable<AppResourceBundle> get bundles => _localeToBundle.values; Iterable<AppResourceBundle> get bundles => _localeToBundle.values;
AppResourceBundle bundleFor(LocaleInfo locale) => _localeToBundle[locale]; AppResourceBundle? bundleFor(LocaleInfo locale) => _localeToBundle[locale];
Iterable<String> get languages => _languageToLocales.keys; Iterable<String> get languages => _languageToLocales.keys;
Iterable<LocaleInfo> localesForLanguage(String language) => _languageToLocales[language] ?? <LocaleInfo>[]; Iterable<LocaleInfo> localesForLanguage(String language) => _languageToLocales[language] ?? <LocaleInfo>[];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment