WordPress.org

Make WordPress Core

Changeset 30335


Ignore:
Timestamp:
11/13/2014 05:00:08 PM (5 years ago)
Author:
ocean90
Message:

Site Language: Install translations on the fly.

The language dropdown now includes installed languages and all available translations when the filesystem is writable by WordPress.
Go to wp-admin/options-general.php, select one of the available translations, submit the form and let WordPress handle the rest.
Works for Multisite's Default Language too.

see #29395.

Location:
trunk/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/css/forms.css

    r30333 r30335  
    612612}
    613613
    614 table.form-table td .updated p { 
    615     font-size: 13px; 
    616     margin: 0.3em 0; 
     614table.form-table td .updated p {
     615    font-size: 13px;
     616    margin: 0.3em 0;
    617617}
    618618
     
    768768}
    769769
     770.settings-php .language-install-spinner,
     771.options-general-php .language-install-spinner {
     772    display: inline-block;
     773    float: none;
     774    margin: -3px 5px 0;
     775    vertical-align: middle;
     776}
     777
    770778/* =Media Queries
    771779-------------------------------------------------------------- */
  • trunk/src/wp-admin/network/settings.php

    r29718 r30335  
    1111require_once( dirname( __FILE__ ) . '/admin.php' );
    1212
     13/** WordPress Translation Install API */
     14require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
     15
    1316if ( ! is_multisite() )
    1417    wp_die( __( 'Multisite support is not enabled.' ) );
     
    1922$title = __( 'Network Settings' );
    2023$parent_file = 'settings.php';
     24
     25/**
     26 * Display JavaScript on the page.
     27 *
     28 * @since 4.1.0
     29*/
     30function network_settings_add_js() {
     31?>
     32<script type="text/javascript">
     33jQuery(document).ready( function($) {
     34    var languageSelect = $( '#WPLANG' );
     35    $( 'form' ).submit( function() {
     36        // Don't show a spinner for English and installed languages,
     37        // as there is nothing to download.
     38        if ( ! languageSelect.find( 'option:selected' ).data( 'installed' ) ) {
     39            $( '#submit', this ).after( '<span class="spinner language-install-spinner" />' );
     40        }
     41    });
     42});
     43</script>
     44<?php
     45}
     46add_action( 'admin_head', 'network_settings_add_js' );
    2147
    2248get_current_screen()->add_help_tab( array(
     
    5985    );
    6086
     87    // Handle translation install.
     88    if ( ! empty( $_POST['WPLANG'] ) && wp_can_install_language_pack() ) {  // @todo: Skip if already installed
     89        $language = wp_download_language_pack( $_POST['WPLANG'] );
     90        if ( $language ) {
     91            $_POST['WPLANG'] = $language;
     92        }
     93    }
     94
    6195    foreach ( $options as $option_name ) {
    6296        if ( ! isset($_POST[$option_name]) )
     
    276310        <?php
    277311        $languages = get_available_languages();
    278         if ( ! empty( $languages ) ) {
     312        $translations = wp_get_available_translations();
     313        if ( ! empty( $languages ) || ! empty( $translations ) ) {
    279314            ?>
    280315            <h3><?php _e( 'Language Settings' ); ?></h3>
     
    290325
    291326                        wp_dropdown_languages( array(
    292                             'name'      => 'WPLANG',
    293                             'id'        => 'WPLANG',
    294                             'selected'  => $lang,
    295                             'languages' => $languages,
     327                            'name'         => 'WPLANG',
     328                            'id'           => 'WPLANG',
     329                            'selected'     => $lang,
     330                            'languages'    => $languages,
     331                            'translations' => $translations,
     332                            'show_available_translations' => wp_can_install_language_pack(),
    296333                        ) );
    297334                        ?>
  • trunk/src/wp-admin/options-general.php

    r29963 r30335  
    99/** WordPress Administration Bootstrap */
    1010require_once( dirname( __FILE__ ) . '/admin.php' );
     11
     12/** WordPress Translation Install API */
     13require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
    1114
    1215if ( ! current_user_can( 'manage_options' ) )
     
    6366                    date : format.val()
    6467                }, function(d) { format.siblings('.spinner').hide(); format.siblings('.example').text(d); } );
     68        });
     69
     70        var languageSelect = $( '#WPLANG' );
     71        $( 'form' ).submit( function() {
     72            // Don't show a spinner for English and installed languages,
     73            // as there is nothing to download.
     74            if ( ! languageSelect.find( 'option:selected' ).data( 'installed' ) ) {
     75                $( '#submit', this ).after( '<span class="spinner language-install-spinner" />' );
     76            }
    6577        });
    6678    });
     
    319331<?php
    320332$languages = get_available_languages();
     333$translations = wp_get_available_translations();
    321334if ( ! is_multisite() && defined( 'WPLANG' ) && '' !== WPLANG && 'en_US' !== WPLANG && ! in_array( WPLANG, $languages ) ) {
    322335    $languages[] = WPLANG;
    323336}
    324 if ( $languages ) {
     337if ( ! empty( $languages ) || ! empty( $translations ) ) {
    325338    ?>
    326339    <tr>
     
    334347
    335348            wp_dropdown_languages( array(
    336                 'name'      => 'WPLANG',
    337                 'id'        => 'WPLANG',
    338                 'selected'  => $locale,
    339                 'languages' => $languages,
     349                'name'         => 'WPLANG',
     350                'id'           => 'WPLANG',
     351                'selected'     => $locale,
     352                'languages'    => $languages,
     353                'translations' => $translations,
     354                'show_available_translations' => ( ! is_multisite() || is_super_admin() ) && wp_can_install_language_pack(),
    340355            ) );
    341356
  • trunk/src/wp-admin/options.php

    r29630 r30335  
    152152    }
    153153
    154     // Handle custom date/time formats.
    155154    if ( 'general' == $option_page ) {
     155        // Handle custom date/time formats.
    156156        if ( !empty($_POST['date_format']) && isset($_POST['date_format_custom']) && '\c\u\s\t\o\m' == wp_unslash( $_POST['date_format'] ) )
    157157            $_POST['date_format'] = $_POST['date_format_custom'];
     
    163163            $_POST['gmt_offset'] = preg_replace('/UTC\+?/', '', $_POST['gmt_offset']);
    164164            $_POST['timezone_string'] = '';
     165        }
     166
     167        // Handle translation install.
     168        if ( ! empty( $_POST['WPLANG'] ) && ( ! is_multisite() || is_super_admin() ) ) { // @todo: Skip if already installed
     169            require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
     170
     171            if ( wp_can_install_language_pack() ) {
     172                $language = wp_download_language_pack( $_POST['WPLANG'] );
     173                if ( $language ) {
     174                    $_POST['WPLANG'] = $language;
     175                }
     176            }
    165177        }
    166178    }
  • trunk/src/wp-includes/l10n.php

    r29984 r30335  
    850850 * @see wp_get_available_translations()
    851851 *
    852  * @param array $args Optional arguments. Default empty array.
     852 * @param string|array $query {
     853 *     Optional. Array of arguments.
     854 *
     855 *     @type string  $id                           ID attribute of the select element. Default empty.
     856 *     @type string  $name                         Name attribute of the select element. Default empty.
     857 *     @type array   $languages                    List of installed languages, contain only the locales.
     858 *                                                 Default empty array.
     859 *     @type array   $translations                 List of available translations. Default result of
     860 *                                                 {@see wp_get_available_translations()}.
     861 *     @type string  $selected                     Language which should be selected. Default empty.
     862 *     @type bool    $show_available_translations  Whether to show available translations. Default true.
     863 * }
    853864 */
    854865function wp_dropdown_languages( $args = array() ) {
    855     require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
    856866
    857867    $args = wp_parse_args( $args, array(
    858         'id'        => '',
    859         'name'      => '',
    860         'languages' => array(),
    861         'selected'  => ''
     868        'id'           => '',
     869        'name'         => '',
     870        'languages'    => array(),
     871        'translations' => array(),
     872        'selected'     => '',
     873        'show_available_translations' => true,
    862874    ) );
    863875
    864     if ( empty( $args['languages'] ) ) {
    865         return false;
    866     }
    867 
    868     $translations = wp_get_available_translations();
     876    $translations = $args['translations'];
     877    if ( empty( $translations ) ) {
     878        require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
     879        $translations = wp_get_available_translations();
     880    }
    869881
    870882    /*
     
    881893                'lang'        => $translation['iso'][1],
    882894            );
     895
     896            // Remove installed language from available translations.
     897            unset( $translations[ $locale ] );
    883898        } else {
    884899            $languages[] = array(
     
    892907    printf( '<select name="%s" id="%s">', esc_attr( $args['name'] ), esc_attr( $args['id'] ) );
    893908
     909    // Holds the HTML markup.
     910    $structure = array();
     911
    894912    // List installed languages.
    895     echo '<option value="" lang="en">English (United States)</option>';
     913    $structure[] = '<optgroup label="' . esc_attr_x( 'Installed', 'translations' ) . '">';
     914    $structure[] = '<option value="" lang="en" data-installed="1">English (United States)</option>';
    896915    foreach ( $languages as $language ) {
    897         $selected = selected( $language['language'], $args['selected'], false );
    898         printf(
    899             '<option value="%s" lang="%s"%s>%s</option>',
     916        $structure[] = sprintf(
     917            '<option value="%s" lang="%s"%s data-installed="1">%s</option>',
    900918            esc_attr( $language['language'] ),
    901919            esc_attr( $language['lang'] ),
    902             $selected,
     920            selected( $language['language'], $args['selected'], false ),
    903921            esc_html( $language['native_name'] )
    904922        );
    905923    }
     924    $structure[] = '</optgroup>';
     925
     926    // List available translations.
     927    if ( ! empty( $translations ) && $args['show_available_translations'] ) {
     928        $structure[] = '<optgroup label="' . esc_attr_x( 'Available', 'translations' ) . '">';
     929        foreach ( $translations as $translation ) {
     930            $structure[] = sprintf(
     931                '<option value="%s" lang="%s"%s>%s</option>',
     932                esc_attr( $translation['language'] ),
     933                esc_attr( $translation['iso'][1] ),
     934                selected( $translation['language'], $args['selected'], false ),
     935                esc_html( $translation['native_name'] )
     936            );
     937        }
     938        $structure[] = '</optgroup>';
     939    }
     940
     941    echo join( "\n", $structure );
    906942
    907943    echo '</select>';
Note: See TracChangeset for help on using the changeset viewer.