Follow-up to #2557113: Make t() return a TranslationWrapper object to remove reliance on a static, unpredictable safe list

Problem/Motivation

We have TranslationWrapper (soon to be TranslatedString but no equivalent for plurals.

Proposed resolution

Build on #2557113: Make t() return a TranslationWrapper object to remove reliance on a static, unpredictable safe list to make it consistent for plurals.

Remaining tasks

Agree

User interface changes

None

API changes

  • TranslationInterface::formatPluralTranslated replaced with PluralTranslatableString::createFromTranslatedString
  • TranslationInterface::formatPlural returns a PluralTranslatableString object instead of string
  • Deprecate constant LOCALE_PLURAL_DELIMITER in favour of \Drupal\Core\StringTranslation\PluralTranslatableString::DELIMITER

Data model changes

None

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Task because the current TranslationWrapper/SafeMarkup::format() approach works.
Issue priority Major because this is a followup to #2557113: Make t() return a TranslationWrapper object to remove reliance on a static, unpredictable safe list that brings consistency
Prioritized changes The main goal of this issue is follow-up to the critical [#2557113
Disruption Minimally disruptive given that TranslationWrapper use is now widespread. The one real API change is the removal of TranslationInterface::formatPluralTranslated which was always a very dodgy method. This patch massively improves this by making it a static object creation function on PluralTranslatableString.

Comments

alexpott created an issue. See original summary.

alexpott’s picture

alexpott’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: 2570107-2.patch, failed testing.

The last submitted patch, 2: 2570107-2.patch, failed testing.

alexpott’s picture

Status: Needs work » Needs review
StatusFileSize
new61.48 KB

Making a patch that applies... includes #2557113: Make t() return a TranslationWrapper object to remove reliance on a static, unpredictable safe list. The do-not-test.patch is unchanged.

dawehner’s picture

  1. +++ b/core/lib/Drupal/Core/StringTranslation/PluralTranslatedString.php
    @@ -0,0 +1,156 @@
    +   */
    +  public function render() {
    +    if (!$this->translatedString) {
    

    Are we sure that this long logic should be part of the value object?

  2. +++ b/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php
    @@ -91,50 +91,16 @@ public function renderTranslatedString(TranslationWrapper $translated_string);
    -   */
    -  public function formatPluralTranslated($count, $translation, array $args = array(), array $options = array());
    -
    

    OH I did not knew that this exists.

Status: Needs review » Needs work

The last submitted patch, 6: 2570107-2-6.patch, failed testing.

The last submitted patch, 6: 2570107-2-6.patch, failed testing.

alexpott’s picture

Assigned: Unassigned » alexpott
alexpott’s picture

Assigned: alexpott » Unassigned
Status: Needs work » Needs review
StatusFileSize
new20.3 KB

Rerolled and fixed the test fails.

Status: Needs review » Needs work

The last submitted patch, 11: 2570107-3-11.patch, failed testing.

alexpott’s picture

Status: Needs work » Needs review
StatusFileSize
new1.25 KB
new21.54 KB

Fixing tests.

The last submitted patch, 11: 2570107-3-11.patch, failed testing.

alexpott’s picture

Title: Make format_plural() return a PluralTranslatedString object to remove reliance on a static, unpredictable safe list » Make format_plural() return a PluralTranslatableString object to remove reliance on a static, unpredictable safe list
Issue summary: View changes
StatusFileSize
new8.7 KB
new21.92 KB
alexpott’s picture

StatusFileSize
new2.55 KB
new22.9 KB

Adding the hack to return unsafe strings so we can do this before finishing !placeholder removal.

lauriii’s picture

Status: Needs review » Needs work

Found some nitpicks from the patch:

  1. +++ b/core/lib/Drupal/Core/StringTranslation/PluralTranslatableString.php
    @@ -0,0 +1,172 @@
    +   *   "@count new comments".
    

    "@count could be on the previous line

  2. +++ b/core/lib/Drupal/Core/StringTranslation/PluralTranslatableString.php
    @@ -0,0 +1,172 @@
    +   *   (optional) The string translation service.
    

    Posted also on the other issue but I this doesn't have to be necessary a service, but what should it be called then?

  3. +++ b/core/lib/Drupal/Core/StringTranslation/PluralTranslatableString.php
    @@ -0,0 +1,172 @@
    +  public function __construct($count, $singular, $plural, array $args = array(), array $options = array(), TranslationInterface $string_translation = NULL) {
    

    Usually not worth mentioning but maybe we could consider using [] for the arrays because it would make this line a lot shorter & more readable

  4. +++ b/core/lib/Drupal/Core/StringTranslation/PluralTranslatableString.php
    @@ -0,0 +1,172 @@
    +   *   themed. See \Drupal\Component\Utility\SafeMarkup::format(). Note that you do
    

    80 chars

  5. +++ b/core/lib/Drupal/Core/StringTranslation/PluralTranslatableString.php
    @@ -0,0 +1,172 @@
    +        // the second plural form as a fallback (which allows for most flexibility
    

    80 chars

  6. +++ b/core/lib/Drupal/Core/StringTranslation/PluralTranslatableString.php
    @@ -0,0 +1,172 @@
    \ No newline at end of file
    

    Newline

  7. +++ b/core/modules/locale/src/Tests/LocalePluralFormatTest.php
    @@ -130,12 +131,15 @@ public function testGetPluralFormat() {
    +        // PluralTranslatableString::createFromTranslatedString() this way. It is
    

    80 chars

  8. +++ b/core/tests/Drupal/Tests/Core/Form/FormErrorHandlerTest.php
    @@ -130,3 +130,11 @@ public function testSetElementErrorsFromFormState() {
    \ No newline at end of file
    

    Newline

  9. +++ b/core/tests/Drupal/Tests/Core/StringTranslation/TranslationManagerTest.php
    @@ -40,7 +40,8 @@ public function providerTestFormatPlural() {
    +      // @todo remove once !placeholder support is removed.
    

    Issue number would be nice

alexpott’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new10.26 KB
new22.22 KB
  1. That's a c&p and I think it is to keep the quoted string together.
  2. I think this is fine
  3. Fixed
  4. Fixed
  5. Fixed
  6. Fixed
  7. Fixed
  8. Fixed
  9. Reverted this whole change as it is no longer necessary no that we support returning unsafe strings.

Discussed with @gabor and we decided to deprecate LOCALE_PLURAL_DELIMITER for Drupal 9 for cleaner unit testing and future proofing.

alexpott’s picture

lauriii’s picture

Status: Needs review » Needs work

CR looks good. Few things that might be a good idea to fix:

  1. +++ b/core/lib/Drupal/Core/StringTranslation/PluralTranslatableString.php
    @@ -0,0 +1,182 @@
    +class PluralTranslatableString extends TranslationWrapper {
    ...
    +   * Constructs a new class instance.
    

    Nit: Wouldn't "new PluralTranslatableString object" be more accurate

  2. +++ b/core/lib/Drupal/Core/StringTranslation/PluralTranslatableString.php
    @@ -0,0 +1,182 @@
    +        // If the index cannot be computed or there's no translation, use
    +        // the second plural form as a fallback (which allows for most
    

    Nit: the still fits the previous line.

  3. +++ b/core/modules/locale/src/Tests/LocalePluralFormatTest.php
    @@ -130,12 +131,15 @@ public function testGetPluralFormat() {
    +        $translated_string = \Drupal::translation()->translate('1 hour' . LOCALE_PLURAL_DELIMITER . '@count hours', array(), array('langcode' => $langcode));
    

    And the reason why I still NW this, the const could be changed to the non-deprecated one :)

alexpott’s picture

Status: Needs work » Needs review
StatusFileSize
new22.25 KB
new2.34 KB
  1. Personally I'd prefer a standard "Constructs a new class instance." but whatevs
  2. Fixed
  3. Fixed - although since we're only deprecating I don't think replacing everything in HEAD should be in the scope of this patch.
lauriii’s picture

Status: Needs review » Reviewed & tested by the community

Yeah I just don't like that we're adding new usages in the patch that is deprecating it :)

alexpott’s picture

alexpott’s picture

StatusFileSize
new846 bytes
new21.71 KB

@catch noticed a missing @return

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.0.x, thanks!

  • catch committed 1d50852 on 8.0.x
    Issue #2570107 by alexpott: Make format_plural() return a...

The last submitted patch, 23: 2570107-3-23.patch, failed testing.

Status: Fixed » Needs work

The last submitted patch, 24: 2570107-3-24.patch, failed testing.

lauriii’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

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

gábor hojtsy’s picture

Issue tags: -sprint

Yay for security and consistency. Thanks!

duaelfr’s picture