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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jludwig created an issue. See original summary.

jludwig’s picture

Status: Needs review » Needs work

The last submitted patch, 2: 2911606-2.patch, failed testing. View results

jludwig’s picture

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

jludwig’s picture

Status: Needs work » Needs review

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

benjifisher’s picture

Issue tags: +Nashville2018, +Novice
leanderl’s picture

#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?

msankhala’s picture

Issue tags: +Needs reroll

Patch does not applies to 8.6.x branch. Needs reroll.

msankhala’s picture

Title: Replace instances of the deprecated LOCALE_PLURAL_DELIMITER const with Drupal\Core\StringTranslation\PluralTranslatableMarkup\PluralTranslatableMarkup::DELIMITER » Replace instances of the deprecated LOCALE_PLURAL_DELIMITER const with Drupal\Core\StringTranslation\PluralTranslatableMarkup::DELIMITER
msankhala’s picture

Status: Needs review » Needs work
msankhala’s picture

Status: Needs work » Needs review
FileSize
16.22 KB

Here is updated patch for 8.6.x. I have also reverted the reordering of use statements from patch #4 because this is not in scope of this issue. If reordering of use statement 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_DELIMITER constant in Drupal\Component\Gettext\PoItem component. That should be removed in #1637662: Clean up the Gettext PO parser

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

msankhala’s picture

Here is the rerolled patch for 8.7.x.

benjifisher’s picture

Status: Needs review » Needs work

@msankhala:

Thanks for continuing to work on this!

Good point in #12 about reordering the use statements. 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 use statement, and so on. (Not quite: NumericField.php already had the use statement.) It mostly looks good. One little problem:

--- a/core/modules/views/tests/src/Functional/Plugin/NumericFormatPluralTest.php
+++ b/core/modules/views/tests/src/Functional/Plugin/NumericFormatPluralTest.php
@@ -4,8 +4,10 @@
 
 use Drupal\Component\Gettext\PoHeader;
 use Drupal\file\Entity\File;
+use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
 use Drupal\Tests\views\Functional\ViewTestBase;
 
+
 /**
  * Tests the creation of numeric fields.
  *

Why add a blank line?

I also searched for LOCALE_PLURAL_DELIMITER in the code base after applying the patch:

$ grep -ril LOCALE_PLURAL_DELIMITER core
core/includes/common.inc
core/lib/Drupal/Component/Gettext/PoMemoryWriter.php
core/lib/Drupal/Component/Gettext/PoItem.php

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 use statement 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.

benjifisher’s picture

One other thing:

--- a/core/modules/system/system.module
+++ b/core/modules/system/system.module
@@ -27,6 +27,7 @@
 use Drupal\user\UserInterface;
 use Symfony\Component\HttpFoundation\RedirectResponse;
 use GuzzleHttp\Exception\RequestException;
+use Drupal\Core\StringTranslation\PluralTranslatableMarkup;

We agree that alphabetizing all the use statements in system.module is out of scope for this issue, but let's not make it worse than it is. Why not add the line between these two?

use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Core\Routing\RouteMatchInterface;

Same point in NumericFormatPluralTest.php. I already quoted the relevant part (hunk) of the patch in my previous comment.

imalabya’s picture

Status: Needs work » Needs review

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

imalabya’s picture

benjifisher’s picture

Status: Needs review » Needs work

@imalabya:

Thanks, the two changes that you made look good.

In #16, I wrote.

Same point in NumericFormatPluralTest.php. I already quoted the relevant part (hunk) of the patch in my previous comment.

Referring to the patch in #17, the relevant lines are

--- a/core/modules/views/tests/src/Functional/Plugin/NumericFormatPluralTest.php
+++ b/core/modules/views/tests/src/Functional/Plugin/NumericFormatPluralTest.php
@@ -4,6 +4,7 @@
 
 use Drupal\Component\Gettext\PoHeader;
 use Drupal\file\Entity\File;
+use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
 use Drupal\Tests\views\Functional\ViewTestBase;

Again, changing the order of existing use statements 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,

If you prefer not to make that change as part of this issue, then please create a follow-up issue to handle it.

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.

brathbone’s picture

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

Status: Needs review » Needs work

The last submitted patch, 20: replace-LOCALE_PLURAL_DELIMITER-2911606-20.patch, failed testing. View results

andypost’s picture

Status: Needs work » Reviewed & tested by the community
Related issues: +#1637662: Clean up the Gettext PO parser

Grep shows that Components "poisoned" by common.inc constant still - looks it needs follow-up to fix

core8$ git grep LOCALE_PLURAL_DELIMITER
core/includes/common.inc:133:const LOCALE_PLURAL_DELIMITER = PluralTranslatableMarkup::DELIMITER;
core/lib/Drupal/Component/Gettext/PoItem.php:189:        strpos($this->source, LOCALE_PLURAL_DELIMITER) !== FALSE) {
core/lib/Drupal/Component/Gettext/PoItem.php:190:      $this->setSource(explode(LOCALE_PLURAL_DELIMITER, $this->source));
core/lib/Drupal/Component/Gettext/PoItem.php:191:      $this->setTranslation(explode(LOCALE_PLURAL_DELIMITER, $this->translation));
core/lib/Drupal/Component/Gettext/PoMemoryWriter.php:29:      $item->setSource(implode(LOCALE_PLURAL_DELIMITER, $item->getSource()));
core/lib/Drupal/Component/Gettext/PoMemoryWriter.php:30:      $item->setTranslation(implode(LOCALE_PLURAL_DELIMITER, $item->getTranslation()));
andypost’s picture

reroll because of system.module's hunk

The last submitted patch, 12: replace-LOCALE_PLURAL_DELIMITER-2911606-11.patch, failed testing. View results

andypost’s picture

In 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::DELIMITER for \Drupal\Component\Gettext\PoItem::DELIMITER it needs change record

PS: no interdiff because it bigger then patch

andypost’s picture

FileSize
622 bytes
25.13 KB

One more leftover

brathbone’s picture

Status: Needs review » Reviewed & tested by the community

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

andypost’s picture

Issue tags: +@deprecated, +Kill includes
andypost’s picture

Title: Replace instances of the deprecated LOCALE_PLURAL_DELIMITER const with Drupal\Core\StringTranslation\PluralTranslatableMarkup::DELIMITER » Replace usage of the deprecated LOCALE_PLURAL_DELIMITER constant
Issue summary: View changes
Issue tags: -Needs change record
alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 0a6e463 and pushed to 8.7.x. Thanks!

  • alexpott committed 0a6e463 on 8.7.x
    Issue #2911606 by andypost, jludwig, msankhala, brathbone, imalabya,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.