Problem/Motivation
Various parts of code are using the deprecated LOCALE_PLURAL_DELIMITER const instead of Drupal\Core\StringTranslation\PluralTranslatableMarkup\PluralTranslatableMarkup::DELIMITER which the former just points to anyway.
The reason it should be changed sooner rather than later is that this makes unit testing certain classes much more difficult. For example, when trying to unit test a subclass of Drupal\views\Plugin\views\field\NumericField, I received the following error:
Use of undefined constant LOCALE_PLURAL_DELIMITER - assumed 'LOCALE_PLURAL_DELIMITER'
There are ways around this, but the work has already been done and this issue should just be fixed in core.
Proposed resolution
- Add \Drupal\Component\Gettext\PoItem::DELIMITER as lowest level component using it
- Deprecate Drupal\Core\StringTranslation\PluralTranslatableMarkup\PluralTranslatableMarkup::DELIMITER and replace its usage with new constant
- Replace all usage of LOCALE_PLURAL_DELIMITER with \Drupal\Component\Gettext\PoItem::DELIMITER. The former already points to the latter anyway, so the current tests should suffice and no additional work should be needed.
| Comment | File | Size | Author |
|---|---|---|---|
| #27 | 2911606-27.patch | 25.13 KB | andypost |
| #27 | interdiff.txt | 622 bytes | andypost |
| #24 | replace_LOCALE_PLURAL_DELIMITER-2911606-24.patch | 16.22 KB | andypost |
Comments
Comment #2
jludwig commentedComment #4
jludwig commentedRemoved the update from the component files because a test was failing for those.
Note that this is just sweeping the problem under the rug. Those components still depend on Drupal core, but the test doesn't know that because it is only checking for Drupal\Core namespacing.
I'm adding comments to https://www.drupal.org/node/1637662 regarding this, because that issue is for cleaning up Drupal\Component\Gettext\PoItem anyway.
Also: this patch cleanly applies to 8.4.x as well, for anyone else running into the unit testing issue on that core version.
Comment #5
jludwig commentedComment #7
benjifisherComment #8
leanderl commented#Nashville2018, #Novice
It would be helpful with a "how to review" instruction in this issue.
Is it enough to apply the patch and check that the installation appears to be working? How should it be tested/reviewed?
Comment #9
msankhala commentedPatch does not applies to 8.6.x branch. Needs reroll.
Comment #10
msankhala commentedComment #11
msankhala commentedComment #12
msankhala commentedHere is updated patch for 8.6.x. I have also reverted the reordering of
usestatements from patch #4 because this is not in scope of this issue. If reordering ofusestatement is necessary then it should be done in separate issue otherwise this will create scop creep.As mentioned in #4 the use of
LOCALE_PLURAL_DELIMITERconstant inDrupal\Component\Gettext\PoItemcomponent. That should be removed in #1637662: Clean up the Gettext PO parserComment #14
msankhala commentedHere is the rerolled patch for 8.7.x.
Comment #15
benjifisher@msankhala:
Thanks for continuing to work on this!
Good point in #12 about reordering the
usestatements. That should be fixed, but not as part of this issue.I reviewed the patch, making sure that it correctly spells the old and new constants, that every file gets a
usestatement, and so on. (Not quite: NumericField.php already had theusestatement.) It mostly looks good. One little problem:Why add a blank line?
I also searched for
LOCALE_PLURAL_DELIMITERin the code base after applying the patch:We need to keep the reference in common.inc (with the deprecation notice).
We need to get rid of the references in the Gettext component. The only question is whether we should do it as part of this issue.
I think the simplest solution is to define a class constant DELIMITER (or maybe PLURAL_DELIMITER) in the PoItem class. Then use it in both classes. They are in the same namespace, so no
usestatement is needed. I see that, in PoMemoryWriter, LOCALE_PLURAL_DELIMITER appears only twice, in a function that has a PoItem parameter. That means the PoMemoryWriter class already "knows about" the PoItem class.If you prefer not to make that change as part of this issue, then please create a follow-up issue to handle it. If you do, then I might postpone this issue on the "follow-up". Either way, please remove the stray blank line I mentioned above.
Comment #16
benjifisherOne other thing:
We agree that alphabetizing all the
usestatements insystem.moduleis out of scope for this issue, but let's not make it worse than it is. Why not add the line between these two?Same point in
NumericFormatPluralTest.php. I already quoted the relevant part (hunk) of the patch in my previous comment.Comment #17
imalabya@benjifisher have removed the stray blank line and added the use statement at the place you suggested.
Regarding the changes in the PoItem and PoMemoryWriter should a follow-up issue IMO.
Comment #18
imalabyaComment #19
benjifisher@imalabya:
Thanks, the two changes that you made look good.
In #16, I wrote.
Referring to the patch in #17, the relevant lines are
Again, changing the order of existing
usestatements is out of scope for this issue, but we should try not to make things worse with the lines we add. In this case, that means we should preserver the (case-insensitive) alphabetical order.In #15, I wrote,
referring to the changes to the Gettext component. In #17, you said that these changes should be in a follow-up issue, but you did not create that issue.
Comment #20
brathbone commented@benjifisher I've applied the changes requested in #19 and #15:
-I put the 'use' statements in NumericFormatPluralTest.php into alphabetical order.
-I created a class constant in the PoItem class.
-I replaced the two instances of LOCALE_PLURAL_DELIMITER in PoMemoryWriter.php
-I replaced the three instances of LOCALE_PLURAL_DELIMITER in PoItem.php
For the comment on the class constant in the PoItem class, I followed the lead of the PERMANENT constant in the Cache class in Drupal\Core\Cache and just repeated the first part of the comment on the DELIMITER constant in PluralTranslatableMarkup.
Happy to make further adjustments if needed.
Comment #22
rajeevchoudhary commentedPlease follow this patch up.
Comment #23
andypostGrep shows that Components "poisoned" by common.inc constant still - looks it needs follow-up to fix
Comment #24
andypostreroll because of system.module's hunk
Comment #26
andypostIn this issue we should replace all deprecated usage including the component
Digging more related issues it becomes clear that here it needs to move definition of delimiter to Gettext component
Core using delimiter in following places
- .po files reader/writer - conditionally flipping source and translation depending on number of plurals - #2918489: Plurals are not exported correctly when exporting source translations
- storing plurals in config - also using flip/flop on export and to create translations- #2545730: Misuse of formatPlural() in Numeric field prefix/suffix
- UI elements and controls to edit translations
So I went ahead and using a patch from #1637662: Clean up the Gettext PO parser
Which deprecates
\Drupal\Core\StringTranslation\PluralTranslatableMarkup::DELIMITERfor\Drupal\Component\Gettext\PoItem::DELIMITERit needs change recordPS: no interdiff because it bigger then patch
Comment #27
andypostOne more leftover
Comment #28
brathbone commentedI applied the patch in #27 and ran "grep -ril LOCALE_PLURAL_DELIMITER core" from the root of my test installation. The command only returned the single instance of LOCALE_PLURAL_DELIMITER in core/includes/common.inc.
Comment #29
andypostComment #30
andypostFiled CR https://www.drupal.org/node/3014611
Fixed summary and title
PS: the deprecated constant was added in #2570107: Make format_plural() return a PluralTranslatableString object to remove reliance on a static, unpredictable safe list
Comment #31
alexpottCommitted 0a6e463 and pushed to 8.7.x. Thanks!