I18N: Allow installing new translations when changing the user locale on the profile page.

Up until now, new translations could only be installed via Settings -> General.
When editing the user profile, one could only select locales that were already installed.

This change allows also installing new translations if the editing user has the necessary capabilities.

Props barryceelen, johnbillion, ocean90, swissspidy.
Fixes #38664.

git-svn-id: https://develop.svn.wordpress.org/trunk@55099 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler
2023-01-19 14:11:08 +00:00
parent 9599b39a5a
commit 157a23382d
3 changed files with 37 additions and 10 deletions

View File

@@ -1069,7 +1069,9 @@ table.form-table td .updated p {
}
.settings-php .language-install-spinner,
.options-general-php .language-install-spinner {
.options-general-php .language-install-spinner,
.user-edit-php .language-install-spinner,
.profile-php .language-install-spinner {
display: inline-block;
float: none;
margin: -3px 5px 0;

View File

@@ -119,7 +119,13 @@ function edit_user( $user_id = 0 ) {
} elseif ( '' === $locale ) {
$locale = 'en_US';
} elseif ( ! in_array( $locale, get_available_languages(), true ) ) {
$locale = '';
if ( current_user_can( 'install_languages' ) && wp_can_install_language_pack() ) {
if ( ! wp_download_language_pack( $locale ) ) {
$locale = '';
}
} else {
$locale = '';
}
}
$user->locale = $locale;

View File

@@ -9,6 +9,9 @@
/** WordPress Administration Bootstrap */
require_once __DIR__ . '/admin.php';
/** WordPress Translation Installation API */
require_once ABSPATH . 'wp-admin/includes/translation-install.php';
wp_reset_vars( array( 'action', 'user_id', 'wp_http_referer' ) );
$user_id = (int) $user_id;
@@ -345,8 +348,11 @@ switch ( $action ) {
</td>
</tr>
<?php $languages = get_available_languages(); ?>
<?php if ( $languages ) : ?>
<?php
$languages = get_available_languages();
$can_install_translations = current_user_can( 'install_languages' ) && wp_can_install_language_pack();
?>
<?php if ( $languages || $can_install_translations ) : ?>
<tr class="user-language-wrap">
<th scope="row">
<?php /* translators: The user language selection field label. */ ?>
@@ -364,12 +370,12 @@ switch ( $action ) {
wp_dropdown_languages(
array(
'name' => 'locale',
'id' => 'locale',
'selected' => $user_locale,
'languages' => $languages,
'show_available_translations' => false,
'show_option_site_default' => true,
'name' => 'locale',
'id' => 'locale',
'selected' => $user_locale,
'languages' => $languages,
'show_available_translations' => $can_install_translations,
'show_option_site_default' => true,
)
);
?>
@@ -911,6 +917,19 @@ switch ( $action ) {
}
</script>
<script type="text/javascript">
jQuery( function( $ ) {
var languageSelect = $( '#locale' );
$( 'form' ).on( 'submit', function() {
// Don't show a spinner for English and installed languages,
// as there is nothing to download.
if ( ! languageSelect.find( 'option:selected' ).data( 'installed' ) ) {
$( '#submit', this ).after( '<span class="spinner language-install-spinner is-active" />' );
}
});
} );
</script>
<?php if ( isset( $application_passwords_list_table ) ) : ?>
<script type="text/html" id="tmpl-new-application-password">
<div class="notice notice-success is-dismissible new-application-password-notice" role="alert" tabindex="-1">