Recaptcha_Map.php
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | /** |
| 5 | * Class Tribe__Languages__Recaptcha_Map |
| 6 | * |
| 7 | * Converts WordPress format language codes to language codes supported by Recaptcha. |
| 8 | */ |
| 9 | class Tribe__Languages__Recaptcha_Map implements Tribe__Languages__Map_Interface { |
| 10 | |
| 11 | /** |
| 12 | * Gets all the languages supported by this language map. |
| 13 | * |
| 14 | * @return array An associative array in the format |
| 15 | * [ <slug> => <name> ] |
| 16 | * e.g. [ 'pt-BR' => 'Portuguese (Brazil)' ] |
| 17 | */ |
| 18 | public function get_supported_languages() { |
| 19 | return [ |
| 20 | 'ar' => 'Arabic', |
| 21 | 'af' => 'Afrikaans', |
| 22 | 'am' => 'Amharic', |
| 23 | 'hy' => 'Armenian', |
| 24 | 'az' => 'Azerbaijani', |
| 25 | 'eu' => 'Basque', |
| 26 | 'bn' => 'Bengali', |
| 27 | 'bg' => 'Bulgarian', |
| 28 | 'ca' => 'Catalan', |
| 29 | 'zh-HK' => 'Chinese (Hong Kong)', |
| 30 | 'zh-CN' => 'Chinese (Simplified)', |
| 31 | 'zh-TW' => 'Chinese (Traditional)', |
| 32 | 'hr' => 'Croatian', |
| 33 | 'cs' => 'Czech', |
| 34 | 'da' => 'Danish', |
| 35 | 'nl' => 'Dutch', |
| 36 | 'en-GB' => 'English (UK)', |
| 37 | 'en' => 'English (US)', |
| 38 | 'et' => 'Estonian', |
| 39 | 'fil' => 'Filipino', |
| 40 | 'fi' => 'Finnish', |
| 41 | 'fr' => 'French', |
| 42 | 'fr-CA' => 'French (Canadian)', |
| 43 | 'gl' => 'Galician', |
| 44 | 'ka' => 'Georgian', |
| 45 | 'de' => 'German', |
| 46 | 'de-AT' => 'German (Austria)', |
| 47 | 'de-CH' => 'German (Switzerland)', |
| 48 | 'el' => 'Greek', |
| 49 | 'gu' => 'Gujarati', |
| 50 | 'iw' => 'Hebrew', |
| 51 | 'hi' => 'Hindi', |
| 52 | 'hu' => 'Hungarain', |
| 53 | 'is' => 'Icelandic', |
| 54 | 'id' => 'Indonesian', |
| 55 | 'it' => 'Italian', |
| 56 | 'ja' => 'Japanese', |
| 57 | 'kn' => 'Kannada', |
| 58 | 'ko' => 'Korean', |
| 59 | 'lo' => 'Laothian', |
| 60 | 'lv' => 'Latvian', |
| 61 | 'lt' => 'Lithuanian', |
| 62 | 'ms' => 'Malay', |
| 63 | 'ml' => 'Malayalam', |
| 64 | 'mr' => 'Marathi', |
| 65 | 'mn' => 'Mongolian', |
| 66 | 'no' => 'Norwegian', |
| 67 | 'fa' => 'Persian', |
| 68 | 'pl' => 'Polish', |
| 69 | 'pt' => 'Portuguese', |
| 70 | 'pt-BR' => 'Portuguese (Brazil)', |
| 71 | 'pt-PT' => 'Portuguese (Portugal)', |
| 72 | 'ro' => 'Romanian', |
| 73 | 'ru' => 'Russian', |
| 74 | 'sr' => 'Serbian', |
| 75 | 'si' => 'Sinhalese', |
| 76 | 'sk' => 'Slovak', |
| 77 | 'sl' => 'Slovenian', |
| 78 | 'es' => 'Spanish', |
| 79 | 'es-419' => 'Spanish (Latin America)', |
| 80 | 'sw' => 'Swahili', |
| 81 | 'sv' => 'Swedish', |
| 82 | 'ta' => 'Tamil', |
| 83 | 'te' => 'Telugu', |
| 84 | 'th' => 'Thai', |
| 85 | 'tr' => 'Turkish', |
| 86 | 'uk' => 'Ukrainian', |
| 87 | 'ur' => 'Urdu', |
| 88 | 'vi' => 'Vietnamese', |
| 89 | 'zu' => 'Zulu', |
| 90 | ]; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Checks whether a language code is supported by the language map or not. |
| 95 | * |
| 96 | * @param string $language_code |
| 97 | * |
| 98 | * @return bool Whether a language code is supported by the language map or not. |
| 99 | */ |
| 100 | public function is_supported( $language_code ) { |
| 101 | return (bool) $this->convert_language_code( $language_code ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Converts a language code from the format used by WP to the one used by the language map. |
| 106 | * |
| 107 | * @param string $language_code A language code in the format used by WP; e.g. `en_US`. |
| 108 | * |
| 109 | * @return string|false The converted language code or `false` if the language code is not supported. |
| 110 | */ |
| 111 | public function convert_language_code( $language_code ) { |
| 112 | $converted_code = $language_code; |
| 113 | if ( strlen( $language_code ) > 2 ) { |
| 114 | // go from `en_US` to `en-US` |
| 115 | $converted_code = str_replace( '_', '-', $language_code ); |
| 116 | $exists = array_key_exists( $converted_code, $this->get_supported_languages() ); |
| 117 | // try with just the two first chars |
| 118 | $converted_code = $exists ? $converted_code : $this->convert_language_code( substr( $language_code, 0, 2 ) ); |
| 119 | } else { |
| 120 | $converted_code = array_key_exists( $language_code, $this->get_supported_languages() ) ? $converted_code : false; |
| 121 | } |
| 122 | |
| 123 | return $converted_code ? $converted_code : false; |
| 124 | } |
| 125 | } |
| 126 |