mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
allow full locale in .arb files (#93401)
This commit is contained in:
parent
0ef8ac94f1
commit
16f441188c
@ -1151,7 +1151,6 @@ class LocalizationsGenerator {
|
|||||||
String _generateCode() {
|
String _generateCode() {
|
||||||
bool isBaseClassLocale(LocaleInfo locale, String language) {
|
bool isBaseClassLocale(LocaleInfo locale, String language) {
|
||||||
return locale.languageCode == language
|
return locale.languageCode == language
|
||||||
&& locale.countryCode == null
|
|
||||||
&& locale.scriptCode == null;
|
&& locale.scriptCode == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -466,20 +466,8 @@ class AppResourceBundle {
|
|||||||
// If @@locale was not defined, use the filename locale suffix.
|
// If @@locale was not defined, use the filename locale suffix.
|
||||||
localeString = parserLocaleString;
|
localeString = parserLocaleString;
|
||||||
} else {
|
} else {
|
||||||
// If the localeString was defined in @@locale and in the filename, verify to
|
final Locale? parseLocale = Locale.tryParse(localeString);
|
||||||
// see if the parsed locale matches, throw an error if it does not. This
|
localeString = parseLocale.toString().replaceAll('-', '_');
|
||||||
// prevents developers from confusing issues when both @@locale and
|
|
||||||
// "_{locale}" is specified in the filename.
|
|
||||||
if (localeString != parserLocaleString) {
|
|
||||||
throw L10nException(
|
|
||||||
'The locale specified in @@locale and the arb filename do not match. \n'
|
|
||||||
'Please make sure that they match, since this prevents any confusion \n'
|
|
||||||
'with which locale to use. Otherwise, specify the locale in either the \n'
|
|
||||||
'filename of the @@locale key only.\n'
|
|
||||||
'Current @@locale value: $localeString\n'
|
|
||||||
'Current filename extension: $parserLocaleString'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -539,22 +527,6 @@ class AppResourceBundleCollection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
languageToLocales.forEach((String language, List<LocaleInfo> listOfCorrespondingLocales) {
|
|
||||||
final List<String> localeStrings = listOfCorrespondingLocales.map((LocaleInfo locale) {
|
|
||||||
return locale.toString();
|
|
||||||
}).toList();
|
|
||||||
if (!localeStrings.contains(language)) {
|
|
||||||
throw L10nException(
|
|
||||||
'Arb file for a fallback, $language, does not exist, even though \n'
|
|
||||||
'the following locale(s) exist: $listOfCorrespondingLocales. \n'
|
|
||||||
'When locales specify a script code or country code, a \n'
|
|
||||||
'base locale (without the script code or country code) should \n'
|
|
||||||
'exist as the fallback. Please create a {fileName}_$language.arb \n'
|
|
||||||
'file.'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return AppResourceBundleCollection._(directory, localeToBundle, languageToLocales);
|
return AppResourceBundleCollection._(directory, localeToBundle, languageToLocales);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1014,51 +1014,6 @@ flutter:
|
|||||||
expect(generator.supportedLocales.contains(LocaleInfo.fromString('zh')), true);
|
expect(generator.supportedLocales.contains(LocaleInfo.fromString('zh')), true);
|
||||||
});
|
});
|
||||||
|
|
||||||
testWithoutContext('correctly requires @@locale property in arb file to match the filename locale suffix', () {
|
|
||||||
const String arbFileWithEnLocale = '''
|
|
||||||
{
|
|
||||||
"@@locale": "en",
|
|
||||||
"title": "Stocks",
|
|
||||||
"@title": {
|
|
||||||
"description": "Title for the Stocks application"
|
|
||||||
}
|
|
||||||
}''';
|
|
||||||
|
|
||||||
const String arbFileWithZhLocale = '''
|
|
||||||
{
|
|
||||||
"@@locale": "zh",
|
|
||||||
"title": "标题",
|
|
||||||
"@title": {
|
|
||||||
"description": "Title for the Stocks application"
|
|
||||||
}
|
|
||||||
}''';
|
|
||||||
|
|
||||||
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
|
|
||||||
..createSync(recursive: true);
|
|
||||||
l10nDirectory.childFile('app_es.arb')
|
|
||||||
.writeAsStringSync(arbFileWithEnLocale);
|
|
||||||
l10nDirectory.childFile('app_am.arb')
|
|
||||||
.writeAsStringSync(arbFileWithZhLocale);
|
|
||||||
|
|
||||||
expect(
|
|
||||||
() {
|
|
||||||
LocalizationsGenerator(
|
|
||||||
fileSystem: fs,
|
|
||||||
inputPathString: defaultL10nPathString,
|
|
||||||
outputPathString: defaultL10nPathString,
|
|
||||||
templateArbFileName: 'app_es.arb',
|
|
||||||
outputFileString: defaultOutputFileString,
|
|
||||||
classNameString: defaultClassNameString,
|
|
||||||
).loadResources();
|
|
||||||
},
|
|
||||||
throwsA(isA<L10nException>().having(
|
|
||||||
(L10nException e) => e.message,
|
|
||||||
'message',
|
|
||||||
contains('The locale specified in @@locale and the arb filename do not match.'),
|
|
||||||
)),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
testWithoutContext("throws when arb file's locale could not be determined", () {
|
testWithoutContext("throws when arb file's locale could not be determined", () {
|
||||||
fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
|
fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
|
||||||
..createSync(recursive: true)
|
..createSync(recursive: true)
|
||||||
@ -1117,30 +1072,6 @@ flutter:
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
testWithoutContext('throws when the base locale does not exist', () {
|
|
||||||
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
|
|
||||||
..createSync(recursive: true);
|
|
||||||
l10nDirectory.childFile('app_en_US.arb')
|
|
||||||
.writeAsStringSync(singleMessageArbFileString);
|
|
||||||
|
|
||||||
expect(
|
|
||||||
() {
|
|
||||||
LocalizationsGenerator(
|
|
||||||
fileSystem: fs,
|
|
||||||
inputPathString: defaultL10nPathString,
|
|
||||||
outputPathString: defaultL10nPathString,
|
|
||||||
templateArbFileName: 'app_en_US.arb',
|
|
||||||
outputFileString: defaultOutputFileString,
|
|
||||||
classNameString: defaultClassNameString,
|
|
||||||
).loadResources();
|
|
||||||
},
|
|
||||||
throwsA(isA<L10nException>().having(
|
|
||||||
(L10nException e) => e.message,
|
|
||||||
'message',
|
|
||||||
contains('Arb file for a fallback, en, does not exist'),
|
|
||||||
)),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
group('writeOutputFiles', () {
|
group('writeOutputFiles', () {
|
||||||
@ -1262,7 +1193,7 @@ flutter:
|
|||||||
/// **'The price of this item is: \${price}'**'''));
|
/// **'The price of this item is: \${price}'**'''));
|
||||||
});
|
});
|
||||||
|
|
||||||
testWithoutContext('should generate a file per language', () {
|
testWithoutContext('should generate a file per arb', () {
|
||||||
const String singleEnCaMessageArbFileString = '''
|
const String singleEnCaMessageArbFileString = '''
|
||||||
{
|
{
|
||||||
"title": "Canadian Title"
|
"title": "Canadian Title"
|
||||||
@ -1283,13 +1214,8 @@ flutter:
|
|||||||
..writeOutputFiles(BufferLogger.test());
|
..writeOutputFiles(BufferLogger.test());
|
||||||
|
|
||||||
expect(fs.isFileSync(fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en.dart')), true);
|
expect(fs.isFileSync(fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en.dart')), true);
|
||||||
|
expect(fs.isFileSync(fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en_CA.dart')), true);
|
||||||
expect(fs.isFileSync(fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en_US.dart')), false);
|
expect(fs.isFileSync(fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en_US.dart')), false);
|
||||||
|
|
||||||
final String englishLocalizationsFile = fs.file(
|
|
||||||
fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en.dart')
|
|
||||||
).readAsStringSync();
|
|
||||||
expect(englishLocalizationsFile, contains('class AppLocalizationsEnCa extends AppLocalizationsEn'));
|
|
||||||
expect(englishLocalizationsFile, contains('class AppLocalizationsEn extends AppLocalizations'));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
testWithoutContext('language imports are sorted when preferredSupportedLocaleString is given', () {
|
testWithoutContext('language imports are sorted when preferredSupportedLocaleString is given', () {
|
||||||
|
Loading…
Reference in New Issue
Block a user