Handle plural gen_l10n regular placeholders and DateTime placeholders#47483
Handle plural gen_l10n regular placeholders and DateTime placeholders#47483shihaohong merged 12 commits intoflutter:masterfrom shihaohong:plural-placeholders
Conversation
|
Date valued parameters? |
|
@HansMuller Let me go ahead date params for plurals a part of this PR as well |
| return 'Object $parameter'; | ||
| }).toList(); | ||
| } | ||
| return <String>[]; |
There was a problem hiding this comment.
If we get here, then we're not going to generate a correct plural method? Should exit with an error.
|
|
||
| // 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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I tried creating an exception here and writing a test to error out if:
- If the first character was not '{'
- 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?
There was a problem hiding this comment.
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.
| 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]*\}'); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Sounds good, I'll change it to be what it was before
|
|
||
| // 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); |
There was a problem hiding this comment.
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.
| 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]*\}'); |
There was a problem hiding this comment.
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.
Description
Adds plural placeholder handling in the
gen_l10ntool. It also accounts for plural DateTime processing.Related Issues
Fixes #47368
Related to #47008
Tests
I added the following tests:
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.///).flutter analyze --flutter-repo) does not report any problems on my PR.Breaking Change
Did any tests fail when you ran them? Please read Handling breaking changes.