forked from firka/flutter
[gen_l10n] Throw a specified exception when the language code is invalid (#102431)
This commit is contained in:
@@ -7,6 +7,7 @@ import 'package:yaml/yaml.dart';
|
||||
|
||||
import '../base/file_system.dart';
|
||||
import '../base/logger.dart';
|
||||
import 'gen_l10n_types.dart';
|
||||
import 'language_subtag_registry.dart';
|
||||
|
||||
typedef HeaderGenerator = String Function(String regenerateInstructions);
|
||||
@@ -215,8 +216,15 @@ void precacheLanguageAndRegionTags() {
|
||||
String describeLocale(String tag) {
|
||||
final List<String> subtags = tag.split('_');
|
||||
assert(subtags.isNotEmpty);
|
||||
assert(_languages.containsKey(subtags[0]));
|
||||
final String language = _languages[subtags[0]]!;
|
||||
final String languageCode = subtags[0];
|
||||
if (!_languages.containsKey(languageCode)) {
|
||||
throw L10nException(
|
||||
'"$languageCode" is not a supported language code.\n'
|
||||
'See https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry '
|
||||
'for the supported list.',
|
||||
);
|
||||
}
|
||||
final String language = _languages[languageCode]!;
|
||||
String output = language;
|
||||
String? region;
|
||||
String? script;
|
||||
|
||||
@@ -2595,6 +2595,39 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
testWithoutContext('throws when the language code is not supported', () {
|
||||
const String arbFileWithInvalidCode = '''
|
||||
{
|
||||
"@@locale": "invalid",
|
||||
"title": "invalid"
|
||||
}''';
|
||||
|
||||
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
|
||||
..createSync(recursive: true);
|
||||
l10nDirectory.childFile('app_invalid.arb')
|
||||
.writeAsStringSync(arbFileWithInvalidCode);
|
||||
|
||||
expect(
|
||||
() {
|
||||
LocalizationsGenerator(
|
||||
fileSystem: fs,
|
||||
inputPathString: defaultL10nPathString,
|
||||
outputPathString: defaultL10nPathString,
|
||||
templateArbFileName: 'app_invalid.arb',
|
||||
outputFileString: defaultOutputFileString,
|
||||
classNameString: defaultClassNameString,
|
||||
)
|
||||
..loadResources()
|
||||
..writeOutputFiles(BufferLogger.test());
|
||||
},
|
||||
throwsA(isA<L10nException>().having(
|
||||
(L10nException e) => e.message,
|
||||
'message',
|
||||
contains('"invalid" is not a supported language code.'),
|
||||
)),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
testWithoutContext('should generate a valid pubspec.yaml file when using synthetic package if it does not already exist', () {
|
||||
|
||||
Reference in New Issue
Block a user