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:
- Clean drupal 8.6.1
- Modules: Language, Configuration Translation, Content Translation,Interface Translation
- Install second language (example; French).
- Allow content to be translated (and date field).
/admin/config/regional/content-language - Date and time format; add custom medium date with abbreviated month (M). For example: Name=
shortened_medium_dateFormat String=M d, y/admin/config/regional/date-time - 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 - Update language translations (French).
/admin/reports/translations - Using Interface Translation, search French Language for 'May' and set
Mayto peux andMay with context: abbreviated month nameto mai./admin/config/regional/translate - Modify basic page, add a date or timestamp field, and set the field display to the custom medium date format created in step 5.
- Create new basic page content and set date field to May 1, 2018.
- Translate the new basic page to French and set date field to May 1, 2018.
- 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
| Comment | File | Size | Author |
|---|
Issue fork drupal-3004425
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:
- 3004425-date-formats-translation
changes, plain diff MR !11192
Comments
Comment #2
dgilbert commentedComment #3
dgilbert commentedComment #4
dgilbert commentedPossible 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 namesource strings.Comment #6
dgilbert commentedComment #7
dgilbert commentedFixed path in patch.
Comment #8
dgilbert commentedTry again to fix path.
Comment #9
dgilbert commentedComment #10
dgilbert commentedComment #11
krzysztof domańskicore/lib/Drupal/Core/Language/language.api.phpRTBC #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.
Comment #12
krzysztof domańskiComment #13
krzysztof domańskiLet's add 'Abbreviated month name' to testLanguageContext()
Comment #14
krzysztof domańskiUpdated testLanguageContext().
Comment #15
krzysztof domańskiComment #16
krzysztof domańskiComment #17
krzysztof domańskiComment #18
krzysztof domańskiI rerolled added the test only.
Comment #19
krzysztof domańskiTest only passed because it adds context ("Abbreviated month name"). It looks like we need an additional test.
Comment #21
maximpodorov commentedThis patch also helps German sites to look better as German translations for "Jan" are
Jan (context: Abbreviated month name)
Jan. (no context)
Comment #22
abhijeet.kumar2107 commentedComment #23
abhijeet.kumar2107 commentedDrupal version 9.1.x
Rerolled the path #18 and updated to Drupal 9.1.x version
Comment #24
abhijeet.kumar2107 commentedComment #25
abhijeet.kumar2107 commentedDrupal version 9.1.x
Used patch #18 and corrected path #23 which will work on drupal Version 9.1
Comment #26
abhijeet.kumar2107 commentedComment #27
avpadernoComment #30
xsdx commentedConfirming 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.
Comment #31
xsdx commentedComment #32
catchNeeds a re-roll. Could also use a test-only patch here to show that it fails without the change.
Comment #33
kapilv commentedHear a reroll patch.
Comment #35
liquidcms commentedI 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".
Comment #36
avpadernoComment #37
liquidcms commentedAfter 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:
to this:
which makes dates format translation and locale based off the site's content language.
Comment #38
maximpodorov commentedInterface / 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.
Comment #39
suresh prabhu parkala commentedJust a re-roll of patch #33 to latest 9.4.x. please Review
Comment #40
avpadernoComment #41
gauravvvv commentedFixed CS issues #39, Attached interdiff for same. Please review.
Comment #42
avpadernoComment #43
keithdoyle9 commentedEdited: 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.Comment #44
avpadernoThe failing command is
core/scripts/dev/commit-code-check.sh --drupalci, which outputs:Comment #45
avpadernoGiven that those lines contain words that aren't English words, CSpell should be disabled before those lines and re-nabled after.
Comment #46
avpadernoComment #48
avpadernoThe failure is caused by a XML deprecated schema, not the patch.
Comment #51
tsotoodeh commentedCould not reproduce under 9.5.3 and 10.0.3. Should be considered fixed by now.
Comment #52
maximpodorov commentedThat was too optimistic to mark this issue as fixed.
Comment #53
tsotoodeh commentedOut of misunderstanding! glad that the team members are aware and noticed maximpodorov.
Comment #54
smustgrave commentedThis 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.
Comment #56
codebymikey commentedUpdated issue to include support for missing ordinal suffix
jS.Comment #58
codebymikey commentedSupport 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.
Comment #59
smustgrave commentedHiding patches.
I've ran the javascript tests 3 times but seem to fail consistently.
Comment #60
codebymikey commentedNot 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.
Comment #61
smustgrave commentedOdd, 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.
Comment #63
catchCommitted/pushed to 11.x, thanks! This doesn't cherry-pick cleanly to any other branches so leaving fixed against 11.x.