Problem/Motivation

When creating or modifying a translated date format which uses the abbreviated month name (M), the translations (Jan, Feb, Mar, etc.) will not use the 'context' related interface translation.

For example, the French interface translation for 'May' should provide two source strings; May and May with context: abbreviated month name. Assume we translate May to peux and May with context to mai. All content date fields which uses the date format with the abbreviated month (M) will display the month of May as peux when it should be mai.

Steps to reproduce:

  1. Clean drupal 8.6.1
  2. Modules: Language, Configuration Translation, Content Translation,Interface Translation
  3. Install second language (example; French).
  4. Allow content to be translated (and date field). /admin/config/regional/content-language
  5. Date and time format; add custom medium date with abbreviated month (M). For example: Name=shortened_medium_date Format String= M d, y /admin/config/regional/date-time
  6. Add a French translation for the custom medium date. For example: Format String= d M, y /admin/config/regional/date-time/formats/manage/shortened_medium_date/translate
  7. Update language translations (French). /admin/reports/translations
  8. Using Interface Translation, search French Language for 'May' and set May to peux and May with context: abbreviated month name to mai. /admin/config/regional/translate
  9. Modify basic page, add a date or timestamp field, and set the field display to the custom medium date format created in step 5.
  10. Create new basic page content and set date field to May 1, 2018.
  11. Translate the new basic page to French and set date field to May 1, 2018.
  12. View the new page with /fr/. The date should be 1 mai, 2018 but it will be 1 peux, 2018.

It also doesn't translate the ordinal suffix in a date format like jS.

e.g.

\Drupal::service('date.formatter')->format(1739375586, 'custom', 'jS', NULL, 'fr');
// 23rd
// But this should be something along the lines of 23er

Proposed resolution

Modify the DrupalDateTime format function to set context = Abbreviated month name when code = M.

As well as add support for the S character code.

Remaining tasks

  • Create patch. (Done)
  • Test.

User interface changes

none

API changes

none

Data model changes

none

Original report by [username]

none

Issue fork drupal-3004425

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

dgilbert created an issue. See original summary.

dgilbert’s picture

dgilbert’s picture

StatusFileSize
new498 bytes
dgilbert’s picture

Status: Active » Needs review

Possible impact of this patch on existing sites: Any content date field that uses a date format with abbreviated month (M) may not display translations if interface translations do not exist for the context: abbreviated month name source strings.

Status: Needs review » Needs work

The last submitted patch, 3: 3004425-3.patch, failed testing. View results

dgilbert’s picture

Version: 8.6.x-dev » 8.6.1
dgilbert’s picture

StatusFileSize
new558 bytes

Fixed path in patch.

dgilbert’s picture

StatusFileSize
new624 bytes

Try again to fix path.

dgilbert’s picture

Status: Needs work » Needs review
dgilbert’s picture

Issue summary: View changes
krzysztof domański’s picture

Version: 8.6.1 » 8.6.x-dev
Assigned: dgilbert » Unassigned
Status: Needs review » Reviewed & tested by the community

core/lib/Drupal/Core/Language/language.api.php

Because the source of translation strings is English, and some words in English have multiple meanings or uses, this centralized, shared translation string storage can sometimes lead to ambiguous translations that are not correct for every place the string is used. As an example, the English word "May", in a string by itself, could be part of a list of full month names or part of a list of 3-letter abbreviated month names. So, in languages where the month name for May is longer than 3 letters, you'd need to translate May differently depending on how it's being used. To address this problem, the translation system includes the concept of the "context" of a translated string, which can be used to disambiguate text for translators, and obtain the correct translation for each usage of the string.

RTBC #8: 'M' is a short textual representation of a month, three letters and the 'Abbreviated month name' is a correct context of month name abbreviations.

krzysztof domański’s picture

Component: transliteration system » language system
krzysztof domański’s picture

Assigned: Unassigned » krzysztof domański
Status: Reviewed & tested by the community » Needs work

Let's add 'Abbreviated month name' to testLanguageContext()

