vendor/symfony/src/Symfony/Component/Locale/Locale.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     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 
       
    12 namespace Symfony\Component\Locale;
       
    13 
       
    14 class Locale extends \Locale
       
    15 {
       
    16     /**
       
    17      * Caches the countries in different locales
       
    18      * @var array
       
    19      */
       
    20     protected static $countries = array();
       
    21 
       
    22     /**
       
    23      * Caches the languages in different locales
       
    24      * @var array
       
    25      */
       
    26     protected static $languages = array();
       
    27 
       
    28     /**
       
    29      * Caches the different locales
       
    30      * @var array
       
    31      */
       
    32     protected static $locales = array();
       
    33 
       
    34     /**
       
    35      * Returns the country names for a locale
       
    36      *
       
    37      * @param  string $locale     The locale to use for the country names
       
    38      * @return array              The country names with their codes as keys
       
    39      * @throws RuntimeException   When the resource bundles cannot be loaded
       
    40      */
       
    41     static public function getDisplayCountries($locale)
       
    42     {
       
    43         if (!isset(self::$countries[$locale])) {
       
    44             $bundle = \ResourceBundle::create($locale, __DIR__.'/Resources/data/region');
       
    45 
       
    46             if (null === $bundle) {
       
    47                 throw new \RuntimeException('The country resource bundle could not be loaded');
       
    48             }
       
    49 
       
    50             $collator = new \Collator($locale);
       
    51             $countries = array();
       
    52 
       
    53             foreach ($bundle->get('Countries') as $code => $name) {
       
    54                 // Global countries (f.i. "America") have numeric codes
       
    55                 // Countries have alphabetic codes
       
    56                 // "ZZ" is the code for unknown country
       
    57                 if (ctype_alpha($code) && 'ZZ' !== $code) {
       
    58                     $countries[$code] = $name;
       
    59                 }
       
    60             }
       
    61 
       
    62             $collator->asort($countries);
       
    63 
       
    64             self::$countries[$locale] = $countries;
       
    65         }
       
    66 
       
    67         return self::$countries[$locale];
       
    68     }
       
    69 
       
    70     /**
       
    71      * Returns all available country codes
       
    72      *
       
    73      * @return array              The country codes
       
    74      * @throws RuntimeException   When the resource bundles cannot be loaded
       
    75      */
       
    76     static public function getCountries()
       
    77     {
       
    78         return array_keys(self::getDisplayCountries(self::getDefault()));
       
    79     }
       
    80 
       
    81     /**
       
    82      * Returns the language names for a locale
       
    83      *
       
    84      * @param  string $locale     The locale to use for the language names
       
    85      * @return array              The language names with their codes as keys
       
    86      * @throws RuntimeException   When the resource bundles cannot be loaded
       
    87      */
       
    88     static public function getDisplayLanguages($locale)
       
    89     {
       
    90         if (!isset(self::$languages[$locale])) {
       
    91             $bundle = \ResourceBundle::create($locale, __DIR__.'/Resources/data/lang');
       
    92 
       
    93             if (null === $bundle) {
       
    94                 throw new \RuntimeException('The language resource bundle could not be loaded');
       
    95             }
       
    96 
       
    97             $collator = new \Collator($locale);
       
    98             $languages = array();
       
    99 
       
   100             foreach ($bundle->get('Languages') as $code => $name) {
       
   101                 // "mul" is the code for multiple languages
       
   102                 if ('mul' !== $code) {
       
   103                     $languages[$code] = $name;
       
   104                 }
       
   105             }
       
   106 
       
   107             $collator->asort($languages);
       
   108 
       
   109             self::$languages[$locale] = $languages;
       
   110         }
       
   111 
       
   112         return self::$languages[$locale];
       
   113     }
       
   114 
       
   115     /**
       
   116      * Returns all available language codes
       
   117      *
       
   118      * @return array              The language codes
       
   119      * @throws RuntimeException   When the resource bundles cannot be loaded
       
   120      */
       
   121     static public function getLanguages()
       
   122     {
       
   123         return array_keys(self::getDisplayLanguages(self::getDefault()));
       
   124     }
       
   125 
       
   126     /**
       
   127      * Returns the locale names for a locale
       
   128      *
       
   129      * @param  string $locale     The locale to use for the locale names
       
   130      * @return array              The locale names with their codes as keys
       
   131      * @throws RuntimeException   When the resource bundles cannot be loaded
       
   132      */
       
   133     static public function getDisplayLocales($locale)
       
   134     {
       
   135         if (!isset(self::$locales[$locale])) {
       
   136             $bundle = \ResourceBundle::create($locale, __DIR__.'/Resources/data/names');
       
   137 
       
   138             if (null === $bundle) {
       
   139                 throw new \RuntimeException('The locale resource bundle could not be loaded');
       
   140             }
       
   141 
       
   142             $collator = new \Collator($locale);
       
   143             $locales = array();
       
   144 
       
   145             foreach ($bundle->get('Locales') as $code => $name) {
       
   146                 $locales[$code] = $name;
       
   147             }
       
   148 
       
   149             $collator->asort($locales);
       
   150 
       
   151             self::$locales[$locale] = $locales;
       
   152         }
       
   153 
       
   154         return self::$locales[$locale];
       
   155     }
       
   156 
       
   157     /**
       
   158      * Returns all available locale codes
       
   159      *
       
   160      * @return array              The locale codes
       
   161      * @throws RuntimeException   When the resource bundles cannot be loaded
       
   162      */
       
   163     static public function getLocales()
       
   164     {
       
   165         return array_keys(self::getDisplayLocales(self::getDefault()));
       
   166     }
       
   167 }