Follow-up to #2573975: function_exists check in PluralTranslatableString is wrong and #2571375: Remove TranslationManager dependency from LanguageManager

Postponed on:
#3590050: Deprecate and replace locale_status related functions
#3581303: Convert locale batch callbacks
#3037156: Modernize locale history functions

Problem/Motivation

Locale module has many functions let's review and deprecate:

  • locale_get_plural

Steps to reproduce

Notes about locale_get_plural

  1. Enabled Language and Interface Translation
  2. Added Romanian
  3. Found some text that uses plurals: Module %name has been enabled.
  4. Changed the plurals for these for Romanian
  5. Enabled some modules (1 module, 2 modules, and 3 modules)
  6. The messages were as expected (see screenshots below) (though I'm not sure what the 2. plural is for in Romanian)

Proposed resolution

TBD

Remaining tasks

User interface changes

API changes

Data model changes

N/A

CommentFileSizeAuthor
#84 2660338-nr-bot_ssmjh3ak.txt91 bytesneeds-review-queue-bot
#5 2660338-5.patch4.88 KBclaudiu.cristea
#6 2660338-6.patch4.84 KBclaudiu.cristea
#6 interdiff.txt903 bytesclaudiu.cristea
#9 interdiff.txt5.27 KBclaudiu.cristea
#9 2660338-9.patch10.11 KBclaudiu.cristea
#11 drupal-plural-module-update-text.png284.29 KBkristen pol
#11 drupal-plural-ro-singular.png164.19 KBkristen pol
#11 drupal-plural-ro-first-2-modules.png169.19 KBkristen pol
#11 drupal-plural-ro-first-3-modules.png182.45 KBkristen pol
#15 drupal-plural_variant_index-2660338-15.patch10.03 KBkristen pol
#15 interdiff-2660338-9-15.txt4.12 KBkristen pol
#23 2660338-23.patch11.58 KBalexpott
#27 2660338-27.patch11.58 KBandypost
#30 interdiff.txt1.9 KBandypost
#30 2660338-29.patch13.48 KBandypost
#32 interdiff.txt665 bytesandypost
#32 2660338-32.patch13.67 KBandypost
#34 2660338-32-reroll.patch13.68 KBandypost
#38 2660338-38.patch13.69 KBridhimaabrol24
#39 2660338-39.patch14.3 KBridhimaabrol24
#39 interdiff_38-39.txt624 bytesridhimaabrol24
#44 2660338-44.patch14.17 KBadityasingh
#44 interdiff_39-44.txt2.33 KBadityasingh
#47 Screen Shot 2020-07-10 at 3.33.21 PM.png239.26 KBtanubansal
#47 Screen Shot 2020-07-10 at 3.34.28 PM.png328.69 KBtanubansal
#47 Screen Shot 2020-07-10 at 3.39.47 PM.png326.72 KBtanubansal
#51 interdiff.txt930 bytesandypost
#51 2660338-51.patch16.91 KBandypost
#55 2660338-55.patch16.9 KBravi.shankar
#55 rawdiff.txt4.51 KBravi.shankar
#56 2660338-56.patch16.93 KBravi.shankar
#56 interdiff_55-56.txt1.34 KBravi.shankar
#59 2660338-59.patch18.07 KBmohit_aghera
#59 interdiff-2660338-56-59.txt1.75 KBmohit_aghera
#66 2660338-66.patch18.04 KBravi.shankar
#66 reroll_diff_59-66.txt4.48 KBravi.shankar

Issue fork drupal-2660338

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

alexpott created an issue. See original summary.

alexpott’s picture

Priority: Major » Normal

No that the major issue is fixed. I think this can be treated as a normal bug.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

claudiu.cristea’s picture

claudiu.cristea’s picture

Status: Needs work » Needs review
StatusFileSize
new4.88 KB

Here's patch.

claudiu.cristea’s picture

StatusFileSize
new4.84 KB
new903 bytes

Small improvement.

The last submitted patch, 5: 2660338-5.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 6: 2660338-6.patch, failed testing.

claudiu.cristea’s picture

Status: Needs work » Needs review
StatusFileSize
new5.27 KB
new10.11 KB

Added the missed service in unit tests container.

kristen pol’s picture

Thanks for the patch. I have reviewed it and don't see any obvious issues though there are some comments that should be changed slightly.

  1. +++ b/core/core.api.php
    @@ -2236,6 +2236,61 @@ function hook_validation_constraint_alter(array &$definitions) {
    + * implementation should return a valid zero-based positive integer or nothing,
    

    I had not heard "zero-based positive integer" used before. I googled it and it does appear to be used but it might be better to change to something like:

    ...implementation should return zero, a positive integer, or...

  2. +++ b/core/core.api.php
    @@ -2236,6 +2236,61 @@ function hook_validation_constraint_alter(array &$definitions) {
    + * if he wants to pass the decision to core or other modules. Returning anything
    

    The "he" should be changed to be gender neutral. In other places "you" is used. Or, the sentence could be restructured something like:

    The implementation should return zero, a positive integer, or, to not impact the results, nothing.

  3. +++ b/core/core.api.php
    @@ -2236,6 +2236,61 @@ function hook_validation_constraint_alter(array &$definitions) {
    + * can provide your own implementation, in your custom module, when you don't
    

    Typo:

    local_get_plural => locale_get_plural

  4. +++ b/core/core.api.php
    @@ -2236,6 +2236,61 @@ function hook_validation_constraint_alter(array &$definitions) {
    + *   example the Local module implementation locale_plural_variant_index() is
    

    Typo:

    Local => Locale

I'll test the patch shortly.

kristen pol’s picture

I've tested this as follows:

  1. Enabled Language and Interface Translation
  2. Added Romanian
  3. Found some text that uses plurals: Module %name has been enabled.
  4. Changed the plurals for these for Romanian
  5. Enabled some modules (1 module, 2 modules, and 3 modules)
  6. The messages were as expected (see screenshots below) (though I'm not sure what the 2. plural is for in Romanian)

kristen pol’s picture

Assigned: Unassigned » kristen pol
Status: Needs review » Needs work

Assigning to myself to make the comment changes. I will try to finish this today.

kristen pol’s picture

Still plan on working on this but the day got away from me. Crossing fingers for tomorrow. If I don't finish by Friday EOD, then I'll take my name off.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

kristen pol’s picture

I reworded some of the comments. Here's the patch and interdiff.

kristen pol’s picture

Assigned: kristen pol » Unassigned
Status: Needs work » Needs review
claudiu.cristea’s picture

@Kristen Pol, thank you for your work and review. Sorry for my poor English. I think the patch looks good. I'm not very sure yet about the solution: if invoking a hook is the way to go. Also I don't find it bad. Probably in 99.99% of the cases nobody will implement such a hook and we'll live with the only implementation which is locale_plural_variant_index(). Normally we should offer also an "alter hook" but for me that sounds a little bit over-engineering.

Nit:

+++ b/core/core.api.php
@@ -2247,6 +2247,63 @@ function hook_validation_constraint_alter(array &$definitions) {
+ * Compute the plural variant index.

Compute > Computes

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should 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.

alberto56’s picture

This might be related to #2273889: Don't use one language's plural index formula with another language's string in the case of untranslated strings using format_plural()., in the sense that computing the plural index requires more information than just the target language -- we also need to know the string itself: if no translation exists for a string, we should not be using the target language plural index, but rather -1, it seems. Perhaps the string itself could be passed to hook_plural_variant_index, so that the locale_plural_variant_index could then check if the string is, in fact, translated; if it is not then the plural variant index would always be -1.

alexpott’s picture

I don't think a new hook is a way to do this. I think PluralTranslatableMarkup should be asking the TranslationManager for the plurals and then when locale is enabled it should be wrapping the TranslationManager service and enhancing the getPluralIndex method. A new hook adds an extension a new extension point that's not needed.

alexpott’s picture

StatusFileSize
new11.58 KB

Here's #21 implemented but not yet working. This could be becasue of how code / services are loaded.

No interdiff because a different approach.

Status: Needs review » Needs work

The last submitted patch, 23: 2660338-23.patch, failed testing. View results

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

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should 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.

claudiu.cristea’s picture

We can also deprecate locale_get_plural() in the scope of this issue as in #23, the procedural function became only a wrapper of the service method call.

Also this approach help us also to remove usages of drupal_static() and drupal_static_reset() from locale_get_plural() and this makes this part of the #1577902: [META] Remove all usages of drupal_static() & drupal_static_reset() effort.

andypost’s picture

Version: 8.6.x-dev » 8.8.x-dev
Status: Needs work » Needs review
StatusFileSize
new11.58 KB

Reroll, looks tests do a lot of mocks for services so instead of strings translatable object returns

andypost’s picture

Debugging test \Drupal\Tests\Core\Datetime\DateTest::testFormatInterval() shows that count passed wrong somehow

8) Drupal\Tests\Core\Datetime\DateTest::testFormatInterval with data set #15 (7344000, 3, '2 months 3 weeks 4 days')
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'2 months 3 weeks 4 days'
+'1 month 1 week 1 day'

Status: Needs review » Needs work

The last submitted patch, 27: 2660338-27.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new1.9 KB
new13.48 KB

Should fix some tests

Status: Needs review » Needs work

The last submitted patch, 30: 2660338-29.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new665 bytes
new13.67 KB

Decorated service should implement all public methods

Status: Needs review » Needs work

The last submitted patch, 32: 2660338-32.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new13.68 KB

reroll

Status: Needs review » Needs work

The last submitted patch, 34: 2660338-32-reroll.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

ridhimaabrol24’s picture

Status: Needs work » Needs review
StatusFileSize
new13.69 KB

Reroll patch for 9.1.x.

ridhimaabrol24’s picture

StatusFileSize
new14.3 KB
new624 bytes

Fixing the error in patch #38.

kristen pol’s picture

Status: Needs review » Needs work

Thanks for the patch. I tried testing it twice (two different installs) but keep getting an error after adding the language:

LogicException: The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database connection. Adjust your code so that is not necessary. Alternatively, look at DependencySerializationTrait as a temporary solution. in Drupal\Core\Database\Connection->__sleep() (line 1725 of /var/lib/tugboat/stm/web/core/lib/Drupal/Core/Database/Connection.php).

When the patch is updated, here are a few nitpicks that could be addressed:

  1. +++ b/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php
    @@ -109,4 +109,22 @@ public function translateString(TranslatableMarkup $translated_string);
    +   *   Optional language code to translate to a language other than
    +   *   what is used to display the page.
    

    Nitpick: Character wrapping (move some words up).

  2. +++ b/core/modules/locale/locale.services.yml
    @@ -37,3 +37,9 @@ services:
    +    # pass the old service as an argument
    

    Nitpick: Comment should be a sentence with starting capital and ending period.

  3. +++ b/core/modules/locale/src/LocaleTranslationManager.php
    @@ -0,0 +1,142 @@
    +      // If there is a plural formula for the language, evaluate it for the given
    

    Nitpick: More than 80 chars.

  4. +++ b/core/modules/locale/src/LocaleTranslationManager.php
    @@ -0,0 +1,142 @@
    +
    +
    +  /**
    

    Nitpick: Extra empty line.

ravi.shankar’s picture

Assigned: Unassigned » ravi.shankar
ravi.shankar’s picture

Assigned: ravi.shankar » Unassigned
adityasingh’s picture

Assigned: Unassigned » adityasingh
adityasingh’s picture

Assigned: adityasingh » Unassigned
Status: Needs work » Needs review
StatusFileSize
new14.17 KB
new2.33 KB

Updated patch as per the changes suggested in #40, please review.

kristen pol’s picture

Issue tags: +Bug Smash Initiative

Thanks for the update. #44 fixes the nitpicks noted in #40.

kristen pol’s picture

Issue tags: +Needs manual testing

Tagging for manual testing. I had issues but maybe someone else can try. If you want test steps, see #11.

tanubansal’s picture

We have followed the steps given in #11 and after adding the patch mentioned #44 (https://www.drupal.org/pift-ci-job/1751390). While enabling singular/plural modules , its showing 'Site encountered error'. When we refresh the modules page then it should that the module is enable. Please refer the attached screenshots below.

tanubansal’s picture

Assigned: tanubansal » Unassigned
kristen pol’s picture

Status: Needs review » Needs work

Ok, since @tanubansal also had an issue, moving this back to needs work.

andypost’s picture

Tests failing because of

Exception: TypeError: Argument 8 passed to Drupal\help_topics\Plugin\HelpSection\HelpTopicSection::__construct() must be an instance of Drupal\Core\StringTranslation\TranslationManager, instance of Drupal\locale\LocaleTranslationManager given, called in /var/www/html/core/modules/help_topics/src/Plugin/HelpSection/HelpTopicSection.php on line 128
Drupal\help_topics\Plugin\HelpSection\HelpTopicSection->__construct()() (Line: 107)
andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new930 bytes
new16.91 KB

Attempt to fix #50

quietone’s picture

Issue summary: View changes

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

quietone’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll, +Needs issue summary update

Patch fails to apply to 9.2.x setting to NW for that.

The Issue summary needs to have a proposed resolution, adding tag.

ravi.shankar’s picture

Issue tags: -Needs reroll
StatusFileSize
new16.9 KB
new4.51 KB

Added reroll of patch #51 on Drupal 9.2.x.

ravi.shankar’s picture

StatusFileSize
new16.93 KB
new1.34 KB

Fixed custom command fails.

mohit_aghera’s picture

Assigned: Unassigned » mohit_aghera
andypost’s picture

New method needs CR also it could help to deprecate locale code a bit

mohit_aghera’s picture

Status: Needs work » Needs review
StatusFileSize
new18.07 KB
new1.75 KB

Fixing test cases.
All the test cases failing in #56 are passing on local now.

InstallerExistingConfigTest test is something that is passing without any change. So not sure if past failure was due to other reasons.

Status: Needs review » Needs work

The last submitted patch, 59: 2660338-59.patch, failed testing. View results

kristen pol’s picture

Pulling out the failure info:

There was 1 failure:
1) Drupal\Tests\locale\Functional\LocalePluralFormatTest::testGetPluralFormat
Plural translation of 1 hours / @count hours for count 0 in fr is 0 heure
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'0 heure'
+'0 heures'

Related code:

./core/modules/locale/tests/src/Functional/LocalePluralFormatTest.php:        $this->assertSame($expected_plural_string, \Drupal::translation()->formatPlural($count, '1 hour', '@count hours', [], ['langcode' => $langcode])->render(), 'Plural translation of 1 hours / @count hours for count ' . $count . ' in ' . $langcode . ' is ' . $expected_plural_string);

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

Assigned: mohit_aghera » Unassigned
ravi.shankar’s picture

StatusFileSize
new18.04 KB
new4.48 KB

Added reroll of patch #59 on Drupal 9.5.x.

andypost’s picture

Status: Needs work » Needs review

Lets see what will fail now

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

For the IS update and change record

Did not test or review yet.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

nicxvan’s picture

Title: locale_get_plural call in PluralTranslatableString is wrong » [pp-3] locale_get_plural call in PluralTranslatableString is wrong
Status: Needs work » Postponed
nicxvan’s picture

Title: [pp-3] locale_get_plural call in PluralTranslatableString is wrong » [pp-3] deprecate locale_get_plural and three locale_system_* functions
Issue summary: View changes

I think we combine a few of the remaining functions in locale.module as part of the .module elimination initiative.

There are still 8 issues remaining even if we combine these few.

  • locale_get_plural
  • locale_system_set_config_langcodes
  • locale_system_update
  • locale_system_remove
nicxvan’s picture

nicxvan’s picture

Issue summary: View changes

Should have done the file audit first, locale_get_plural is complex enough, let's just do that one here.

nicxvan’s picture

Title: [pp-3] deprecate locale_get_plural and three locale_system_* functions » [pp-3] deprecate locale_get_plural

berdir made their first commit to this issue’s fork.

berdir’s picture

Title: [pp-3] deprecate locale_get_plural » Deprecate locale_get_plural
Status: Postponed » Needs work

Removing postponed status, I think this is fairly conflict-free with other issues (maybe except services.yml, but I've moved the change up and that's easy to rebase) and can be worked on. Created an MR with that. At some point changes to TranslateEditForm snuck in, those seem unrelated and I ignored them.

berdir’s picture

I think I want to explore adding this method to a new interface + service in core, that should be much easier to overwrite. I'm not found of decorators generally and this also comes with changes to a pretty fundamental interface.

berdir’s picture

Working on a new service. That requires updates to a bunch of unit tests, but seems to work well and feels much cleaner.

\Drupal\Core\StringTranslation\StringTranslationTrait::getNumberOfPlurals() is somewhat related to this. That also wraps a locale service. I only identified 3 calls to that. one in locale, one in config_translation and one in views. We could consider to just deprecate that, only the views call doesn't depend on locale anyway. Alternatively we could expand the same of the new interface and add it to that as well.

I'm even wondering if we could somehow reuse the existing \Drupal\locale\PluralFormula service implement the new interface instead of a new one that calls out to it. Would nee to alias and deprecate the current service. This is already complex enough I think.

FWIW, the static/memory cache here feels overkill. what exactly is this even caching? a php calculation?

berdir’s picture

Status: Needs work » Needs review

I think this is ready for review.

nicxvan’s picture

Took a quick look and had some minor questions, I'll have a clear look in a bit.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new91 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

andypost’s picture

This is where formula caching been added so not sure it's a good idea to remove #1273968: remove eval from locale.module

berdir’s picture

Status: Needs work » Needs review

That's not true, locale_get_plural() was already using drupal_static() before that, those lines weren't touched in that commit. and it had been using static caching even before drupal_static(), it had been using the static keyword since that function was added in a commit in 2004 by Dries before there were issues and issue numbers.

Raw PHP execution performance changed *a tiny bit* since 2004, it doesn't look like anyone every questioned whether static caching here is worth it or not. Might not even have been back in 2004.

I did a script that called the function 100k times, it barely registered with microtime() and it made no difference whether or not the static caching is there. I verified how often it's called on the frontpage of umami with disabled render caches: zero times, on the article add form, once.

Rebased and removed it.

nicxvan’s picture

Ok I did a bunch of digging, had a couple of discussions in slack and read a bunch of references.
I think we should find a way to document this here in the comments for future people.

Some of this may be obvious to other people on this thread, but I want to type it out to help ensure I understand it properly.

Different languages have different formulas for plurals: https://docs.translatehouse.org/projects/localization-guide/en/latest/l1...

English for example: nplurals=2; plural=(n != 1);
There are two plural forms and the rule for plurals is if there is 1 say the singular, if there are more or less than 1 then use the plural.
E.g 1 horse, 2 horses, 50 horses, 0 horses, -10 horses.

Other languages have other rules: https://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html
Polish: nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);

1 plik
2,3,4 pliki
5-21 pliko'w
22-24 pliki
25-31 pliko'w

There is some documentation on d.o here: https://www.drupal.org/community/contributor-guide/reference-information...

My understanding is that the formula will be at the top of the .po files.
We then precalculate the plural formula in a compressed way shown here: https://www.drupal.org/project/drupal/issues/1273968#comment-7559379

A translation will look like this:

#: modules/user/views_handler_filter_user_name.inc:112
msgid "Unable to find user: @users"
msgid_plural "Unable to find users: @users"
msgstr[0] "Benutzer konnte nicht gefunden werden: @users"
msgstr[1] "Benutzer konnten nicht gefunden werden: @users"

The 0 or 1 is the plural index.

I think we can fill out and link to this reference on PluralFormula.
And maybe on LocalePluralIndex