/**
* Test msgctxt context support.
*/
public function testLanguageContext() {
  // Try importing a .po file.
  $this->importPoFile($this->getPoFileWithContext(), [
    'langcode' => 'hr',
  ]);

  // We cast the return value of t() to string so as to retrieve the
  // translated value, rendered as a string.
  $this->assertIdentical((string) t('May', [], ['langcode' => 'hr', 'context' => 'Long month name']), 'Svibanj', 'Long month name context is working.');
  $this->assertIdentical((string) t('May', [], ['langcode' => 'hr']), 'Svi.', 'Default context is working.');
}
krzysztof domański’s picture

Assigned: krzysztof domański » Unassigned
StatusFileSize
new1.14 KB
new1.76 KB

Updated testLanguageContext().

krzysztof domański’s picture

Status: Needs work » Needs review
krzysztof domański’s picture

krzysztof domański’s picture

Version: 8.6.x-dev » 8.9.x-dev
Category: Bug report » Feature request
krzysztof domański’s picture

Component: language system » locale.module
StatusFileSize
new2.86 KB
new1.75 KB

I rerolled added the test only.

krzysztof domański’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Test only passed because it adds context ("Abbreviated month name"). It looks like we need an additional test.

+msgctxt "Abbreviated month name"
+msgid "May"
+msgstr "svib."<code>

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.

maximpodorov’s picture

This patch also helps German sites to look better as German translations for "Jan" are
Jan (context: Abbreviated month name)
Jan. (no context)

abhijeet.kumar2107’s picture

Assigned: Unassigned » abhijeet.kumar2107
abhijeet.kumar2107’s picture

StatusFileSize
new2.12 KB

Drupal version 9.1.x
Rerolled the path #18 and updated to Drupal 9.1.x version

abhijeet.kumar2107’s picture

Assigned: abhijeet.kumar2107 » Unassigned
Status: Needs work » Needs review
abhijeet.kumar2107’s picture

Assigned: Unassigned » abhijeet.kumar2107
StatusFileSize
new1.75 KB

Drupal version 9.1.x
Used patch #18 and corrected path #23 which will work on drupal Version 9.1

abhijeet.kumar2107’s picture

Assigned: abhijeet.kumar2107 » Unassigned
avpaderno’s picture

Title: Date formats with abbreviated month name (M) are not translated using context. » Date formats with abbreviated month name (M) are not translated using context

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.

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.

xsdx’s picture

Confirming issue exists and can be tested with German dates also. For example März is displaying incorrectly instead of Mär. And this patch is solving the issue.

xsdx’s picture

Status: Needs review » Reviewed & tested by the community
catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: -Needs tests +Needs reroll

Needs a re-roll. Could also use a test-only patch here to show that it fails without the change.

kapilv’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new1.58 KB
new2.02 KB

Hear a reroll patch.

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.

liquidcms’s picture

I am arriving late to this but i think my issue is the same issue and i also think this discussion has missed the mark completely. It sounds like the focus is specifically on one part of PHP's date function ('M'); when i suspect this is not at all what this is about. I also think people are referring to interface translation as string translation and translating the text "May" (and i guess all the other months). Pretty sure all of this is incorrect.

PHP and Drupal already correctly translates all the date parts (month, day of the week). The issue is that Drupal (incorrectly) uses the site's
"interface language" to determine the language rather than the "content language".

avpaderno’s picture

Status: Needs review » Needs work
liquidcms’s picture

After digging into this a bit more:

- translation of dates is based on 2 things: locale and translation of format. Locale is set in PHP and determines language to use for textual parts of a date: day of week, months. Format is a Drupal thing and allows to translate a dates "format" which is typically to re-arrange parts of the date definition: Y-m-d (for English) and m-d-Y (for some other language).

both of these, as i mentioned above and imho are done wrong in the core DateFormatter class as they use the site's "interface language". Unclear to me why dates would be considered interface when all other content is considered content. My (simple) solution was to override core's DateFormat class and change this 1 line:

    if (empty($langcode)) {
      $langcode = $this->languageManager->getCurrentLanguage()->getId();
    }

