Make WordPress Core


Ignore:
Timestamp:
11/13/2014 05:00:08 PM (10 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.