Skip to content

Handle plural gen_l10n regular placeholders and DateTime placeholders#47483

Merged
shihaohong merged 12 commits intoflutter:masterfrom
shihaohong:plural-placeholders
Dec 21, 2019
Merged

Handle plural gen_l10n regular placeholders and DateTime placeholders#47483
shihaohong merged 12 commits intoflutter:masterfrom
shihaohong:plural-placeholders

Conversation

@shihaohong
Copy link
Copy Markdown
Contributor

@shihaohong shihaohong commented Dec 19, 2019

Description

Adds plural placeholder handling in the gen_l10n tool. It also accounts for plural DateTime processing.

Related Issues

Fixes #47368
Related to #47008

Tests

I added the following tests:

  • A test to ensure that the count placeholder and a regular placeholder are properly differentiated.
  • A test to ensure that DateTime processing happens for plural messages
  • A few tests to ensure that that placeholders resource attribute for plural messages are properly formatted and that appropriate errors are thrown when they are not

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I signed the CLA.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I updated/added relevant documentation (doc comments with ///).
  • All existing and new tests are passing.
  • The analyzer (flutter analyze --flutter-repo) does not report any problems on my PR.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Did any tests fail when you ran them? Please read Handling breaking changes.

  • No, no existing tests failed, so this is not a breaking change.
  • Yes, this is a breaking change. If not, delete the remainder of this section.
    • I wrote a design doc: https://flutter.dev/go/template Replace this with a link to your design doc's short link
    • I got input from the developer relations team, specifically from: Replace with the names of who gave advice
    • I wrote a migration guide: Replace with a link to your migration guide

@shihaohong shihaohong added the a: internationalization Supporting other languages or locales. (aka i18n) label Dec 19, 2019
@fluttergithubbot fluttergithubbot added the c: contributor-productivity Team-specific productivity, code health, technical debt. label Dec 19, 2019
@HansMuller
Copy link
Copy Markdown
Contributor

Date valued parameters?

@shihaohong
Copy link
Copy Markdown
Contributor Author

@HansMuller Let me go ahead date params for plurals a part of this PR as well

Comment thread dev/tools/localization/gen_l10n.dart Outdated
return 'Object $parameter';
}).toList();
}
return <String>[];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we get here, then we're not going to generate a correct plural method? Should exit with an error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


// Used to determine which placeholder is the plural count placeholder
final String resourceValue = bundle[key] as String;
final String countPlaceholder = resourceValue.split(',')[0].substring(1);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail in a confusing way if the plurals string isn't formatted correctly. Maybe check the resourceValue string first, and exit with an error if it doesn't appear to be formatted correctly, i.e. if it looks like resourceValue.split(',')[0].substring(1) would fail.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried creating an exception here and writing a test to error out if:

  1. If the first character was not '{'
  2. If resourceValue.split(',').length <= 2

However, it seems that the string is not parsed as a plural message earlier in the tool (it is instead treated as a simple message). So, it never reaches these checks I added. Can you think of any other situation where the plurals string might be formatted incorrectly here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right - we only get here if the resource matches pluralValueRE, which requires the string to contain fooVar,plural. There are probably other ways for plural strings to go wrong, but I don't think that needs to be addressed in this PR.

Comment thread dev/tools/localization/gen_l10n.dart Outdated
static RegExp arbFilenameLocaleRE = RegExp(r'^[^_]*_(\w+)\.arb$');
static RegExp arbFilenameRE = RegExp(r'(\w+)\.arb$');
static RegExp pluralValueRE = RegExp(r'^\s*\{[\w\s,]*,\s*plural\s*,');
static RegExp pluralValueRE = RegExp(r'^\s*\{[\w\s]*,\s*plural\s*,[\s\S]*\}');
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of maybe making this a little more precise to help avoid the plural string formatting issue mentioned before. However, I was wondering if this would somehow make the match too strict? @HansMuller

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is OK as it is. We'll have to see what failure modes actually crop up in real apps before adding more checking.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I'll change it to be what it was before

@shihaohong shihaohong changed the title Plural placeholders Handle plural gen_l10n regular placeholders and DateTime placeholders Dec 20, 2019
Copy link
Copy Markdown
Contributor

@HansMuller HansMuller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM


// Used to determine which placeholder is the plural count placeholder
final String resourceValue = bundle[key] as String;
final String countPlaceholder = resourceValue.split(',')[0].substring(1);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right - we only get here if the resource matches pluralValueRE, which requires the string to contain fooVar,plural. There are probably other ways for plural strings to go wrong, but I don't think that needs to be addressed in this PR.

Comment thread dev/tools/localization/gen_l10n.dart Outdated
static RegExp arbFilenameLocaleRE = RegExp(r'^[^_]*_(\w+)\.arb$');
static RegExp arbFilenameRE = RegExp(r'(\w+)\.arb$');
static RegExp pluralValueRE = RegExp(r'^\s*\{[\w\s,]*,\s*plural\s*,');
static RegExp pluralValueRE = RegExp(r'^\s*\{[\w\s]*,\s*plural\s*,[\s\S]*\}');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is OK as it is. We'll have to see what failure modes actually crop up in real apps before adding more checking.

@shihaohong shihaohong merged commit 88cd113 into flutter:master Dec 21, 2019
@shihaohong shihaohong deleted the plural-placeholders branch December 21, 2019 00:22
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Aug 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

a: internationalization Supporting other languages or locales. (aka i18n) c: contributor-productivity Team-specific productivity, code health, technical debt.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gen_l10n tool should handle placeholders for plural messages

4 participants