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 8254de97f2af3..310752e85bcb7 100644 Binary files a/tests/phpunit/data/languages/de_DE.mo and b/tests/phpunit/data/languages/de_DE.mo differ 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 62e26b49c288c..52ecd6ac6876f 100644 Binary files a/tests/phpunit/data/languages/es_ES.mo and b/tests/phpunit/data/languages/es_ES.mo differ 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.' ); + } +}