mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Fix error when generating pt_BR localizations (#175832)
Fixes #138609 The original logic of splitting by underscore doesn't work for locales like pt_BR where the part after the underscore is itself a valid locale. <!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Ben Konyi <bkonyi@google.com>
This commit is contained in:
parent
284dbdfb65
commit
8fed33ab3e
@ -638,40 +638,44 @@ class AppResourceBundle {
|
||||
// Look for the first instance of an ISO 639-1 language code, matching exactly.
|
||||
final String fileName = file.fileSystem.path.basenameWithoutExtension(file.path);
|
||||
|
||||
for (var index = 0; index < fileName.length; index += 1) {
|
||||
// If an underscore was found, check if locale string follows.
|
||||
if (fileName[index] == '_') {
|
||||
// If Locale.tryParse fails, it returns null.
|
||||
final Locale? parserResult = Locale.tryParse(fileName.substring(index + 1));
|
||||
// If the parserResult is not an actual locale identifier, end the loop.
|
||||
if (parserResult != null && _iso639Languages.contains(parserResult.languageCode)) {
|
||||
// The parsed result uses dashes ('-'), but we want underscores ('_').
|
||||
final String parserLocaleString = parserResult.toString().replaceAll('-', '_');
|
||||
// Try to parse a locale from the filename.
|
||||
String? fileNameLocale;
|
||||
|
||||
if (localeString == null) {
|
||||
// If @@locale was not defined, use the filename locale suffix.
|
||||
localeString = parserLocaleString;
|
||||
} else {
|
||||
// If the localeString was defined in @@locale and in the filename, verify to
|
||||
// see if the parsed locale matches, throw an error if it does not. This
|
||||
// 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 or the @@locale key only.\n'
|
||||
'Current @@locale value: $localeString\n'
|
||||
'Current filename extension: $parserLocaleString',
|
||||
);
|
||||
}
|
||||
// First, try parsing the whole filename as a locale.
|
||||
Locale? parserResult = Locale.tryParse(fileName);
|
||||
if (parserResult != null && _iso639Languages.contains(parserResult.languageCode)) {
|
||||
fileNameLocale = parserResult.toString().replaceAll('-', '_');
|
||||
} else {
|
||||
// If that fails, look for underscores and try parsing after each one.
|
||||
for (var index = 0; index < fileName.length; index += 1) {
|
||||
if (fileName[index] == '_') {
|
||||
parserResult = Locale.tryParse(fileName.substring(index + 1));
|
||||
if (parserResult != null && _iso639Languages.contains(parserResult.languageCode)) {
|
||||
// The parsed result uses dashes ('-'), but we want underscores ('_').
|
||||
fileNameLocale = parserResult.toString().replaceAll('-', '_');
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (localeString != null) {
|
||||
// If @@locale is provided, check if there's a conflicting locale in the filename.
|
||||
if (fileNameLocale != null && localeString != fileNameLocale) {
|
||||
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 or the @@locale key only.\n'
|
||||
'Current @@locale value: $localeString\n'
|
||||
'Current filename extension: $fileNameLocale',
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// If @@locale is not provided, use the locale parsed from the filename.
|
||||
localeString = fileNameLocale;
|
||||
}
|
||||
|
||||
if (localeString == null) {
|
||||
throw L10nException(
|
||||
"The following .arb file's locale could not be determined: \n"
|
||||
|
||||
@ -1210,6 +1210,39 @@ class AppLocalizationsEn extends AppLocalizations {
|
||||
},
|
||||
);
|
||||
|
||||
testWithoutContext('handles pt_BR locale correctly', () {
|
||||
const ptArbFileString = '''
|
||||
{
|
||||
"@@locale": "pt",
|
||||
"test": "test_PT"
|
||||
}''';
|
||||
|
||||
const ptBrArbFileString = '''
|
||||
{
|
||||
"@@locale": "pt_BR",
|
||||
"test": "test_PT_BR"
|
||||
}''';
|
||||
|
||||
fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
|
||||
..createSync(recursive: true)
|
||||
..childFile('pt.arb').writeAsStringSync(ptArbFileString)
|
||||
..childFile('pt_BR.arb').writeAsStringSync(ptBrArbFileString);
|
||||
|
||||
final generator = LocalizationsGenerator(
|
||||
fileSystem: fs,
|
||||
inputPathString: defaultL10nPath,
|
||||
outputPathString: defaultL10nPath,
|
||||
templateArbFileName: 'pt.arb',
|
||||
outputFileString: defaultOutputFileString,
|
||||
classNameString: defaultClassNameString,
|
||||
projectPathString: fs.currentDirectory.path,
|
||||
logger: logger,
|
||||
)..loadResources();
|
||||
|
||||
expect(generator.supportedLocales.contains(LocaleInfo.fromString('pt')), true);
|
||||
expect(generator.supportedLocales.contains(LocaleInfo.fromString('pt_BR')), true);
|
||||
});
|
||||
|
||||
testWithoutContext("throws when arb file's locale could not be determined", () {
|
||||
fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
|
||||
..createSync(recursive: true)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user