2013-03-01 17:49:04 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\Intl ;
2014-09-15 12:39:24 +01:00
use Symfony\Component\Intl\Data\Bundle\Reader\BufferedBundleReader ;
use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReader ;
use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReaderInterface ;
2018-07-26 10:03:18 +01:00
use Symfony\Component\Intl\Data\Bundle\Reader\JsonBundleReader ;
2018-03-07 23:32:04 +00:00
use Symfony\Component\Intl\Data\Provider\LocaleDataProvider ;
2014-09-15 12:39:24 +01:00
use Symfony\Component\Intl\Data\Provider\ScriptDataProvider ;
use Symfony\Component\Intl\ResourceBundle\CurrencyBundle ;
use Symfony\Component\Intl\ResourceBundle\CurrencyBundleInterface ;
use Symfony\Component\Intl\ResourceBundle\LanguageBundle ;
use Symfony\Component\Intl\ResourceBundle\LanguageBundleInterface ;
use Symfony\Component\Intl\ResourceBundle\LocaleBundle ;
use Symfony\Component\Intl\ResourceBundle\LocaleBundleInterface ;
use Symfony\Component\Intl\ResourceBundle\RegionBundle ;
use Symfony\Component\Intl\ResourceBundle\RegionBundleInterface ;
2013-03-01 17:49:04 +00:00
/**
* Gives access to internationalization data .
*
* @ author Bernhard Schussek < bschussek @ gmail . com >
*/
2014-09-15 12:39:24 +01:00
final class Intl
2013-03-01 17:49:04 +00:00
{
/**
* The number of resource bundles to buffer . Loading the same resource
* bundle for n locales takes up n spots in the buffer .
*/
const BUFFER_SIZE = 10 ;
2014-09-15 12:39:24 +01:00
/**
* The directory name of the currency data .
*/
const CURRENCY_DIR = 'currencies' ;
/**
* The directory name of the language data .
*/
const LANGUAGE_DIR = 'languages' ;
/**
* The directory name of the script data .
*/
const SCRIPT_DIR = 'scripts' ;
/**
* The directory name of the locale data .
*/
const LOCALE_DIR = 'locales' ;
/**
* The directory name of the region data .
*/
const REGION_DIR = 'regions' ;
2013-03-01 17:49:04 +00:00
/**
* @ var ResourceBundle\CurrencyBundleInterface
*/
private static $currencyBundle ;
/**
* @ var ResourceBundle\LanguageBundleInterface
*/
private static $languageBundle ;
/**
* @ var ResourceBundle\LocaleBundleInterface
*/
private static $localeBundle ;
/**
* @ var ResourceBundle\RegionBundleInterface
*/
private static $regionBundle ;
/**
2014-04-16 11:30:19 +01:00
* @ var string | bool | null
2013-03-01 17:49:04 +00:00
*/
private static $icuVersion = false ;
/**
* @ var string
*/
private static $icuDataVersion = false ;
/**
2014-09-15 12:39:24 +01:00
* @ var BundleEntryReaderInterface
2013-03-01 17:49:04 +00:00
*/
2014-09-15 12:39:24 +01:00
private static $entryReader ;
2013-03-01 17:49:04 +00:00
/**
* Returns whether the intl extension is installed .
*
2016-06-28 06:50:50 +01:00
* @ return bool Returns true if the intl extension is installed , false otherwise
2013-03-01 17:49:04 +00:00
*/
public static function isExtensionLoaded ()
{
2013-04-04 17:26:59 +01:00
return class_exists ( '\ResourceBundle' );
2013-03-01 17:49:04 +00:00
}
/**
* Returns the bundle containing currency information .
*
2016-06-28 06:50:50 +01:00
* @ return CurrencyBundleInterface The currency resource bundle
2018-10-12 20:48:56 +01:00
*
* @ deprecated since Symfony 4.3 , to be removed in 5.0 . Use { @ see Currencies } instead .
2013-03-01 17:49:04 +00:00
*/
public static function getCurrencyBundle ()
{
2018-10-12 20:48:56 +01:00
@ trigger_error ( sprintf ( 'The method "%s()" is deprecated since Symfony 4.3, use "%s" instead.' , __METHOD__ , Currencies :: class ), E_USER_DEPRECATED );
2013-03-01 17:49:04 +00:00
if ( null === self :: $currencyBundle ) {
2014-09-15 12:39:24 +01:00
self :: $currencyBundle = new CurrencyBundle (
2015-03-31 00:38:19 +01:00
self :: getDataDirectory () . '/' . self :: CURRENCY_DIR ,
2014-09-15 12:39:24 +01:00
self :: getEntryReader (),
2018-10-12 20:48:56 +01:00
self :: $localeBundle ? ? self :: $localeBundle = new LocaleBundle ( self :: getDataDirectory () . '/' . self :: LOCALE_DIR , self :: getEntryReader ())
2014-09-15 12:39:24 +01:00
);
2013-03-01 17:49:04 +00:00
}
return self :: $currencyBundle ;
}
/**
* Returns the bundle containing language information .
*
2016-06-28 06:50:50 +01:00
* @ return LanguageBundleInterface The language resource bundle
2018-10-12 20:48:56 +01:00
*
* @ deprecated since Symfony 4.3 , to be removed in 5.0 . Use { @ see Languages } or { @ see Scripts } instead .
2013-03-01 17:49:04 +00:00
*/
public static function getLanguageBundle ()
{
2018-10-12 20:48:56 +01:00
@ trigger_error ( sprintf ( 'The method "%s()" is deprecated since Symfony 4.3, use "%s" or "%s" instead.' , __METHOD__ , Languages :: class , Scripts :: class ), E_USER_DEPRECATED );
2013-03-01 17:49:04 +00:00
if ( null === self :: $languageBundle ) {
2014-09-15 12:39:24 +01:00
self :: $languageBundle = new LanguageBundle (
2015-03-31 00:38:19 +01:00
self :: getDataDirectory () . '/' . self :: LANGUAGE_DIR ,
2014-09-15 12:39:24 +01:00
self :: getEntryReader (),
2018-10-12 20:48:56 +01:00
self :: $localeBundle ? ? self :: $localeBundle = new LocaleBundle ( self :: getDataDirectory () . '/' . self :: LOCALE_DIR , self :: getEntryReader ()),
2014-09-15 12:39:24 +01:00
new ScriptDataProvider (
2015-03-31 00:38:19 +01:00
self :: getDataDirectory () . '/' . self :: SCRIPT_DIR ,
2014-09-15 12:39:24 +01:00
self :: getEntryReader ()
)
);
2013-03-01 17:49:04 +00:00
}
return self :: $languageBundle ;
}
/**
* Returns the bundle containing locale information .
*
2016-06-28 06:50:50 +01:00
* @ return LocaleBundleInterface The locale resource bundle
2018-10-12 20:48:56 +01:00
*
* @ deprecated since Symfony 4.3 , to be removed in 5.0 . Use { @ see Locales } instead .
2013-03-01 17:49:04 +00:00
*/
public static function getLocaleBundle ()
{
2018-10-12 20:48:56 +01:00
@ trigger_error ( sprintf ( 'The method "%s()" is deprecated since Symfony 4.3, use "%s" instead.' , __METHOD__ , Locales :: class ), E_USER_DEPRECATED );
2013-03-01 17:49:04 +00:00
if ( null === self :: $localeBundle ) {
2014-09-15 12:39:24 +01:00
self :: $localeBundle = new LocaleBundle (
2015-03-31 00:38:19 +01:00
self :: getDataDirectory () . '/' . self :: LOCALE_DIR ,
2014-09-15 12:39:24 +01:00
self :: getEntryReader ()
);
2013-03-01 17:49:04 +00:00
}
return self :: $localeBundle ;
}
/**
* Returns the bundle containing region information .
*
2016-06-28 06:50:50 +01:00
* @ return RegionBundleInterface The region resource bundle
2018-10-12 20:48:56 +01:00
*
* @ deprecated since Symfony 4.3 , to be removed in 5.0 . Use { @ see Regions } instead .
2013-03-01 17:49:04 +00:00
*/
public static function getRegionBundle ()
{
2018-10-12 20:48:56 +01:00
@ trigger_error ( sprintf ( 'The method "%s()" is deprecated since Symfony 4.3, use "%s" instead.' , __METHOD__ , Regions :: class ), E_USER_DEPRECATED );
2013-03-01 17:49:04 +00:00
if ( null === self :: $regionBundle ) {
2014-09-15 12:39:24 +01:00
self :: $regionBundle = new RegionBundle (
2015-03-31 00:38:19 +01:00
self :: getDataDirectory () . '/' . self :: REGION_DIR ,
2014-09-15 12:39:24 +01:00
self :: getEntryReader (),
2018-10-12 20:48:56 +01:00
self :: $localeBundle ? ? self :: $localeBundle = new LocaleBundle ( self :: getDataDirectory () . '/' . self :: LOCALE_DIR , self :: getEntryReader ())
2014-09-15 12:39:24 +01:00
);
2013-03-01 17:49:04 +00:00
}
return self :: $regionBundle ;
}
/**
* Returns the version of the installed ICU library .
*
2018-10-02 04:12:00 +01:00
* @ return string | null The ICU version or NULL if it could not be determined
2013-03-01 17:49:04 +00:00
*/
public static function getIcuVersion ()
{
if ( false === self :: $icuVersion ) {
2013-03-15 12:39:37 +00:00
if ( ! self :: isExtensionLoaded ()) {
self :: $icuVersion = self :: getIcuStubVersion ();
2018-07-05 12:24:53 +01:00
} elseif ( \defined ( 'INTL_ICU_VERSION' )) {
2013-03-01 17:49:04 +00:00
self :: $icuVersion = INTL_ICU_VERSION ;
} else {
try {
$reflector = new \ReflectionExtension ( 'intl' );
ob_start ();
$reflector -> info ();
$output = strip_tags ( ob_get_clean ());
preg_match ( '/^ICU version (?:=>)?(.*)$/m' , $output , $matches );
self :: $icuVersion = trim ( $matches [ 1 ]);
} catch ( \ReflectionException $e ) {
self :: $icuVersion = null ;
}
}
}
return self :: $icuVersion ;
}
/**
* Returns the version of the installed ICU data .
*
2016-06-28 06:50:50 +01:00
* @ return string The version of the installed ICU data
2013-03-01 17:49:04 +00:00
*/
public static function getIcuDataVersion ()
{
if ( false === self :: $icuDataVersion ) {
2014-09-15 12:39:24 +01:00
self :: $icuDataVersion = trim ( file_get_contents ( self :: getDataDirectory () . '/version.txt' ));
2013-03-01 17:49:04 +00:00
}
return self :: $icuDataVersion ;
}
/**
* Returns the ICU version that the stub classes mimic .
*
2016-06-28 06:50:50 +01:00
* @ return string The ICU version of the stub classes
2013-03-01 17:49:04 +00:00
*/
2013-03-15 11:55:01 +00:00
public static function getIcuStubVersion ()
2013-03-01 17:49:04 +00:00
{
2019-03-30 00:13:06 +00:00
return '64.1' ;
2013-03-01 17:49:04 +00:00
}
/**
2014-09-15 12:39:24 +01:00
* Returns the absolute path to the data directory .
2013-03-01 17:49:04 +00:00
*
2014-09-15 12:39:24 +01:00
* @ return string The absolute path to the data directory
2013-03-01 17:49:04 +00:00
*/
2014-09-15 12:39:24 +01:00
public static function getDataDirectory ()
2013-03-01 17:49:04 +00:00
{
2016-10-25 09:15:38 +01:00
return __DIR__ . '/Resources/data' ;
2014-09-15 12:39:24 +01:00
}
/**
* Returns the cached bundle entry reader .
*
* @ return BundleEntryReaderInterface The bundle entry reader
*/
private static function getEntryReader ()
{
if ( null === self :: $entryReader ) {
self :: $entryReader = new BundleEntryReader ( new BufferedBundleReader (
new JsonBundleReader (),
2013-03-01 17:49:04 +00:00
self :: BUFFER_SIZE
));
2018-03-07 23:32:04 +00:00
$localeDataProvider = new LocaleDataProvider (
self :: getDataDirectory () . '/' . self :: LOCALE_DIR ,
self :: $entryReader
);
self :: $entryReader -> setLocaleAliases ( $localeDataProvider -> getAliases ());
2013-03-01 17:49:04 +00:00
}
2014-09-15 12:39:24 +01:00
return self :: $entryReader ;
}
2013-03-01 17:49:04 +00:00
/**
* This class must not be instantiated .
*/
2014-09-21 19:53:12 +01:00
private function __construct ()
{
}
2013-03-01 17:49:04 +00:00
}