WordPress.org

Making WordPress.org

Changeset 5790


Ignore:
Timestamp:
08/09/2017 09:32:46 PM (11 months ago)
Author:
coffee2code
Message:

Browse Happy: Fix/improve locale detection.

  • Takes into account preference weighting specified in Accept-Language request header.
  • Stops skipping English languages, which resulted in first non-English language with translations getting chosen.
  • Permits non-en_US English variant translations to be loaded, if present.

See https://github.com/WordPress/browsehappy/issues/23

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/browsehappy.com/public_html/inc/locale.php

    r2212 r5790  
    3838
    3939    static function guess_locale() {
    40         if ( ! isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) )
     40        if ( ! isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) {
    4141            return;
     42        }
    4243
    43         if ( ! preg_match_all( '/([a-z]{2,}(\-[a-z]{2,})?)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches ) )
    44             return;
     44        $locales = array();
     45        $variant = '';
    4546
    46         $locales = $matches[0];
     47        $langs = explode( ',', $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
    4748
    48         foreach ( $locales as $locale ) {
    49             if ( strpos( $locale, 'en' ) === 0 )
    50                 continue;
     49        foreach ( $langs as $lang ) {
     50            $lang   = str_replace( 'q=', '', $lang );
     51            $parts  = explode( ';', trim( $lang ) );
     52            $locale = $parts[0];
     53            $weight = empty( $parts[1] ) ? '1.0' : $parts[1];
    5154
    52             if ( $maybe = self::check_variants( $locale ) )
    53                 return $maybe;
     55            $locales[ $locale ] = $weight;
    5456        }
     57
     58        // Sort locales by browser-provided weighting.
     59        arsort( $locales, SORT_NUMERIC );
     60
     61        // Find the best locale variant.
     62        foreach ( array_keys( $locales ) as $locale_pref ) {
     63            // Preference for 'en' or 'en-US' forces default language.
     64            if ( in_array( $locale_pref, array( 'en', 'en-US' ) ) ) {
     65                break;
     66            }
     67
     68            // Check for the closest language variant.
     69            if ( $maybe = self::check_variants( $locale_pref ) ) {
     70                $variant = $maybe;
     71            }
     72
     73            // For English, only use an exact variant, otherwise fall back to default.
     74            if ( 0 === strpos( $locale_pref, 'en' ) ) {
     75                if ( $variant && $variant != str_replace( '-', '_', $locale_pref ) ) {
     76                    $variant = '';
     77                }
     78                break;
     79            }
     80
     81            // Stop searching if a valid variant has been found.
     82            if ( $variant ) {
     83                break;
     84            }
     85        }
     86
     87        return $variant;
    5588    }
    5689
Note: See TracChangeset for help on using the changeset viewer.