|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Set up some helper functions for fetching locale data. |
| 4 | + */ |
| 5 | + |
| 6 | +namespace WordPressdotorg\MU_Plugins\Helpers\Locale; |
| 7 | + |
| 8 | +/** |
| 9 | + * Get all locales with subdomain mapping. |
| 10 | + */ |
| 11 | +function get_all_locales_with_subdomain() { |
| 12 | + global $wpdb; |
| 13 | + return $wpdb->get_results( |
| 14 | + "SELECT locale, subdomain FROM wporg_locales WHERE locale NOT LIKE '%\_%\_%'", |
| 15 | + OBJECT_K |
| 16 | + ); |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Get all available locales with valid WordPress locale values. |
| 21 | + * |
| 22 | + * Not all locales have valid WordPress sites, this filters out those that |
| 23 | + * don't exist. |
| 24 | + */ |
| 25 | +function get_all_valid_locales() { |
| 26 | + $all_locales = get_all_locales_with_subdomain(); |
| 27 | + // Retrieve all the WordPress locales. |
| 28 | + $all_locales = wp_list_pluck( $all_locales, 'locale' ); |
| 29 | + |
| 30 | + return array_filter( |
| 31 | + $all_locales, |
| 32 | + function( $locale ) { |
| 33 | + return \GP_Locales::by_field( 'wp_locale', $locale ); |
| 34 | + } |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Get locales matching the HTTP accept language header. |
| 40 | + * |
| 41 | + * @return array List of locales. |
| 42 | + */ |
| 43 | +function get_locale_from_header() { |
| 44 | + $res = array(); |
| 45 | + |
| 46 | + $available_locales = get_all_valid_locales(); |
| 47 | + if ( ! $available_locales ) { |
| 48 | + return $res; |
| 49 | + } |
| 50 | + |
| 51 | + if ( ! isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) { |
| 52 | + return $res; |
| 53 | + } |
| 54 | + |
| 55 | + $http_locales = get_http_locales( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ); // phpcs:ignore |
| 56 | + |
| 57 | + if ( is_array( $http_locales ) ) { |
| 58 | + foreach ( $http_locales as $http_locale ) { |
| 59 | + $lang = $http_locale; |
| 60 | + $region = $http_locale; |
| 61 | + if ( str_contains( $http_locale, '-' ) ) { |
| 62 | + list( $lang, $region ) = explode( '-', $http_locale ); |
| 63 | + } |
| 64 | + |
| 65 | + /* |
| 66 | + * Discard English -- it's the default for all browsers, |
| 67 | + * ergo not very reliable information |
| 68 | + */ |
| 69 | + if ( 'en' === $lang ) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + // Region should be uppercase. |
| 74 | + $region = strtoupper( $region ); |
| 75 | + |
| 76 | + $mapped = map_locale( $lang, $region, $available_locales ); |
| 77 | + if ( $mapped ) { |
| 78 | + $res[] = $mapped; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + $res = array_unique( $res ); |
| 83 | + } |
| 84 | + |
| 85 | + return $res; |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Given a HTTP Accept-Language header $header |
| 90 | + * returns all the locales in it. |
| 91 | + * |
| 92 | + * @param string $header HTTP acccept header. |
| 93 | + * @return array Matched locales. |
| 94 | + */ |
| 95 | +function get_http_locales( $header ) { |
| 96 | + $locale_part_re = '[a-z]{2,}'; |
| 97 | + $locale_re = "($locale_part_re(\-$locale_part_re)?)"; |
| 98 | + |
| 99 | + if ( preg_match_all( "/$locale_re/i", $header, $matches ) ) { |
| 100 | + return $matches[0]; |
| 101 | + } else { |
| 102 | + return []; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Tries to map a lang/region pair to one of our locales. |
| 108 | + * |
| 109 | + * @param string $lang Lang part of the HTTP accept header. |
| 110 | + * @param string $region Region part of the HTTP accept header. |
| 111 | + * @param array $available_locales List of available locales. |
| 112 | + * @return string|false Our locale matching $lang and $region, false otherwise. |
| 113 | + */ |
| 114 | +function map_locale( $lang, $region, $available_locales ) { |
| 115 | + $uregion = strtoupper( $region ); |
| 116 | + $ulang = strtoupper( $lang ); |
| 117 | + $variants = array( |
| 118 | + "$lang-$region", |
| 119 | + "{$lang}_$region", |
| 120 | + "$lang-$uregion", |
| 121 | + "{$lang}_$uregion", |
| 122 | + "{$lang}_$ulang", |
| 123 | + $lang, |
| 124 | + ); |
| 125 | + |
| 126 | + foreach ( $variants as $variant ) { |
| 127 | + if ( in_array( $variant, $available_locales ) ) { |
| 128 | + return $variant; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + foreach ( $available_locales as $locale ) { |
| 133 | + list( $locale_lang, ) = preg_split( '/[_-]/', $locale ); |
| 134 | + if ( $lang === $locale_lang ) { |
| 135 | + return $locale; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return false; |
| 140 | +} |
| 141 | + |
| 142 | +/** |
| 143 | + * Get the active language packs for a package. |
| 144 | + * |
| 145 | + * @param string $type Package type. One of "theme", "plugin". |
| 146 | + * @param string $slug Slug of the requested item (e.g., `jetpack`, `twentynineteen`). |
| 147 | + * |
| 148 | + * @return array |
| 149 | + */ |
| 150 | +function get_translated_locales( $type, $slug ) { |
| 151 | + global $wpdb; |
| 152 | + |
| 153 | + $language_packs = $wpdb->get_results( |
| 154 | + $wpdb->prepare( |
| 155 | + 'SELECT * |
| 156 | + FROM language_packs |
| 157 | + WHERE |
| 158 | + type = %s AND |
| 159 | + domain = %s AND |
| 160 | + active = 1 |
| 161 | + GROUP BY language', |
| 162 | + $type, |
| 163 | + $slug |
| 164 | + ) |
| 165 | + ); |
| 166 | + |
| 167 | + // Retrieve all the WordPress locales in which the theme is translated. |
| 168 | + $translated_locales = wp_list_pluck( $language_packs, 'language' ); |
| 169 | + |
| 170 | + require_once GLOTPRESS_LOCALES_PATH; |
| 171 | + |
| 172 | + // Validate the list of locales can be found by `wp_locale`. |
| 173 | + return array_filter( |
| 174 | + $translated_locales, |
| 175 | + function( $locale ) { |
| 176 | + return \GP_Locales::by_field( 'wp_locale', $locale ); |
| 177 | + } |
| 178 | + ); |
| 179 | +} |
0 commit comments