From 6c2a629d47197dfa229739135c13eb6a147f7ca3 Mon Sep 17 00:00:00 2001 From: Ramon Date: Mon, 28 Aug 2023 11:36:23 +1000 Subject: [PATCH] Date: add relative time translations for moment.js (#53931) * Overwriting wp-date script string to include new relateTime translations * Making the translator comments clearer because some languages change the case of indefinite articles * LINTY LINTY SPACEY SPACEY * comma comma comma comma comma chameleon * Move global inside check * Remove useless assignment * It's better to add this to lib/compat/wordpress-6.4/script-loader.php as it reflects the intended destination in Core and also communicates compatibility. --- lib/compat/wordpress-6.4/script-loader.php | 98 ++++++++++++++++++++++ lib/load.php | 1 + 2 files changed, 99 insertions(+) create mode 100644 lib/compat/wordpress-6.4/script-loader.php diff --git a/lib/compat/wordpress-6.4/script-loader.php b/lib/compat/wordpress-6.4/script-loader.php new file mode 100644 index 00000000000000..ac1b01700511f3 --- /dev/null +++ b/lib/compat/wordpress-6.4/script-loader.php @@ -0,0 +1,98 @@ +query( 'wp-date', 'registered' ) ) { + global $wp_locale; + // Calculate the timezone abbr (EDT, PST) if possible. + $timezone_string = get_option( 'timezone_string', 'UTC' ); + $timezone_abbr = ''; + + if ( ! empty( $timezone_string ) ) { + $timezone_date = new DateTime( 'now', new DateTimeZone( $timezone_string ) ); + $timezone_abbr = $timezone_date->format( 'T' ); + } + $scripts->registered['wp-date']->extra['after'] = array( + false, + sprintf( + 'wp.date.setSettings( %s );', + wp_json_encode( + array( + 'l10n' => array( + 'locale' => get_user_locale(), + 'months' => array_values( $wp_locale->month ), + 'monthsShort' => array_values( $wp_locale->month_abbrev ), + 'weekdays' => array_values( $wp_locale->weekday ), + 'weekdaysShort' => array_values( $wp_locale->weekday_abbrev ), + 'meridiem' => (object) $wp_locale->meridiem, + 'relative' => array( + /* translators: %s: Duration. */ + 'future' => __( '%s from now', 'default' ), + /* translators: %s: Duration. */ + 'past' => __( '%s ago', 'default' ), + /* translators: One second from or to a particular datetime, e.g., "a second ago" or "a second from now". */ + 's' => __( 'a second', 'default' ), + /* translators: %s: Duration in seconds from or to a particular datetime, e.g., "4 seconds ago" or "4 seconds from now". */ + 'ss' => __( '%d seconds', 'default' ), + /* translators: One minute from or to a particular datetime, e.g., "a minute ago" or "a minute from now". */ + 'm' => __( 'a minute', 'default' ), + /* translators: %s: Duration in minutes from or to a particular datetime, e.g., "4 minutes ago" or "4 minutes from now". */ + 'mm' => __( '%d minutes', 'default' ), + /* translators: %s: One hour from or to a particular datetime, e.g., "an hour ago" or "an hour from now". */ + 'h' => __( 'an hour', 'default' ), + /* translators: %s: Duration in hours from or to a particular datetime, e.g., "4 hours ago" or "4 hours from now". */ + 'hh' => __( '%d hours', 'default' ), + /* translators: %s: One day from or to a particular datetime, e.g., "a day ago" or "a day from now". */ + 'd' => __( 'a day', 'default' ), + /* translators: %s: Duration in days from or to a particular datetime, e.g., "4 days ago" or "4 days from now". */ + 'dd' => __( '%d days', 'default' ), + /* translators: %s: One month from or to a particular datetime, e.g., "a month ago" or "a month from now". */ + 'M' => __( 'a month', 'default' ), + /* translators: %s: Duration in months from or to a particular datetime, e.g., "4 months ago" or "4 months from now". */ + 'MM' => __( '%d months', 'default' ), + /* translators: %s: One year from or to a particular datetime, e.g., "a year ago" or "a year from now". */ + 'y' => __( 'a year', 'default' ), + /* translators: %s: Duration in years from or to a particular datetime, e.g., "4 years ago" or "4 years from now". */ + 'yy' => __( '%d years', 'default' ), + ), + 'startOfWeek' => (int) get_option( 'start_of_week', 0 ), + ), + 'formats' => array( + /* translators: Time format, see https://www.php.net/manual/datetime.format.php */ + 'time' => get_option( 'time_format', __( 'g:i a', 'gutenberg' ) ), + /* translators: Date format, see https://www.php.net/manual/datetime.format.php */ + 'date' => get_option( 'date_format', __( 'F j, Y', 'gutenberg' ) ), + /* translators: Date/Time format, see https://www.php.net/manual/datetime.format.php */ + 'datetime' => __( 'F j, Y g:i a', 'default' ), + /* translators: Abbreviated date/time format, see https://www.php.net/manual/datetime.format.php */ + 'datetimeAbbreviated' => __( 'M j, Y g:i a', 'default' ), + ), + 'timezone' => array( + 'offset' => (float) get_option( 'gmt_offset', 0 ), + 'string' => $timezone_string, + 'abbr' => $timezone_abbr, + ), + ) + ) + ), + ); + } +} + +add_action( 'wp_default_scripts', 'gutenberg_update_wp_date_settings' ); + diff --git a/lib/load.php b/lib/load.php index 1db219dd681976..6560ef566d444b 100644 --- a/lib/load.php +++ b/lib/load.php @@ -122,6 +122,7 @@ function gutenberg_is_experiment_enabled( $name ) { // WordPress 6.4 compat. require __DIR__ . '/compat/wordpress-6.4/blocks.php'; require __DIR__ . '/compat/wordpress-6.4/block-patterns.php'; +require __DIR__ . '/compat/wordpress-6.4/script-loader.php'; // Experimental features. require __DIR__ . '/experimental/block-editor-settings-mobile.php';