Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date: add relative time translations for moment.js #53931

Merged
merged 7 commits into from
Aug 28, 2023

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented Aug 25, 2023

Resolves #53888

What?

Overwrites wp-date script string to include new relativeTime translations.

The inline script is generated in Core here: https://github.com/WordPress/wordpress-develop/blob/bf00a673a5f1d7f4707a6f914e1408526a8dc06a/src/wp-includes/script-loader.php#L430 and will need to be synced.

See: https://momentjscom.readthedocs.io/en/latest/moment/07-customization/07-relative-time/

Why?

The settings strings were never synced to Core.

Now that we're using relativeTime in humanTimeDiff, we need the translations.

Unfortunately, %d minutes and others have not yet been translated.

How?

Hooking into wp_default_scripts and overwriting the settings of the registered wp-date script.

The other option is to re-add the inline script, which will also work, but it will output 2 x inline scripts, e.g.,

<script id="wp-date-js-after">
wp.date.setSettings( ...oldSettings );
wp.date.setSettings( ...newSettings );
</script>

Testing Instructions

Change your editor/site to another language via Settings > General
Since the relevant translations have not been added, you can check that the wp.date settings have been updated by heading to /wp-admin/site-editor.php and, in the browser console, running wp.date.getSettings()

The relative object should match the requirements of https://momentjscom.readthedocs.io/en/latest/moment/07-customization/07-relative-time/

{
    "l10n": {
        "locale": "de_DE",
        "months": [
            "Januar",
            "Februar",
            "März",
            "April",
            "Mai",
            "Juni",
            "Juli",
            "August",
            "September",
            "Oktober",
            "November",
            "Dezember"
        ],
        "monthsShort": [
            "Jan",
            "Feb",
            "Mrz",
            "Apr",
            "Mai",
            "Jun",
            "Jul",
            "Aug",
            "Sep",
            "Okt",
            "Nov",
            "Dez"
        ],
        "weekdays": [
            "Sonntag",
            "Montag",
            "Dienstag",
            "Mittwoch",
            "Donnerstag",
            "Freitag",
            "Samstag"
        ],
        "weekdaysShort": [
            "So",
            "Mo",
            "Di",
            "Mi",
            "Do",
            "Fr",
            "Sa"
        ],
        "meridiem": {
            "am": "am",
            "pm": "pm",
            "AM": "AM",
            "PM": "PM"
        },
        "relative": {
            "future": "%s ab jetzt",
            "past": "vor %s",
            "s": "a second",
            "ss": "%d seconds",
            "m": "a minute",
            "mm": "%d minutes",
            "h": "an hour",
            "hh": "%d hours",
            "d": "a day",
            "dd": "%d days",
            "M": "a month",
            "MM": "%d months",
            "y": "a year",
            "yy": "%d years"
        },
        "startOfWeek": 1
    },
    "formats": {
        "time": "g:i a",
        "date": "F j, Y",
        "datetime": "j. F Y G:i",
        "datetimeAbbreviated": "j. M. Y G:i"
    },
    "timezone": {
        "offset": 0,
        "string": "",
        "abbr": ""
    }
}

@ramonjd ramonjd added [Type] Bug An existing feature does not function as intended Internationalization (i18n) Issues or PRs related to internationalization efforts [Package] Date /packages/date Needs PHP backport Needs PHP backport to Core labels Aug 25, 2023
@ramonjd ramonjd self-assigned this Aug 25, 2023
@github-actions
Copy link

github-actions bot commented Aug 25, 2023

This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress.

If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged.

If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack.

Thank you! ❤️

View changed files
❔ lib/compat/wordpress-6.4/script-loader.php
❔ lib/load.php

Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for the fix @ramonjd!

✅ Confirmed the missing strings prior to applying this PR
✅ With this PR applied, the settings are identical as without it, except for the additional relative strings (when running JSON.stringify( wp.date.getSettings() ); to compare strings)
✅ Visually, the code here otherwise matches what's in core

LGTM! ✨

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

My concern is that this code might be better written in lib/compat/wordpress-6.4/script-loader.php.

I think this hook expects all relative times to translate correctly on all WordPress versions supported by the Gutenberg plugin. And if the minimum WordPress version that the Gutenberg plugin supports has this code, this hook will not be needed.

I think the lib/compat/wordpres-{X.Y} directory will be removed as the Gutenberg plugin supports more WordPress versions, so you can avoid forgetting to remove this hook.

What do you think?

lib/client-assets.php Outdated Show resolved Hide resolved
@ramonjd
Copy link
Member Author

ramonjd commented Aug 27, 2023

I think this hook expects all relative times to translate correctly on all WordPress versions supported by the Gutenberg plugin. And if the minimum WordPress version that the Gutenberg plugin supports has this code, this hook will not be needed.

Yes, it totally makes more sense to store this under a compat folder. I'll move it there 👍🏻

@ramonjd ramonjd force-pushed the update/wp-date-moment-settings branch from 2a73135 to d21cbdd Compare August 28, 2023 01:02
@ramonjd ramonjd enabled auto-merge (squash) August 28, 2023 01:31
@ramonjd ramonjd merged commit 6c2a629 into trunk Aug 28, 2023
49 checks passed
@ramonjd ramonjd deleted the update/wp-date-moment-settings branch August 28, 2023 01:36
@github-actions github-actions bot added this to the Gutenberg 16.6 milestone Aug 28, 2023
@ramonjd ramonjd removed the Needs PHP backport Needs PHP backport to Core label Aug 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Internationalization (i18n) Issues or PRs related to internationalization efforts [Package] Date /packages/date [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Site editor: last modified time is not translatable
4 participants