From 0cc606cc5fce512f5c54cabc5c5aee58cfbdf010 Mon Sep 17 00:00:00 2001 From: Bent Hillerkus Date: Mon, 31 Mar 2025 22:00:08 +0200 Subject: [PATCH] [Gen-l10n] Infer placeholder types on both templates and localizations (#163690) This fixes https://github.com/flutter/flutter/issues/163627 by trying to the infer the type of a placeholder on both the template messages and their localised versions. This way, if the placeholder is being referenced in the localisation, but the type is omitted in both the template and the localisation, the check for if the template and the localisation have the same type will not fail anymore. ## 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. - [x] 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]. - [x] 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]. [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 --- .../lib/src/localizations/gen_l10n_types.dart | 4 +- .../generate_localizations_test.dart | 46 ++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/packages/flutter_tools/lib/src/localizations/gen_l10n_types.dart b/packages/flutter_tools/lib/src/localizations/gen_l10n_types.dart index 96e9451e00..4be34a3dd4 100644 --- a/packages/flutter_tools/lib/src/localizations/gen_l10n_types.dart +++ b/packages/flutter_tools/lib/src/localizations/gen_l10n_types.dart @@ -584,7 +584,9 @@ class Message { return x && !y && !z || !x && y && !z || !x && !y && z || !x && !y && !z; } - for (final Placeholder placeholder in templatePlaceholders.values) { + for (final Placeholder placeholder in templatePlaceholders.values.followedBy( + localePlaceholders.values.expand((Map e) => e.values), + )) { if (!atMostOneOf(placeholder.isPlural, placeholder.isDateTime, placeholder.isSelect)) { throw L10nException('Placeholder is used as plural/select/datetime in certain languages.'); } else if (placeholder.isPlural) { diff --git a/packages/flutter_tools/test/general.shard/generate_localizations_test.dart b/packages/flutter_tools/test/general.shard/generate_localizations_test.dart index 7d3832518d..05a47e334f 100644 --- a/packages/flutter_tools/test/general.shard/generate_localizations_test.dart +++ b/packages/flutter_tools/test/general.shard/generate_localizations_test.dart @@ -1689,6 +1689,50 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e expect(content, contains("String get helloWorld => 'Hello {name}'")); }, ); + + // Regression test for https://github.com/flutter/flutter/issues/163627 + // + // If placeholders have no explicit type (like `int` or `String`) set + // their type can be inferred. + // + // Later in the pipeline it is ensured that each locales placeholder types + // matches the definitions in the template. + // + // If only the types of the template had been inferred, + // and not for the translation there would be a mismatch: + // in this case `num` for count and `null` (the default), which is incompatible + // and `getSyntheticGeneratedFileContent` would throw an exception. + // + // This test ensures that both template and locale can be equally partially defined + // in the arb. + testWithoutContext( + 'translation placeholder type definitions can be inferred for plurals', + () { + setupLocalizations({ + 'en': ''' +{ + "helloWorld": "{count, plural, one{Hello World!} other{Hello Worlds!}}", + "@helloWorld": { + "description": "The conventional newborn programmer greeting", + "placeholders": { + "count": {} + } + } +}''', + 'de': ''' +{ + "helloWorld": "{count, plural, one{Hallo Welt!} other{Hallo Welten!}}", + "@helloWorld": { + "description": "The conventional newborn programmer greeting", + "placeholders": { + "count": {} + } + } +}''', + }); + expect(getSyntheticGeneratedFileContent(locale: 'en'), isA()); + }, + ); }); group('DateTime tests', () { @@ -2167,7 +2211,7 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e (L10nException e) => e.message, 'message', contains( - 'The placeholder, springStartDate, has its "type" resource attribute set to the "null" type in locale "ja", but it is "DateTime" in the template placeholder.', + 'The placeholder, springStartDate, has its "type" resource attribute set to the "Object" type in locale "ja", but it is "DateTime" in the template placeholder.', ), ), ),