@@ -566,13 +566,18 @@ class Message {
566
566
}
567
567
}
568
568
569
- // Represents the contents of one ARB file.
569
+ /// Represents the contents of one ARB file.
570
570
class AppResourceBundle {
571
+ /// Assuming that the caller has verified that the file exists and is readable.
571
572
factory AppResourceBundle (File file) {
572
- // Assuming that the caller has verified that the file exists and is readable.
573
- Map <String , Object ?> resources;
573
+ final Map <String , Object ?> resources;
574
574
try {
575
- resources = json.decode (file.readAsStringSync ()) as Map <String , Object ?>;
575
+ final String content = file.readAsStringSync ().trim ();
576
+ if (content.isEmpty) {
577
+ resources = < String , Object ? > {};
578
+ } else {
579
+ resources = json.decode (content) as Map <String , Object ?>;
580
+ }
576
581
} on FormatException catch (e) {
577
582
throw L10nException (
578
583
'The arb file ${file .path } has the following formatting issue: \n '
@@ -657,20 +662,26 @@ class AppResourceBundleCollection {
657
662
final RegExp filenameRE = RegExp (r'(\w+)\.arb$' );
658
663
final Map <LocaleInfo , AppResourceBundle > localeToBundle = < LocaleInfo , AppResourceBundle > {};
659
664
final Map <String , List <LocaleInfo >> languageToLocales = < String , List <LocaleInfo >> {};
660
- final List <File > files = directory.listSync ().whereType <File >().toList ()..sort (sortFilesByPath);
665
+ // We require the list of files to be sorted so that
666
+ // "languageToLocales[bundle.locale.languageCode]" is not null
667
+ // by the time we handle locales with country codes.
668
+ final List <File > files = directory
669
+ .listSync ()
670
+ .whereType <File >()
671
+ .where ((File e) => filenameRE.hasMatch (e.path))
672
+ .toList ()
673
+ ..sort (sortFilesByPath);
661
674
for (final File file in files) {
662
- if (filenameRE.hasMatch (file.path)) {
663
- final AppResourceBundle bundle = AppResourceBundle (file);
664
- if (localeToBundle[bundle.locale] != null ) {
665
- throw L10nException (
666
- "Multiple arb files with the same '${bundle .locale }' locale detected. \n "
667
- 'Ensure that there is exactly one arb file for each locale.'
668
- );
669
- }
670
- localeToBundle[bundle.locale] = bundle;
671
- languageToLocales[bundle.locale.languageCode] ?? = < LocaleInfo > [];
672
- languageToLocales[bundle.locale.languageCode]! .add (bundle.locale);
675
+ final AppResourceBundle bundle = AppResourceBundle (file);
676
+ if (localeToBundle[bundle.locale] != null ) {
677
+ throw L10nException (
678
+ "Multiple arb files with the same '${bundle .locale }' locale detected. \n "
679
+ 'Ensure that there is exactly one arb file for each locale.'
680
+ );
673
681
}
682
+ localeToBundle[bundle.locale] = bundle;
683
+ languageToLocales[bundle.locale.languageCode] ?? = < LocaleInfo > [];
684
+ languageToLocales[bundle.locale.languageCode]! .add (bundle.locale);
674
685
}
675
686
676
687
languageToLocales.forEach ((String language, List <LocaleInfo > listOfCorrespondingLocales) {
0 commit comments