1<?php 2 3/* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Symfony\Component\Intl\Locale; 13 14use Symfony\Component\Intl\Exception\MethodNotImplementedException; 15 16/** 17 * Replacement for PHP's native {@link \Locale} class. 18 * 19 * The only methods supported in this class are `getDefault` and `canonicalize`. 20 * All other methods will throw an exception when used. 21 * 22 * @author Eriksen Costa <eriksen.costa@infranology.com.br> 23 * @author Bernhard Schussek <bschussek@gmail.com> 24 * 25 * @internal 26 */ 27abstract class Locale 28{ 29 const DEFAULT_LOCALE = null; 30 31 /* Locale method constants */ 32 const ACTUAL_LOCALE = 0; 33 const VALID_LOCALE = 1; 34 35 /* Language tags constants */ 36 const LANG_TAG = 'language'; 37 const EXTLANG_TAG = 'extlang'; 38 const SCRIPT_TAG = 'script'; 39 const REGION_TAG = 'region'; 40 const VARIANT_TAG = 'variant'; 41 const GRANDFATHERED_LANG_TAG = 'grandfathered'; 42 const PRIVATE_TAG = 'private'; 43 44 /** 45 * Not supported. Returns the best available locale based on HTTP "Accept-Language" header according to RFC 2616. 46 * 47 * @param string $header The string containing the "Accept-Language" header value 48 * 49 * @return string The corresponding locale code 50 * 51 * @see https://php.net/locale.acceptfromhttp 52 * 53 * @throws MethodNotImplementedException 54 */ 55 public static function acceptFromHttp(string $header) 56 { 57 throw new MethodNotImplementedException(__METHOD__); 58 } 59 60 /** 61 * Returns a canonicalized locale string. 62 * 63 * This polyfill doesn't implement the full-spec algorithm. It only 64 * canonicalizes locale strings handled by the `LocaleBundle` class. 65 * 66 * @return string 67 */ 68 public static function canonicalize(string $locale) 69 { 70 if ('' === $locale || '.' === $locale[0]) { 71 return self::getDefault(); 72 } 73 74 if (!preg_match('/^([a-z]{2})[-_]([a-z]{2})(?:([a-z]{2})(?:[-_]([a-z]{2}))?)?(?:\..*)?$/i', $locale, $m)) { 75 return $locale; 76 } 77 78 if (!empty($m[4])) { 79 return strtolower($m[1]).'_'.ucfirst(strtolower($m[2].$m[3])).'_'.strtoupper($m[4]); 80 } 81 82 if (!empty($m[3])) { 83 return strtolower($m[1]).'_'.ucfirst(strtolower($m[2].$m[3])); 84 } 85 86 return strtolower($m[1]).'_'.strtoupper($m[2]); 87 } 88 89 /** 90 * Not supported. Returns a correctly ordered and delimited locale code. 91 * 92 * @param array $subtags A keyed array where the keys identify the particular locale code subtag 93 * 94 * @return string The corresponding locale code 95 * 96 * @see https://php.net/locale.composelocale 97 * 98 * @throws MethodNotImplementedException 99 */ 100 public static function composeLocale(array $subtags) 101 { 102 throw new MethodNotImplementedException(__METHOD__); 103 } 104 105 /** 106 * Not supported. Checks if a language tag filter matches with locale. 107 * 108 * @param string $langtag The language tag to check 109 * @param string $locale The language range to check against 110 * 111 * @return string The corresponding locale code 112 * 113 * @see https://php.net/locale.filtermatches 114 * 115 * @throws MethodNotImplementedException 116 */ 117 public static function filterMatches(string $langtag, string $locale, bool $canonicalize = false) 118 { 119 throw new MethodNotImplementedException(__METHOD__); 120 } 121 122 /** 123 * Not supported. Returns the variants for the input locale. 124 * 125 * @param string $locale The locale to extract the variants from 126 * 127 * @return array The locale variants 128 * 129 * @see https://php.net/locale.getallvariants 130 * 131 * @throws MethodNotImplementedException 132 */ 133 public static function getAllVariants(string $locale) 134 { 135 throw new MethodNotImplementedException(__METHOD__); 136 } 137 138 /** 139 * Returns the default locale. 140 * 141 * @return string The default locale code. Always returns 'en' 142 * 143 * @see https://php.net/locale.getdefault 144 */ 145 public static function getDefault() 146 { 147 return 'en'; 148 } 149 150 /** 151 * Not supported. Returns the localized display name for the locale language. 152 * 153 * @param string $locale The locale code to return the display language from 154 * @param string $inLocale Optional format locale code to use to display the language name 155 * 156 * @return string The localized language display name 157 * 158 * @see https://php.net/locale.getdisplaylanguage 159 * 160 * @throws MethodNotImplementedException 161 */ 162 public static function getDisplayLanguage(string $locale, string $inLocale = null) 163 { 164 throw new MethodNotImplementedException(__METHOD__); 165 } 166 167 /** 168 * Not supported. Returns the localized display name for the locale. 169 * 170 * @param string $locale The locale code to return the display locale name from 171 * @param string $inLocale Optional format locale code to use to display the locale name 172 * 173 * @return string The localized locale display name 174 * 175 * @see https://php.net/locale.getdisplayname 176 * 177 * @throws MethodNotImplementedException 178 */ 179 public static function getDisplayName(string $locale, string $inLocale = null) 180 { 181 throw new MethodNotImplementedException(__METHOD__); 182 } 183 184 /** 185 * Not supported. Returns the localized display name for the locale region. 186 * 187 * @param string $locale The locale code to return the display region from 188 * @param string $inLocale Optional format locale code to use to display the region name 189 * 190 * @return string The localized region display name 191 * 192 * @see https://php.net/locale.getdisplayregion 193 * 194 * @throws MethodNotImplementedException 195 */ 196 public static function getDisplayRegion(string $locale, string $inLocale = null) 197 { 198 throw new MethodNotImplementedException(__METHOD__); 199 } 200 201 /** 202 * Not supported. Returns the localized display name for the locale script. 203 * 204 * @param string $locale The locale code to return the display script from 205 * @param string $inLocale Optional format locale code to use to display the script name 206 * 207 * @return string The localized script display name 208 * 209 * @see https://php.net/locale.getdisplayscript 210 * 211 * @throws MethodNotImplementedException 212 */ 213 public static function getDisplayScript(string $locale, string $inLocale = null) 214 { 215 throw new MethodNotImplementedException(__METHOD__); 216 } 217 218 /** 219 * Not supported. Returns the localized display name for the locale variant. 220 * 221 * @param string $locale The locale code to return the display variant from 222 * @param string $inLocale Optional format locale code to use to display the variant name 223 * 224 * @return string The localized variant display name 225 * 226 * @see https://php.net/locale.getdisplayvariant 227 * 228 * @throws MethodNotImplementedException 229 */ 230 public static function getDisplayVariant(string $locale, string $inLocale = null) 231 { 232 throw new MethodNotImplementedException(__METHOD__); 233 } 234 235 /** 236 * Not supported. Returns the keywords for the locale. 237 * 238 * @param string $locale The locale code to extract the keywords from 239 * 240 * @return array Associative array with the extracted variants 241 * 242 * @see https://php.net/locale.getkeywords 243 * 244 * @throws MethodNotImplementedException 245 */ 246 public static function getKeywords(string $locale) 247 { 248 throw new MethodNotImplementedException(__METHOD__); 249 } 250 251 /** 252 * Not supported. Returns the primary language for the locale. 253 * 254 * @param string $locale The locale code to extract the language code from 255 * 256 * @return string|null The extracted language code or null in case of error 257 * 258 * @see https://php.net/locale.getprimarylanguage 259 * 260 * @throws MethodNotImplementedException 261 */ 262 public static function getPrimaryLanguage(string $locale) 263 { 264 throw new MethodNotImplementedException(__METHOD__); 265 } 266 267 /** 268 * Not supported. Returns the region for the locale. 269 * 270 * @param string $locale The locale code to extract the region code from 271 * 272 * @return string|null The extracted region code or null if not present 273 * 274 * @see https://php.net/locale.getregion 275 * 276 * @throws MethodNotImplementedException 277 */ 278 public static function getRegion(string $locale) 279 { 280 throw new MethodNotImplementedException(__METHOD__); 281 } 282 283 /** 284 * Not supported. Returns the script for the locale. 285 * 286 * @param string $locale The locale code to extract the script code from 287 * 288 * @return string|null The extracted script code or null if not present 289 * 290 * @see https://php.net/locale.getscript 291 * 292 * @throws MethodNotImplementedException 293 */ 294 public static function getScript(string $locale) 295 { 296 throw new MethodNotImplementedException(__METHOD__); 297 } 298 299 /** 300 * Not supported. Returns the closest language tag for the locale. 301 * 302 * @param array $langtag A list of the language tags to compare to locale 303 * @param string $locale The locale to use as the language range when matching 304 * @param bool $canonicalize If true, the arguments will be converted to canonical form before matching 305 * @param string $default The locale to use if no match is found 306 * 307 * @see https://php.net/locale.lookup 308 * 309 * @throws MethodNotImplementedException 310 */ 311 public static function lookup(array $langtag, string $locale, bool $canonicalize = false, string $default = null) 312 { 313 throw new MethodNotImplementedException(__METHOD__); 314 } 315 316 /** 317 * Not supported. Returns an associative array of locale identifier subtags. 318 * 319 * @param string $locale The locale code to extract the subtag array from 320 * 321 * @return array Associative array with the extracted subtags 322 * 323 * @see https://php.net/locale.parselocale 324 * 325 * @throws MethodNotImplementedException 326 */ 327 public static function parseLocale(string $locale) 328 { 329 throw new MethodNotImplementedException(__METHOD__); 330 } 331 332 /** 333 * Not supported. Sets the default runtime locale. 334 * 335 * @return bool true on success or false on failure 336 * 337 * @see https://php.net/locale.setdefault 338 * 339 * @throws MethodNotImplementedException 340 */ 341 public static function setDefault(string $locale) 342 { 343 if ('en' !== $locale) { 344 throw new MethodNotImplementedException(__METHOD__); 345 } 346 347 return true; 348 } 349} 350