to this:

    if (empty($langcode)) {
      $langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
    }

which makes dates format translation and locale based off the site's content language.

maximpodorov’s picture

Interface / content dates is a different task which is not related to the current one.
Moreover, some dates on a page are interface while some other dates on the same page are content. E.g. node creation date output for site visitors is content, but node creation date output as a service field in an editor dashboard is interface.

suresh prabhu parkala’s picture

StatusFileSize
new1.59 KB

Just a re-roll of patch #33 to latest 9.4.x. please Review

avpaderno’s picture

Status: Needs work » Needs review
gauravvvv’s picture

StatusFileSize
new1.58 KB
new1.1 KB

Fixed CS issues #39, Attached interdiff for same. Please review.

avpaderno’s picture

Status: Needs review » Needs work
keithdoyle9’s picture

Edited: Needed to fix a module issue that was blocking the upgrade. #41 worked.
None of these patches are working anymore for 9.3.x. The patch won't apply which means I'm unable to upgrade past 9.2 for now.

avpaderno’s picture

The failing command is core/scripts/dev/commit-code-check.sh --drupalci, which outputs:

/var/www/html/core/modules/locale/tests/src/Functional/LocaleImportFunctionalTest.php:265:24 - Unknown word (Svib)

CSpell: failed

avpaderno’s picture

     $this->assertSame('Svibanj', (string) t('May', [], ['langcode' => 'hr', 'context' => 'Long month name']), 'Long month name context is working.');
+    $this->assertSame('Svib.', (string) t('May', [], ['langcode' => 'hr', 'context' => 'Abbreviated month name']), 'Abbreviated month name context is working.');
     $this->assertSame('Svi.', (string) t('May', [], ['langcode' => 'hr']), 'Default context is working.');

Given that those lines contain words that aren't English words, CSpell should be disabled before those lines and re-nabled after.

avpaderno’s picture

Status: Needs work » Needs review
StatusFileSize
new1.64 KB

Status: Needs review » Needs work
avpaderno’s picture

Status: Needs work » Needs review

The failure is caused by a XML deprecated schema, not the patch.

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.

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.

tsotoodeh’s picture

Status: Needs review » Fixed

Could not reproduce under 9.5.3 and 10.0.3. Should be considered fixed by now.

maximpodorov’s picture

Status: Fixed » Needs review

That was too optimistic to mark this issue as fixed.

tsotoodeh’s picture

That was too optimistic to mark this issue as fixed.

Out of misunderstanding! glad that the team members are aware and noticed maximpodorov.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

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.

Appears last patch had a failure.

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.

codebymikey’s picture

Title: Date formats with abbreviated month name (M) are not translated using context » Date formats with abbreviated month name (M) and ordinal suffix are not translated using context
Issue summary: View changes

Updated issue to include support for missing ordinal suffix jS.

codebymikey’s picture

Status: Needs work » Needs review

Support for ordinal suffixes is a bit more complicated (e.g. https://www.omniglot.com/language/dates/welsh.htm), but providing some translation is better than using English ordinal text within a different language.

And for the more complicated ones, an ordinal dot could could be used.

Available reference: https://3v4l.org/jXP5l#v8.4.4

Test failures are due to a separate issue with functional JS.

smustgrave’s picture

Status: Needs review » Needs work

Hiding patches.

I've ran the javascript tests 3 times but seem to fail consistently.

codebymikey’s picture

Status: Needs work » Needs review

Not sure what happened, but the changes didn't touch or should break any of the functional JS tests.

It's now passing following a rerun without any specific changes, so it's something caused by upstream or Gitlab CI as I've experienced something similar in the past.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Odd, definitely ran multiple times guess 4th time was the charm!

Test-only feature has already been ran and shows coverage for the change.

Don't see any open threads or remaining tasks, believe this is good.

  • catch committed f964df2b on 11.x
    Issue #3004425 by dgilbert, krzysztof domański, codebymikey, avpaderno,...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 11.x, thanks! This doesn't cherry-pick cleanly to any other branches so leaving fixed against 11.x.

Status: Fixed » Closed (fixed)

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