发布于 2015-08-27 16:36:06 | 311 次阅读 | 评论: 0 | 来源: 网络整理
A PHP replacement layer for the C intl extension that also provides access to the localization data of the ICU library.
2.3 新版功能: The Intl component was introduced in Symfony 2.3. In earlier versions of Symfony, you should use the Locale component instead.
警告
The replacement layer is limited to the locale “en”. If you want to use other locales, you should install the intl extension instead.
You can install the component in two different ways:
symfony/intl
on Packagist);If you install the component via Composer, the following classes and functions of the intl extension will be automatically provided if the intl extension is not loaded:
Collator
IntlDateFormatter
Locale
NumberFormatter
intl_error_name
intl_is_failure
intl_get_error_code
intl_get_error_message
When the intl extension is not available, the following classes are used to replace the intl classes:
Composer automatically exposes these classes in the global namespace.
If you don’t use Composer but the Symfony ClassLoader component, you need to expose them manually by adding the following lines to your autoload code:
if (!function_exists('intl_is_failure')) { require '/path/to/Icu/Resources/stubs/functions.php'; $loader->registerPrefixFallback('/path/to/Icu/Resources/stubs'); }
The ResourceBundle
class is not currently supported by this component. Instead, it includes a set of readers and writers for reading and writing arrays (or array-like objects) from/to resource bundle files. The following classes are supported:
Continue reading if you are interested in how to use these classes. Otherwise skip this section and jump to Accessing ICU Data.
The TextBundleWriter
writes an array or an array-like object to a plain-text resource bundle. The resulting .txt file can be converted to a binary .res file with the BundleCompiler
class:
use SymfonyComponentIntlResourceBundleWriterTextBundleWriter; use SymfonyComponentIntlResourceBundleCompilerBundleCompiler; $writer = new TextBundleWriter(); $writer->write('/path/to/bundle', 'en', array( 'Data' => array( 'entry1', 'entry2', // ... ), )); $compiler = new BundleCompiler(); $compiler->compile('/path/to/bundle', '/path/to/binary/bundle');
The command “genrb” must be available for the BundleCompiler
to work. If the command is located in a non-standard location, you can pass its path to the BundleCompiler
constructor.
The PhpBundleWriter
writes an array or an array-like object to a .php resource bundle:
use SymfonyComponentIntlResourceBundleWriterPhpBundleWriter; $writer = new PhpBundleWriter(); $writer->write('/path/to/bundle', 'en', array( 'Data' => array( 'entry1', 'entry2', // ... ), ));
The BinaryBundleReader
reads binary resource bundle files and returns an array or an array-like object. This class currently only works with the intl extension installed:
use SymfonyComponentIntlResourceBundleReaderBinaryBundleReader; $reader = new BinaryBundleReader(); $data = $reader->read('/path/to/bundle', 'en'); echo $data['Data']['entry1'];
The PhpBundleReader
reads resource bundles from .php files and returns an array or an array-like object:
use SymfonyComponentIntlResourceBundleReaderPhpBundleReader; $reader = new PhpBundleReader(); $data = $reader->read('/path/to/bundle', 'en'); echo $data['Data']['entry1'];
The BufferedBundleReader
wraps another reader, but keeps the last N reads in a buffer, where N is a buffer size passed to the constructor:
use SymfonyComponentIntlResourceBundleReaderBinaryBundleReader; use SymfonyComponentIntlResourceBundleReaderBufferedBundleReader; $reader = new BufferedBundleReader(new BinaryBundleReader(), 10); // actually reads the file $data = $reader->read('/path/to/bundle', 'en'); // returns data from the buffer $data = $reader->read('/path/to/bundle', 'en'); // actually reads the file $data = $reader->read('/path/to/bundle', 'fr');
The StructuredBundleReader
wraps another reader and offers a readEntry()
method for reading an entry of the resource bundle without having to worry whether array keys are set or not. If a path cannot be resolved, null
is returned:
use SymfonyComponentIntlResourceBundleReaderBinaryBundleReader; use SymfonyComponentIntlResourceBundleReaderStructuredBundleReader; $reader = new StructuredBundleReader(new BinaryBundleReader()); $data = $reader->read('/path/to/bundle', 'en'); // Produces an error if the key "Data" does not exist echo $data['Data']['entry1']; // Returns null if the key "Data" does not exist echo $reader->readEntry('/path/to/bundle', 'en', array('Data', 'entry1'));
Additionally, the readEntry()
method resolves fallback locales. For example, the fallback locale of “en_GB” is “en”. For single-valued entries (strings, numbers etc.), the entry will be read from the fallback locale if it cannot be found in the more specific locale. For multi-valued entries (arrays), the values of the more specific and the fallback locale will be merged. In order to suppress this behavior, the last parameter $fallback
can be set to false
:
echo $reader->readEntry( '/path/to/bundle', 'en', array('Data', 'entry1'), false );
The ICU data is located in several “resource bundles”. You can access a PHP wrapper of these bundles through the static Intl
class. At the moment, the following data is supported:
The translations of language and script names can be found in the language bundle:
use SymfonyComponentIntlIntl; Locale::setDefault('en'); $languages = Intl::getLanguageBundle()->getLanguageNames(); // => array('ab' => 'Abkhazian', ...) $language = Intl::getLanguageBundle()->getLanguageName('de'); // => 'German' $language = Intl::getLanguageBundle()->getLanguageName('de', 'AT'); // => 'Austrian German' $scripts = Intl::getLanguageBundle()->getScriptNames(); // => array('Arab' => 'Arabic', ...) $script = Intl::getLanguageBundle()->getScriptName('Hans'); // => 'Simplified'
All methods accept the translation locale as the last, optional parameter, which defaults to the current default locale:
$languages = Intl::getLanguageBundle()->getLanguageNames('de'); // => array('ab' => 'Abchasisch', ...)
The translations of country names can be found in the region bundle:
use SymfonyComponentIntlIntl; Locale::setDefault('en'); $countries = Intl::getRegionBundle()->getCountryNames(); // => array('AF' => 'Afghanistan', ...) $country = Intl::getRegionBundle()->getCountryName('GB'); // => 'United Kingdom'
All methods accept the translation locale as the last, optional parameter, which defaults to the current default locale:
$countries = Intl::getRegionBundle()->getCountryNames('de'); // => array('AF' => 'Afghanistan', ...)
The translations of locale names can be found in the locale bundle:
use SymfonyComponentIntlIntl; Locale::setDefault('en'); $locales = Intl::getLocaleBundle()->getLocaleNames(); // => array('af' => 'Afrikaans', ...) $locale = Intl::getLocaleBundle()->getLocaleName('zh_Hans_MO'); // => 'Chinese (Simplified, Macau SAR China)'
All methods accept the translation locale as the last, optional parameter, which defaults to the current default locale:
$locales = Intl::getLocaleBundle()->getLocaleNames('de'); // => array('af' => 'Afrikaans', ...)
The translations of currency names and other currency-related information can be found in the currency bundle:
use SymfonyComponentIntlIntl; Locale::setDefault('en'); $currencies = Intl::getCurrencyBundle()->getCurrencyNames(); // => array('AFN' => 'Afghan Afghani', ...) $currency = Intl::getCurrencyBundle()->getCurrencyName('INR'); // => 'Indian Rupee' $symbol = Intl::getCurrencyBundle()->getCurrencySymbol('INR'); // => '₹' $fractionDigits = Intl::getCurrencyBundle()->getFractionDigits('INR'); // => 2 $roundingIncrement = Intl::getCurrencyBundle()->getRoundingIncrement('INR'); // => 0
All methods (except for getFractionDigits()
and getRoundingIncrement()
) accept the translation locale as the last, optional parameter, which defaults to the current default locale:
$currencies = Intl::getCurrencyBundle()->getCurrencyNames('de'); // => array('AFN' => 'Afghanische Afghani', ...)
That’s all you need to know for now. Have fun coding!