4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Intl
;
14 use Symfony\Component\Icu\IcuCurrencyBundle
;
15 use Symfony\Component\Icu\IcuData
;
16 use Symfony\Component\Icu\IcuLanguageBundle
;
17 use Symfony\Component\Icu\IcuLocaleBundle
;
18 use Symfony\Component\Icu\IcuRegionBundle
;
19 use Symfony\Component\Intl\ResourceBundle\Reader\BufferedBundleReader
;
20 use Symfony\Component\Intl\ResourceBundle\Reader\StructuredBundleReader
;
23 * Gives access to internationalization data.
25 * @author Bernhard Schussek <bschussek@gmail.com>
30 * The number of resource bundles to buffer. Loading the same resource
31 * bundle for n locales takes up n spots in the buffer.
33 const BUFFER_SIZE
= 10;
36 * @var ResourceBundle\CurrencyBundleInterface
38 private static $currencyBundle;
41 * @var ResourceBundle\LanguageBundleInterface
43 private static $languageBundle;
46 * @var ResourceBundle\LocaleBundleInterface
48 private static $localeBundle;
51 * @var ResourceBundle\RegionBundleInterface
53 private static $regionBundle;
56 * @var string|Boolean|null
58 private static $icuVersion = false;
63 private static $icuDataVersion = false;
66 * @var ResourceBundle\Reader\StructuredBundleReaderInterface
68 private static $bundleReader;
71 * Returns whether the intl extension is installed.
73 * @return Boolean Returns true if the intl extension is installed, false otherwise.
75 public static function isExtensionLoaded()
77 return class_exists('\ResourceBundle');
81 * Returns the bundle containing currency information.
83 * @return ResourceBundle\CurrencyBundleInterface The currency resource bundle.
85 public static function getCurrencyBundle()
87 if (null === self
::$currencyBundle) {
88 self
::$currencyBundle = new IcuCurrencyBundle(self
::getBundleReader());
91 return self
::$currencyBundle;
95 * Returns the bundle containing language information.
97 * @return ResourceBundle\LanguageBundleInterface The language resource bundle.
99 public static function getLanguageBundle()
101 if (null === self
::$languageBundle) {
102 self
::$languageBundle = new IcuLanguageBundle(self
::getBundleReader());
105 return self
::$languageBundle;
109 * Returns the bundle containing locale information.
111 * @return ResourceBundle\LocaleBundleInterface The locale resource bundle.
113 public static function getLocaleBundle()
115 if (null === self
::$localeBundle) {
116 self
::$localeBundle = new IcuLocaleBundle(self
::getBundleReader());
119 return self
::$localeBundle;
123 * Returns the bundle containing region information.
125 * @return ResourceBundle\RegionBundleInterface The region resource bundle.
127 public static function getRegionBundle()
129 if (null === self
::$regionBundle) {
130 self
::$regionBundle = new IcuRegionBundle(self
::getBundleReader());
133 return self
::$regionBundle;
137 * Returns the version of the installed ICU library.
139 * @return null|string The ICU version or NULL if it could not be determined.
141 public static function getIcuVersion()
143 if (false === self
::$icuVersion) {
144 if (!self
::isExtensionLoaded()) {
145 self
::$icuVersion = self
::getIcuStubVersion();
146 } elseif (defined('INTL_ICU_VERSION')) {
147 self
::$icuVersion = INTL_ICU_VERSION
;
150 $reflector = new \
ReflectionExtension('intl');
153 $output = strip_tags(ob_get_clean());
154 preg_match('/^ICU version (?:=>)?(.*)$/m', $output, $matches);
156 self
::$icuVersion = trim($matches[1]);
157 } catch (\ReflectionException
$e) {
158 self
::$icuVersion = null;
163 return self
::$icuVersion;
167 * Returns the version of the installed ICU data.
169 * @return string The version of the installed ICU data.
171 public static function getIcuDataVersion()
173 if (false === self
::$icuDataVersion) {
174 self
::$icuDataVersion = IcuData
::getVersion();
177 return self
::$icuDataVersion;
181 * Returns the ICU version that the stub classes mimic.
183 * @return string The ICU version of the stub classes.
185 public static function getIcuStubVersion()
191 * Returns a resource bundle reader for .php resource bundle files.
193 * @return ResourceBundle\Reader\StructuredBundleReaderInterface The resource reader.
195 private static function getBundleReader()
197 if (null === self
::$bundleReader) {
198 self
::$bundleReader = new StructuredBundleReader(new BufferedBundleReader(
199 IcuData
::getBundleReader(),
204 return self
::$bundleReader;
208 * This class must not be instantiated.
210 private function __construct() {}