Skip to content

Commit 157a233

Browse files
committed
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
1 parent 9599b39 commit 157a233

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

src/wp-admin/css/forms.css

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,9 @@ table.form-table td .updated p {
10691069
}
10701070

10711071
.settings-php .language-install-spinner,
1072-
.options-general-php .language-install-spinner {
1072+
.options-general-php .language-install-spinner,
1073+
.user-edit-php .language-install-spinner,
1074+
.profile-php .language-install-spinner {
10731075
display: inline-block;
10741076
float: none;
10751077
margin: -3px 5px 0;

src/wp-admin/includes/user.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,13 @@ function edit_user( $user_id = 0 ) {
119119
} elseif ( '' === $locale ) {
120120
$locale = 'en_US';
121121
} elseif ( ! in_array( $locale, get_available_languages(), true ) ) {
122-
$locale = '';
122+
if ( current_user_can( 'install_languages' ) && wp_can_install_language_pack() ) {
123+
if ( ! wp_download_language_pack( $locale ) ) {
124+
$locale = '';
125+
}
126+
} else {
127+
$locale = '';
128+
}
123129
}
124130

125131
$user->locale = $locale;

src/wp-admin/user-edit.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
/** WordPress Administration Bootstrap */
1010
require_once __DIR__ . '/admin.php';
1111

12+
/** WordPress Translation Installation API */
13+
require_once ABSPATH . 'wp-admin/includes/translation-install.php';
14+
1215
wp_reset_vars( array( 'action', 'user_id', 'wp_http_referer' ) );
1316

1417
$user_id = (int) $user_id;
@@ -345,8 +348,11 @@
345348
</td>
346349
</tr>
347350

348-
<?php $languages = get_available_languages(); ?>
349-
<?php if ( $languages ) : ?>
351+
<?php
352+
$languages = get_available_languages();
353+
$can_install_translations = current_user_can( 'install_languages' ) && wp_can_install_language_pack();
354+
?>
355+
<?php if ( $languages || $can_install_translations ) : ?>
350356
<tr class="user-language-wrap">
351357
<th scope="row">
352358
<?php /* translators: The user language selection field label. */ ?>
@@ -364,12 +370,12 @@
364370

365371
wp_dropdown_languages(
366372
array(
367-
'name' => 'locale',
368-
'id' => 'locale',
369-
'selected' => $user_locale,
370-
'languages' => $languages,
371-
'show_available_translations' => false,
372-
'show_option_site_default' => true,
373+
'name' => 'locale',
374+
'id' => 'locale',
375+
'selected' => $user_locale,
376+
'languages' => $languages,
377+
'show_available_translations' => $can_install_translations,
378+
'show_option_site_default' => true,
373379
)
374380
);
375381
?>
@@ -911,6 +917,19 @@
911917
}
912918
</script>
913919

920+
<script type="text/javascript">
921+
jQuery( function( $ ) {
922+
var languageSelect = $( '#locale' );
923+
$( 'form' ).on( 'submit', function() {
924+
// Don't show a spinner for English and installed languages,
925+
// as there is nothing to download.
926+
if ( ! languageSelect.find( 'option:selected' ).data( 'installed' ) ) {
927+
$( '#submit', this ).after( '<span class="spinner language-install-spinner is-active" />' );
928+
}
929+
});
930+
} );
931+
</script>
932+
914933
<?php if ( isset( $application_passwords_list_table ) ) : ?>
915934
<script type="text/html" id="tmpl-new-application-password">
916935
<div class="notice notice-success is-dismissible new-application-password-notice" role="alert" tabindex="-1">

0 commit comments

Comments
 (0)