From 8377d12ff2dbb3b7aab5852746a519beb2fde96b Mon Sep 17 00:00:00 2001 From: Faisal Ahammad Date: Wed, 25 Nov 2026 21:30:26 +0600 Subject: [PATCH] fix(i18n): Update date and time formats when the site language changes - Add wp_update_date_time_formats_on_locale_change() to wp-admin/includes/options.php. - Call it from wp-admin/options.php after the General settings are saved. - Add date and time format strings to the de_DE and es_ES test translations. - Add unit tests. Why: the date and time formats stayed on the old language defaults after switching the site language. A format is only updated when it still matches the previous locale default, so custom formats are kept. See #36259. --- src/wp-admin/includes/options.php | 42 ++++++++ src/wp-admin/options.php | 6 ++ tests/phpunit/data/languages/de_DE.l10n.php | 2 +- tests/phpunit/data/languages/de_DE.mo | Bin 965 -> 1046 bytes tests/phpunit/data/languages/de_DE.po | 10 ++ tests/phpunit/data/languages/es_ES.l10n.php | 2 +- tests/phpunit/data/languages/es_ES.mo | Bin 1039 -> 1120 bytes tests/phpunit/data/languages/es_ES.po | 10 ++ .../wpUpdateDateTimeFormatsOnLocaleChange.php | 102 ++++++++++++++++++ 9 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/tests/admin/includes/options/wpUpdateDateTimeFormatsOnLocaleChange.php diff --git a/src/wp-admin/includes/options.php b/src/wp-admin/includes/options.php index cd9bff786f1a8..1980ce971a7ae 100644 --- a/src/wp-admin/includes/options.php +++ b/src/wp-admin/includes/options.php @@ -137,3 +137,45 @@ function options_reading_blog_charset() { echo ''; echo '

' . __( 'The character encoding of your site (UTF-8 is recommended)' ) . '

