Skip to content
Snippets Groups Projects
Unverified Commit 0cc606cc authored by Bent Hillerkus's avatar Bent Hillerkus Committed by GitHub
Browse files

[Gen-l10n] Infer placeholder types on both templates and localizations (#163690)

<!--
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
-->

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].

<!-- 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: default avatarBen Konyi <bkonyi@google.com>
parent 02bdfd2a
No related merge requests found
......@@ -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<String, Placeholder> 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) {
......
......@@ -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(<String, String>{
'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<String>());
},
);
});
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.',
),
),
),
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment