@@ -2007,96 +2007,98 @@ function wp_get_word_count_type() {
20072007function get_locales_from_accept_language_header () {
20082008 global $ wpdb ;
20092009
2010+ if ( empty ( $ _SERVER ['HTTP_ACCEPT_LANGUAGE ' ] ) || ! is_string ( $ _SERVER ['HTTP_ACCEPT_LANGUAGE ' ] ) ) {
2011+ return array ();
2012+ }
2013+
20102014 $ locales = array ();
20112015
2012- if ( ! empty ( $ _SERVER ['HTTP_ACCEPT_LANGUAGE ' ] ) && is_string ( $ _SERVER ['HTTP_ACCEPT_LANGUAGE ' ] ) ) {
2013- $ matches = array ();
2014- // Parses a header value such as "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5".
2015- preg_match_all ( '((?P<code>[a-z-_A-Z]+|\*)([;q=]+?(?P<prio>1|0\.\d))?) ' , $ _SERVER ['HTTP_ACCEPT_LANGUAGE ' ], $ matches );
2016+ $ matches = array ();
2017+ // Parses a header value such as "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5".
2018+ preg_match_all ( '((?P<code>[a-z-_A-Z]+|\*)([;q=]+?(?P<prio>1|0\.\d))?) ' , $ _SERVER ['HTTP_ACCEPT_LANGUAGE ' ], $ matches );
20162019
2017- if ( empty ( $ matches ['code ' ] ) ) {
2018- return $ locales ;
2019- }
2020+ if ( empty ( $ matches ['code ' ] ) ) {
2021+ return $ locales ;
2022+ }
20202023
2021- $ codes = $ matches ['code ' ];
2024+ $ codes = $ matches ['code ' ];
20222025
2023- // An empty priority defaults to 1.
2024- $ prios = array_map (
2025- static function ( $ value ) {
2026- if ( '' === $ value ) {
2027- return 1.0 ;
2028- }
2026+ // An empty priority defaults to 1.
2027+ $ prios = array_map (
2028+ static function ( $ value ) {
2029+ if ( '' === $ value ) {
2030+ return 1.0 ;
2031+ }
20292032
2030- return (float ) $ value ;
2031- },
2032- $ matches ['prio ' ]
2033- );
2033+ return (float ) $ value ;
2034+ },
2035+ $ matches ['prio ' ]
2036+ );
20342037
2035- // Sort codes by priority.
2036- usort (
2037- $ codes ,
2038- static function ( $ a , $ b ) use ( $ codes , $ prios ) {
2039- $ index_a = array_search ( $ a , $ codes , true );
2040- $ index_b = array_search ( $ b , $ codes , true );
2038+ // Sort codes by priority.
2039+ usort (
2040+ $ codes ,
2041+ static function ( $ a , $ b ) use ( $ codes , $ prios ) {
2042+ $ index_a = array_search ( $ a , $ codes , true );
2043+ $ index_b = array_search ( $ b , $ codes , true );
20412044
2042- return $ prios [ $ index_b ] <=> $ prios [ $ index_a ];
2043- }
2044- );
2045+ return $ prios [ $ index_b ] <=> $ prios [ $ index_a ];
2046+ }
2047+ );
20452048
2046- $ translations = array ();
2049+ $ translations = array ();
20472050
2048- /*
2049- * Get list of available translations without potentially deleting an expired transient and causing an HTTP request.
2050- * Only works if either the object cache or the database are already available.
2051- */
2052- if ( wp_using_ext_object_cache () || wp_installing () ) {
2053- wp_start_object_cache ();
2054- $ translations = wp_cache_get ( 'available_translations ' , 'site-transient ' );
2055- } elseif ( isset ( $ wpdb ) ) {
2056- $ translations = get_site_option ( '_site_transient_available_translations ' );
2051+ /*
2052+ * Get list of available translations without potentially deleting an expired transient and causing an HTTP request.
2053+ * Only works if either the object cache or the database are already available.
2054+ */
2055+ if ( wp_using_ext_object_cache () ) {
2056+ wp_start_object_cache ();
2057+ $ translations = wp_cache_get ( 'available_translations ' , 'site-transient ' );
2058+ } elseif ( isset ( $ wpdb ) ) {
2059+ $ translations = get_site_option ( '_site_transient_available_translations ' );
2060+ }
2061+
2062+ $ has_available_translations = is_array ( $ translations ) && ! empty ( $ translations );
2063+
2064+ foreach ( $ codes as $ code ) {
2065+ if ( '* ' === $ code ) {
2066+ // Ignore anything after the wildcard, as we can then just default to en_US.
2067+ break ;
20572068 }
20582069
2059- $ has_available_translations = is_array ( $ translations ) && ! empty ( $ translations );
2070+ $ locale = sanitize_locale_name ( str_replace ( ' - ' , ' _ ' , $ code ) );
20602071
2061- foreach ( $ codes as $ code ) {
2062- if ( '* ' === $ code ) {
2063- // Ignore anything after the wildcard, as we can then just default to en_US.
2064- break ;
2065- }
2072+ if ( '' === $ locale ) {
2073+ continue ;
2074+ }
20662075
2067- $ locale = sanitize_locale_name ( str_replace ( '- ' , '_ ' , $ code ) );
2076+ // If English is accepted, then there is no point in adding any other locales after it.
2077+ if ( 'en ' === $ locale ) {
2078+ break ;
2079+ }
20682080
2069- if ( '' === $ locale ) {
2070- continue ;
2071- }
2081+ if ( $ has_available_translations ) {
2082+ $ found = array_keys (
2083+ array_filter (
2084+ $ translations ,
2085+ static function ( $ translation ) use ( $ locale , $ code ) {
2086+ return $ locale === $ translation ['language ' ] || in_array ( $ code , $ translation ['iso ' ], true );
2087+ }
2088+ )
2089+ );
2090+ sort ( $ found );
20722091
2073- // If English is accepted, then there is no point in adding any other locales after it.
2074- if ( 'en ' === $ locale ) {
2075- break ;
2092+ if ( ! empty ( $ found ) ) {
2093+ array_push ( $ locales , ...$ found );
20762094 }
2095+ } else {
20772096
2078- if ( $ has_available_translations ) {
2079- $ found = array_keys (
2080- array_filter (
2081- $ translations ,
2082- static function ( $ translation ) use ( $ locale ) {
2083- return $ locale === $ translation ['language ' ] || in_array ( $ locale , $ translation ['iso ' ], true );
2084- }
2085- )
2086- );
2087- sort ( $ found );
2088-
2089- if ( ! empty ( $ found ) ) {
2090- array_push ( $ locales , ...$ found );
2091- }
2092- } else {
2093-
2094- $ locales [] = $ locale ;
2097+ $ locales [] = $ locale ;
20952098
2096- // Fallback approximation, supporting cases like "el", but also "fr" -> "fr_FR",
2097- if ( 2 === strlen ( $ locale ) ) {
2098- $ locales [] = $ locale . '_ ' . strtoupper ( $ locale );
2099- }
2099+ // Fallback approximation, supporting cases like "el", but also "fr" -> "fr_FR",
2100+ if ( 2 === strlen ( $ locale ) ) {
2101+ $ locales [] = $ locale . '_ ' . strtoupper ( $ locale );
21002102 }
21012103 }
21022104 }
0 commit comments