'; } + +/** + * Updates the date and time formats after the site language changes. + * + * A format is only updated when it still matches the default format of the + * previous locale, so formats chosen by the user are kept. + * + * @since 7.3.0 + * + * @param string $old_locale The site locale before the change. + * @param string $new_locale The site locale after the change. + */ +function wp_update_date_time_formats_on_locale_change( $old_locale, $new_locale ) { + if ( $old_locale === $new_locale ) { + return; + } + + /* + * Load the translations directly, as switch_to_locale() does not know + * about language packs installed during the current request. + */ + load_default_textdomain( $old_locale ); + $old_formats = array( + 'date_format' => __( 'F j, Y' ), + 'time_format' => __( 'g:i a' ), + ); + + load_default_textdomain( $new_locale ); + $new_formats = array( + 'date_format' => __( 'F j, Y' ), + 'time_format' => __( 'g:i a' ), + ); + + // Restore the translations for the current user. + load_default_textdomain(); + + foreach ( $old_formats as $option => $old_format ) { + if ( get_option( $option ) === $old_format ) { + update_option( $option, $new_formats[ $option ] ); + } + } +} diff --git a/src/wp-admin/options.php b/src/wp-admin/options.php index 8f065f64473e6..a95c34d942e6f 100644 --- a/src/wp-admin/options.php +++ b/src/wp-admin/options.php @@ -324,6 +324,7 @@ if ( $options ) { $user_language_old = get_user_locale(); + $site_language_old = get_locale(); foreach ( $options as $option ) { if ( $unregistered ) { @@ -357,6 +358,11 @@ * used as a fallback in get_user_locale(). */ unset( $GLOBALS['locale'] ); + + if ( 'general' === $option_page ) { + wp_update_date_time_formats_on_locale_change( $site_language_old, get_locale() ); + } + $user_language_new = get_user_locale(); if ( $user_language_old !== $user_language_new ) { load_default_textdomain( $user_language_new ); diff --git a/tests/phpunit/data/languages/de_DE.l10n.php b/tests/phpunit/data/languages/de_DE.l10n.php index 9190d3641ca68..03f3f606ee1c5 100644 --- a/tests/phpunit/data/languages/de_DE.l10n.php +++ b/tests/phpunit/data/languages/de_DE.l10n.php @@ -1,2 +1,2 @@ NULL,'plural-forms'=>'nplurals=2; plural=n != 1;','messages'=>['html_lang_attribute'=>'de-DE','text directionltr'=>'ltr','number_format_decimal_point'=>',','number_format_thousands_sep'=>'.','Update %s now'=>'Jetzt %s aktualisieren','[%1$s] Confirm Action: %2$s'=>'[%1$s] Aktion bestätigen: %2$s','[%s] Erasure Request Fulfilled'=>'[%s] Löschauftrag ausgeführt','[%s] Personal Data Export'=>'[%s] Export personenbezogener Daten', 'You must provide at least one recipient email address.'=>'Du musst mindestens eine Empfänger-E-Mail-Adresse angeben.'],'language'=>'de_DE','x-generator'=>'Poedit 2.2.1']; +return ['domain'=>NULL,'plural-forms'=>'nplurals=2; plural=n != 1;','messages'=>['F j, Y'=>'j. F Y','g:i a'=>'G:i','html_lang_attribute'=>'de-DE','text directionltr'=>'ltr','number_format_decimal_point'=>',','number_format_thousands_sep'=>'.','Update %s now'=>'Jetzt %s aktualisieren','[%1$s] Confirm Action: %2$s'=>'[%1$s] Aktion bestätigen: %2$s','[%s] Erasure Request Fulfilled'=>'[%s] Löschauftrag ausgeführt','[%s] Personal Data Export'=>'[%s] Export personenbezogener Daten', 'You must provide at least one recipient email address.'=>'Du musst mindestens eine Empfänger-E-Mail-Adresse angeben.'],'language'=>'de_DE','x-generator'=>'Poedit 2.2.1']; diff --git a/tests/phpunit/data/languages/de_DE.mo b/tests/phpunit/data/languages/de_DE.mo index 8254de97f2af35c7f3e97a518dce585162dcb2f3..310752e85bcb7f2c3d284a8a823834c3f44d0bad 100644 GIT binary patch delta 381 zcmXxfy-EW?6o%n5n~l5v>ne&UW+X)-#FQ!4PV&=;r4TG+AeSiRo&F>>)cb+io4QDx9Sb4NL7x` GPURO1#wxx5 delta 300 zcmbQnag@FOo)F7a1|Z-9Vi_RL0das}4}=2Kyg>F|AT|Ty(?Bc@#7v9~401qP4oFJ^ zXG)O)hO1A@PAs~M-koE%NZ9psrB&Hv6-aLf(m*{7d!h7xAPqze2Z6+7b;j9~PcSA-e#@jT7L;0$UsR&&Tb!PmqU%(e zUaT9EZ>2C*mfbtVmdpTxZM(!_KiKP5HZ d#dY!uCYQ;+%-i`=QgvNi8FU!*7;;LA7yw!tG4KEY diff --git a/tests/phpunit/data/languages/de_DE.po b/tests/phpunit/data/languages/de_DE.po index b68c53867d8bc..e83837dc92fce 100644 --- a/tests/phpunit/data/languages/de_DE.po +++ b/tests/phpunit/data/languages/de_DE.po @@ -61,3 +61,13 @@ msgstr "[%s] Export personenbezogener Daten" #: wp-includes/class-wp-phpmailer.php:71 msgid "You must provide at least one recipient email address." msgstr "Du musst mindestens eine Empfänger-E-Mail-Adresse angeben." + +#. translators: Default date format, see https://www.php.net/manual/datetime.format.php +#: wp-admin/includes/schema.php:436 +msgid "F j, Y" +msgstr "j. F Y" + +#. translators: Default time format, see https://www.php.net/manual/datetime.format.php +#: wp-admin/includes/schema.php:438 +msgid "g:i a" +msgstr "G:i" diff --git a/tests/phpunit/data/languages/es_ES.l10n.php b/tests/phpunit/data/languages/es_ES.l10n.php index cecec708eac78..4d3e1daa95a93 100644 --- a/tests/phpunit/data/languages/es_ES.l10n.php +++ b/tests/phpunit/data/languages/es_ES.l10n.php @@ -1,2 +1,2 @@ NULL,'plural-forms'=>'nplurals=2; plural=n != 1;','messages'=>['text directionltr'=>'ltr','ERROR: Sorry, that username is not allowed.'=>'ERROR: Lo siento, ese nombre de usuario no está permitido.','Invalid parameter.'=>'Parámetro no válido. ','menu location(Current: %s)'=>'(Actual: %s)','menu(Currently set to: %s)'=>'(Actualmente fijado en: %s)','[%1$s] Confirm Action: %2$s'=>'[%1$s] Confirmar la acción: %2$s','[%s] Erasure Request Fulfilled'=>'[%s] Solicitud de borrado completada','[%s] Personal Data Export'=>'[%s] Exportación de datos personales', 'Manual Offsets' => 'Compensaciones manuales', 'Select a city' => 'Elige una ciudad' ],'language'=>'es_ES','x-generator'=>'Poedit 2.3']; \ No newline at end of file +return ['domain'=>NULL,'plural-forms'=>'nplurals=2; plural=n != 1;','messages'=>['F j, Y'=>'j \\d\\e F \\d\\e Y','g:i a'=>'H:i','text directionltr'=>'ltr','ERROR: Sorry, that username is not allowed.'=>'ERROR: Lo siento, ese nombre de usuario no está permitido.','Invalid parameter.'=>'Parámetro no válido. ','menu location(Current: %s)'=>'(Actual: %s)','menu(Currently set to: %s)'=>'(Actualmente fijado en: %s)','[%1$s] Confirm Action: %2$s'=>'[%1$s] Confirmar la acción: %2$s','[%s] Erasure Request Fulfilled'=>'[%s] Solicitud de borrado completada','[%s] Personal Data Export'=>'[%s] Exportación de datos personales', 'Manual Offsets' => 'Compensaciones manuales', 'Select a city' => 'Elige una ciudad' ],'language'=>'es_ES','x-generator'=>'Poedit 2.3']; \ No newline at end of file diff --git a/tests/phpunit/data/languages/es_ES.mo b/tests/phpunit/data/languages/es_ES.mo index 62e26b49c288c4a2f9e09ed9c7a9224875a15e06..52ecd6ac6876fe155032e5ee0fd39307446711a7 100644 GIT binary patch delta 341 zcmXZXziI+O5XbS^tbZ>4*Cs_m*aM3cY6?3^8wnwh`rrrzB0?fA^|3)8oSj9|2gri)e zE^eYe;xX=r$S#3H>1DK7cc?61tCKq9kyD~C)XI?bE_yEKQJig@9O(k%55^NIL>)kh%aM zEe)jOfV3Qtu7dJsLg}qQ8l>+ckk$a=CqV22$ z^lTul0;Kl>X^;hvfHV(~{s*Lih=H4hfq`-2Lt#k}2gnBdfD=fw12G2>vrU#{be){a Un7#QL<3YyB%b7bS8?)#F02zrJp8x;= diff --git a/tests/phpunit/data/languages/es_ES.po b/tests/phpunit/data/languages/es_ES.po index 079d377c4338d..2d57b354c3051 100644 --- a/tests/phpunit/data/languages/es_ES.po +++ b/tests/phpunit/data/languages/es_ES.po @@ -62,3 +62,13 @@ msgstr "[%1$s] Confirmar la acción: %2$s" msgid "[%s] Erasure Request Fulfilled" msgstr "[%s] Solicitud de borrado completada" + +#. translators: Default date format, see https://www.php.net/manual/datetime.format.php +#: wp-admin/includes/schema.php:436 +msgid "F j, Y" +msgstr "j \\d\\e F \\d\\e Y" + +#. translators: Default time format, see https://www.php.net/manual/datetime.format.php +#: wp-admin/includes/schema.php:438 +msgid "g:i a" +msgstr "H:i" diff --git a/tests/phpunit/tests/admin/includes/options/wpUpdateDateTimeFormatsOnLocaleChange.php b/tests/phpunit/tests/admin/includes/options/wpUpdateDateTimeFormatsOnLocaleChange.php new file mode 100644 index 0000000000000..f4fcb1ebf5c1b --- /dev/null +++ b/tests/phpunit/tests/admin/includes/options/wpUpdateDateTimeFormatsOnLocaleChange.php @@ -0,0 +1,102 @@ +assertSame( $new_date, get_option( 'date_format' ), 'The date format is not correct.' ); + $this->assertSame( $new_time, get_option( 'time_format' ), 'The time format is not correct.' ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_should_update_default_formats() { + return array( + 'English to German, default formats' => array( 'en_US', 'de_DE', 'F j, Y', 'g:i a', 'j. F Y', 'G:i' ), + 'German to Spanish, default formats' => array( 'de_DE', 'es_ES', 'j. F Y', 'G:i', 'j \d\e F \d\e Y', 'H:i' ), + 'Spanish to English, default formats' => array( 'es_ES', 'en_US', 'j \d\e F \d\e Y', 'H:i', 'F j, Y', 'g:i a' ), + 'English to German, custom date' => array( 'en_US', 'de_DE', 'Y-m-d', 'g:i a', 'Y-m-d', 'G:i' ), + 'English to German, custom time' => array( 'en_US', 'de_DE', 'F j, Y', 'H:i:s', 'j. F Y', 'H:i:s' ), + 'English to German, custom formats' => array( 'en_US', 'de_DE', 'd M Y', 'g:i A', 'd M Y', 'g:i A' ), + 'German to English, English formats' => array( 'de_DE', 'en_US', 'F j, Y', 'g:i a', 'F j, Y', 'g:i a' ), + 'Same locale, other locale defaults' => array( 'en_US', 'en_US', 'j. F Y', 'G:i', 'j. F Y', 'G:i' ), + ); + } + + /** + * @ticket 36259 + */ + public function test_should_restore_translations_of_current_locale() { + wp_update_date_time_formats_on_locale_change( 'en_US', 'de_DE' ); + + $this->assertSame( 'F j, Y', __( 'F j, Y' ) ); + } + + /** + * @ticket 36259 + */ + public function test_should_use_site_locales_when_user_locale_differs() { + $user_id = self::factory()->user->create( + array( + 'role' => 'administrator', + 'locale' => 'de_DE', + ) + ); + wp_set_current_user( $user_id ); + set_current_screen( 'options-general' ); + + update_option( 'date_format', 'j \d\e F \d\e Y' ); + update_option( 'time_format', 'H:i' ); + + wp_update_date_time_formats_on_locale_change( 'es_ES', 'en_US' ); + + $this->assertSame( 'F j, Y', get_option( 'date_format' ), 'The date format is not correct.' ); + $this->assertSame( 'g:i a', get_option( 'time_format' ), 'The time format is not correct.' ); + $this->assertSame( 'j. F Y', __( 'F j, Y' ), 'The translations of the user locale were not restored.' ); + } +}