Making WordPress.org

Changeset 6677


Ignore:
Timestamp:
02/17/2018 01:40:56 PM (8 years ago)
Author:
ocean90
Message:

Login: Add a simple language switcher.

See #1673.

Location:
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/functions.php

    r6645 r6677  
    147147}
    148148add_filter( 'insert_user_meta', 'wporg_login_limit_user_meta', 1 );
     149
     150/**
     151 * Retreives all avaiable locales with their native names.
     152 *
     153 * @return array Locales with their native names.
     154 */
     155function wporg_login_get_locales() {
     156        wp_cache_add_global_groups( [ 'locale-associations' ] );
     157
     158        $wp_locales = wp_cache_get( 'locale-list', 'locale-associations' );
     159        if ( false === $wp_locales ) {
     160                $wp_locales = (array) $GLOBALS['wpdb']->get_col( 'SELECT locale FROM wporg_locales' );
     161                wp_cache_set( 'locale-list', $wp_locales, 'locale-associations' );
     162        }
     163
     164        $wp_locales[] = 'en_US';
     165
     166        require_once GLOTPRESS_LOCALES_PATH;
     167
     168        $locales = [];
     169
     170        foreach ( $wp_locales as $locale ) {
     171                $gp_locale = GP_Locales::by_field( 'wp_locale', $locale );
     172                if ( ! $gp_locale ) {
     173                        continue;
     174                }
     175
     176                $locales[ $locale ] = $gp_locale->native_name;
     177        }
     178
     179        natsort( $locales );
     180
     181        return $locales;
     182}
     183
     184/**
     185 * Prints markup for a simple language switcher.
     186 */
     187function wporg_login_language_switcher() {
     188        $current_locale = get_locale();
     189
     190        ?>
     191        <div class="language-switcher">
     192                <form id="language-switcher" action="" method="GET">
     193                        <label for="language-switcher-locales">
     194                                <span aria-hidden="true" class="dashicons dashicons-translation"></span>
     195                                <span class="screen-reader-text"><?php _e( 'Select the language:', 'wporg' ); ?></span>
     196                        </label>
     197                        <select id="language-switcher-locales" name="locale">
     198                                <?php
     199                                foreach ( wporg_login_get_locales() as $locale => $locale_name ) {
     200                                        printf(
     201                                                '<option value="%s"%s>%s</option>',
     202                                                esc_attr( $locale ),
     203                                                selected( $locale, $current_locale, false ),
     204                                                esc_html( $locale_name )
     205                                        );
     206                                }
     207                                ?>
     208                        </select>
     209                </form>
     210        </div>
     211        <script>
     212                var switcherForm  = document.getElementById( 'language-switcher' );
     213                var localesSelect = document.getElementById( 'language-switcher-locales' );
     214                localesSelect.addEventListener( 'change', function() {
     215                        switcherForm.submit()
     216                } );
     217        </script>
     218        <?php
     219}
     220add_action( 'login_footer', 'wporg_login_language_switcher' );
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/stylesheets/login.css

    r6672 r6677  
    1313        -ms-flex-align: center;
    1414        align-items: center;
     15        -webkit-box-orient: vertical;
     16        -webkit-box-direction: normal;
     17        -ms-flex-direction: column;
     18        flex-direction: column;
     19        -webkit-box-pack: center;
     20        -ms-flex-pack: center;
     21        justify-content: center;
    1522        height: auto;
    1623        min-height: 100%;
     
    482489}
    483490
     491.screen-reader-text {
     492        border: 0;
     493        clip: rect(1px, 1px, 1px, 1px);
     494        -webkit-clip-path: inset(50%);
     495        clip-path: inset(50%);
     496        height: 1px;
     497        margin: -1px;
     498        overflow: hidden;
     499        padding: 0;
     500        position: absolute;
     501        width: 1px;
     502        word-wrap: normal !important;
     503}
     504
     505.language-switcher {
     506        width: 350px;
     507        margin: 0 auto 24px;
     508        text-align: center;
     509}
     510
     511.language-switcher label {
     512        margin-right: .5em;
     513}
     514
     515.rtl .language-switcher label {
     516        margin-right: 0;
     517        margin-left: .5em;
     518}
     519
     520.language-switcher span.dashicons {
     521        line-height: 30px;
     522}
     523
     524.language-switcher select {
     525        height: 30px;
     526}
     527
    484528@media (max-width: 375px) {
    485529        body, html {
     
    541585        }
    542586
     587        .language-switcher {
     588                width: 100%;
     589        }
     590
    543591        #login h1 {
    544592                padding: 0 24px 24px;
Note: See TracChangeset for help on using the changeset viewer.