Problem/Motivation

If a part of datelist is not filled, you see a translatable error with a description which parts are not filled.
However, the names of the parts are not translatable because they are pasted as simple string literal parameters:

// The $value might be either "year", "month" or "day".
$form_state->setError($element[$value], t('A value must be selected for %part.', ['%part' => $value]));

Proposed resolution

Wrap the $value variable with t() function.

Remaining tasks

None.

User interface changes

Fully translatable error messages for invalid datetime fields.

API changes

None.

Data model changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

y.litovchenko created an issue. See original summary.

y.litovchenko’s picture

y.litovchenko’s picture

Assigned: y.litovchenko » Unassigned
Status: Active » Needs review
mpdonadio’s picture

Issue tags: -translation +D8MI
+++ b/core/lib/Drupal/Core/Datetime/Element/Datelist.php
@@ -318,7 +318,7 @@ public static function validateDatelist(&$element, FormStateInterface $form_stat
-          $form_state->setError($element[$value], t('A value must be selected for %part.', ['%part' => $value]));
+          $form_state->setError($element[$value], t('A value must be selected for %part.', ['%part' => t($value)]));
         }

Confused here. Shouldn those labels already be translated as part of Datelist::processDatelist()?

What language are you using? Can you post a screenshot of the widget + error message?

jml16’s picture

FileSize
27.07 KB

Hi, here you have the screenshot in spanish.

Untranslated language

mpdonadio’s picture

Issue tags: +Needs tests

Thanks for the screenshot. I think we need to add a test here.

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.

jhedstrom’s picture

Status: Needs review » Needs work
+++ b/core/lib/Drupal/Core/Datetime/Element/Datelist.php
@@ -318,7 +318,7 @@ public static function validateDatelist(&$element, FormStateInterface $form_stat
+          $form_state->setError($element[$value], t('A value must be selected for %part.', ['%part' => t($value)]));

Using variables inside the t() function is usually not recommended.

I wonder instead if we need to translate the #date_part_order key in getInfo():

  public function getInfo() {
    $class = get_class($this);
    return [
      '#input' => TRUE,
      '#element_validate' => [
        [$class, 'validateDatelist'],
      ],
      '#process' => [
        [$class, 'processDatelist'],
      ],
      '#theme' => 'datetime_form',
      '#theme_wrappers' => ['datetime_wrapper'],
      '#date_part_order' => ['year', 'month', 'day', 'hour', 'minute'],
      '#date_year_range' => '1900:2050',
      '#date_increment' => 1,
      '#date_date_callbacks' => [],
      '#date_timezone' => '',
    ];
  }

Note that it isn't as simple as just wrapping those values in t() since those are actual markup elements.

The root of this issue is that we're using machine names in the UI in this warning instead of a translatable label.

This also still needs tests, so setting to NW.

jhedstrom’s picture

Status: Needs work » Needs review
FileSize
1.49 KB
1.49 KB

I'm not actually sure how to right language/translation tests...

This patch implements what I described in #8.

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

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.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.

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

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

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

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

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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

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

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
FileSize
1.79 KB

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

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

Version: 9.5.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.