Skip to content

Commit 16f4411

Browse files
authored
allow full locale in .arb files (flutter#93401)
1 parent 0ef8ac9 commit 16f4411

File tree

3 files changed

+4
-107
lines changed

3 files changed

+4
-107
lines changed

packages/flutter_tools/lib/src/localizations/gen_l10n.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,6 @@ class LocalizationsGenerator {
11511151
String _generateCode() {
11521152
bool isBaseClassLocale(LocaleInfo locale, String language) {
11531153
return locale.languageCode == language
1154-
&& locale.countryCode == null
11551154
&& locale.scriptCode == null;
11561155
}
11571156

packages/flutter_tools/lib/src/localizations/gen_l10n_types.dart

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -466,20 +466,8 @@ class AppResourceBundle {
466466
// If @@locale was not defined, use the filename locale suffix.
467467
localeString = parserLocaleString;
468468
} else {
469-
// If the localeString was defined in @@locale and in the filename, verify to
470-
// see if the parsed locale matches, throw an error if it does not. This
471-
// prevents developers from confusing issues when both @@locale and
472-
// "_{locale}" is specified in the filename.
473-
if (localeString != parserLocaleString) {
474-
throw L10nException(
475-
'The locale specified in @@locale and the arb filename do not match. \n'
476-
'Please make sure that they match, since this prevents any confusion \n'
477-
'with which locale to use. Otherwise, specify the locale in either the \n'
478-
'filename of the @@locale key only.\n'
479-
'Current @@locale value: $localeString\n'
480-
'Current filename extension: $parserLocaleString'
481-
);
482-
}
469+
final Locale? parseLocale = Locale.tryParse(localeString);
470+
localeString = parseLocale.toString().replaceAll('-', '_');
483471
}
484472
break;
485473
}
@@ -539,22 +527,6 @@ class AppResourceBundleCollection {
539527
}
540528
}
541529

542-
languageToLocales.forEach((String language, List<LocaleInfo> listOfCorrespondingLocales) {
543-
final List<String> localeStrings = listOfCorrespondingLocales.map((LocaleInfo locale) {
544-
return locale.toString();
545-
}).toList();
546-
if (!localeStrings.contains(language)) {
547-
throw L10nException(
548-
'Arb file for a fallback, $language, does not exist, even though \n'
549-
'the following locale(s) exist: $listOfCorrespondingLocales. \n'
550-
'When locales specify a script code or country code, a \n'
551-
'base locale (without the script code or country code) should \n'
552-
'exist as the fallback. Please create a {fileName}_$language.arb \n'
553-
'file.'
554-
);
555-
}
556-
});
557-
558530
return AppResourceBundleCollection._(directory, localeToBundle, languageToLocales);
559531
}
560532

packages/flutter_tools/test/general.shard/generate_localizations_test.dart

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,51 +1014,6 @@ flutter:
10141014
expect(generator.supportedLocales.contains(LocaleInfo.fromString('zh')), true);
10151015
});
10161016

1017-
testWithoutContext('correctly requires @@locale property in arb file to match the filename locale suffix', () {
1018-
const String arbFileWithEnLocale = '''
1019-
{
1020-
"@@locale": "en",
1021-
"title": "Stocks",
1022-
"@title": {
1023-
"description": "Title for the Stocks application"
1024-
}
1025-
}''';
1026-
1027-
const String arbFileWithZhLocale = '''
1028-
{
1029-
"@@locale": "zh",
1030-
"title": "标题",
1031-
"@title": {
1032-
"description": "Title for the Stocks application"
1033-
}
1034-
}''';
1035-
1036-
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
1037-
..createSync(recursive: true);
1038-
l10nDirectory.childFile('app_es.arb')
1039-
.writeAsStringSync(arbFileWithEnLocale);
1040-
l10nDirectory.childFile('app_am.arb')
1041-
.writeAsStringSync(arbFileWithZhLocale);
1042-
1043-
expect(
1044-
() {
1045-
LocalizationsGenerator(
1046-
fileSystem: fs,
1047-
inputPathString: defaultL10nPathString,
1048-
outputPathString: defaultL10nPathString,
1049-
templateArbFileName: 'app_es.arb',
1050-
outputFileString: defaultOutputFileString,
1051-
classNameString: defaultClassNameString,
1052-
).loadResources();
1053-
},
1054-
throwsA(isA<L10nException>().having(
1055-
(L10nException e) => e.message,
1056-
'message',
1057-
contains('The locale specified in @@locale and the arb filename do not match.'),
1058-
)),
1059-
);
1060-
});
1061-
10621017
testWithoutContext("throws when arb file's locale could not be determined", () {
10631018
fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
10641019
..createSync(recursive: true)
@@ -1117,30 +1072,6 @@ flutter:
11171072
);
11181073
});
11191074

1120-
testWithoutContext('throws when the base locale does not exist', () {
1121-
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
1122-
..createSync(recursive: true);
1123-
l10nDirectory.childFile('app_en_US.arb')
1124-
.writeAsStringSync(singleMessageArbFileString);
1125-
1126-
expect(
1127-
() {
1128-
LocalizationsGenerator(
1129-
fileSystem: fs,
1130-
inputPathString: defaultL10nPathString,
1131-
outputPathString: defaultL10nPathString,
1132-
templateArbFileName: 'app_en_US.arb',
1133-
outputFileString: defaultOutputFileString,
1134-
classNameString: defaultClassNameString,
1135-
).loadResources();
1136-
},
1137-
throwsA(isA<L10nException>().having(
1138-
(L10nException e) => e.message,
1139-
'message',
1140-
contains('Arb file for a fallback, en, does not exist'),
1141-
)),
1142-
);
1143-
});
11441075
});
11451076

11461077
group('writeOutputFiles', () {
@@ -1262,7 +1193,7 @@ flutter:
12621193
/// **'The price of this item is: \${price}'**'''));
12631194
});
12641195

1265-
testWithoutContext('should generate a file per language', () {
1196+
testWithoutContext('should generate a file per arb', () {
12661197
const String singleEnCaMessageArbFileString = '''
12671198
{
12681199
"title": "Canadian Title"
@@ -1283,13 +1214,8 @@ flutter:
12831214
..writeOutputFiles(BufferLogger.test());
12841215

12851216
expect(fs.isFileSync(fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en.dart')), true);
1217+
expect(fs.isFileSync(fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en_CA.dart')), true);
12861218
expect(fs.isFileSync(fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en_US.dart')), false);
1287-
1288-
final String englishLocalizationsFile = fs.file(
1289-
fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en.dart')
1290-
).readAsStringSync();
1291-
expect(englishLocalizationsFile, contains('class AppLocalizationsEnCa extends AppLocalizationsEn'));
1292-
expect(englishLocalizationsFile, contains('class AppLocalizationsEn extends AppLocalizations'));
12931219
});
12941220

12951221
testWithoutContext('language imports are sorted when preferredSupportedLocaleString is given', () {

0 commit comments

Comments
 (0)