8000 [WIP] added back ICU classes from symfony/Icu by fabpot · Pull Request #11884 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[WIP] added back ICU classes from symfony/Icu #11884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
added back ICU classes from symfony/Icu
  • Loading branch information
fabpot committed Sep 9, 2014
commit 37b867221835f813f99c9a9493343b49ddc3c133
117 changes: 117 additions & 0 deletions src/Symfony/Component/Intl/Icu/IcuCurrencyBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?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\Icu;

use Symfony\Component\Intl\ResourceBundle\CurrencyBundle;
use Symfony\Component\Intl\ResourceBundle\Reader\StructuredBundleReaderInterface;

/**
* An ICU-specific implementation of {@link \Symfony\Component\Intl\ResourceBundle\CurrencyBundleInterface}.
*
* This class normalizes the data of the ICU .res files to satisfy the contract
* defined in {@link \Symfony\Component\Intl\ResourceBundle\CurrencyBundleInterface}.
*
* When no ICU resource files are available, this class acts as a stub implementation of
* {@link \Symfony\Component\Intl\ResourceBundle\CurrencyBundleInterface}, only for the
* English language.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class IcuCurrencyBundle extends CurrencyBundle
{
const INDEX_SYMBOL = 0;
const INDEX_NAME = 1;
const INDEX_FRACTION_DIGITS = 0;
const INDEX_ROUNDING_INCREMENT = 1;

private $stubbed;

public function __construct(StructuredBundleReaderInterface $reader)
{
$this->stubbed = IcuData::isStubbed();

parent::__construct(realpath(IcuData::getResourceDirectory().'/curr'), $reader);
}

/**
* {@inheritdoc}
*/
public function getLocales()
{
return $this->stubbed ? array('en') : $this->readEntry('misc', array('Locales'));
}

/**
* {@inheritdoc}
*/
public function getCurrencyNames($locale = null)
{
$names = parent::getCurrencyNames($locale);

if ($this->stubbed) {
return $names;
}

if (null === $locale) {
$locale = \Locale::getDefault();
}

$collator = new \Collator($locale);
$collator->asort($names);

return $names;
}

/**
* {@inheritdoc}
*/
public function getFractionDigits($currency)
{
if ($this->stubbed) {
return parent::getFractionDigits($currency);
}

$entry = $this->readEntry('misc', array('CurrencyMeta'));

if (!isset($entry[$currency][self::INDEX_FRACTION_DIGITS])) {
// The 'DEFAULT' key contains the fraction digits and the rounding
// increment that are common for a lot of currencies.
// Only currencies with different values are added to the icu-data
// (e.g: CHF and JPY)
return $entry['DEFAULT'][self::INDEX_FRACTION_DIGITS];
}

return $entry[$currency][self::INDEX_FRACTION_DIGITS];
}

/**
* {@inheritdoc}
*/
public function getRoundingIncrement($currency)
{
if ($this->stubbed) {
return parent::getRoundingIncrement($currency);
}

$entry = $this->readEntry('misc', array('CurrencyMeta'));

if (!isset($entry[$currency][self::INDEX_ROUNDING_INCREMENT])) {
// The 'DEFAULT' key contains the fraction digits and the rounding
// increment that are common for a lot of currencies.
// Only currencies with different values are added to the icu-data
// (e.g: CHF and JPY)
return $entry['DEFAULT'][self::INDEX_ROUNDING_INCREMENT];
}

return $entry[$currency][self::INDEX_ROUNDING_INCREMENT];
}
}
68 changes: 68 additions & 0 deletions src/Symfony/Component/Intl/Icu/IcuData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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\Icu;

use Symfony\Component\Intl\ResourceBundle\Reader\PhpBundleReader;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class IcuData
{
/**
* Returns the version of the bundled ICU data.
*
* @return string The version string.
*/
public static function getVersion()
{
return trim(file_get_contents(__DIR__.'/../Resources/data/version.txt'));
}

/**
* Returns whether the ICU data is stubbed.
*
* @return Boolean Returns true if the ICU data is stubbed, false if it is
* loaded from ICU .res files.
*/
public static function isStubbed()
{
return true;
}

/**
* Returns the path to the directory where the resource bundles are stored.
*
* @return string The absolute path to the resource directory.
*/
public static function getResourceDirectory()
{
return realpath(__DIR__.'/../Resources/data');
}

/**
* Returns a reader for reading resource bundles in this component.
*
* @return \Symfony\Component\Intl\ResourceBundle\Reader\BundleReaderInterface
*/
public static function getBundleReader()
{
return new PhpBundleReader();
}

/**
* This class must not be instantiated.
*/
private function __construct()
{
}
}
100 changes: 100 additions & 0 deletions src/Symfony/Component/Intl/Icu/IcuLanguageBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?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\Icu;

use Symfony\Component\Intl\ResourceBundle\LanguageBundle;
use Symfony\Component\Intl\ResourceBundle\Reader\StructuredBundleReaderInterface;

/**
* An ICU-specific implementation of {@link \Symfony\Component\Intl\ResourceBundle\LanguageBundleInterface}.
*
* This class normalizes the data of the ICU .res files to satisfy the contract
* defined in {@link \Symfony\Component\Intl\ResourceBundle\LanguageBundleInterface}.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class IcuLanguageBundle extends LanguageBundle
{
private $stubbed;

public function __construct(StructuredBundleReaderInterface $reader)
{
$this->stubbed = IcuData::isStubbed();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is useless. IcuData::isStubbed() will always be true now, given there is a single version of the class

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah no, I missed the class_alias hack replacing the class defined here by the IntlData one. But I find this quite weird

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's weird. As we are going to remove the intl dep on IntlData, I will change this strategy.


parent::__construct(realpath(IcuData::getResourceDirectory().'/lang'), $reader);
}

/**
* {@inheritdoc}
*/
public function getLocales()
{
return $this->stubbed ? array('en') : $this->readEntry('misc', array('Locales'));
}

/**
* {@inheritdoc}
*/
public function getLanguageName($lang, $region = null, $locale = null)
{
if ('mul' === $lang) {
return null;
}

return parent::getLanguageName($lang, $region, $locale);
}

/**
* {@inheritdoc}
*/
public function getLanguageNames($locale = null)
{
$languages = parent::getLanguageNames($locale);

if ($this->stubbed) {
return $languages;
}

if (null === $locale) {
$locale = \Locale::getDefault();
}

$collator = new \Collator($locale);
$collator->asort($languages);

// "mul" is the code for multiple languages
unset($languages['mul']);

return $languages;
}

/**
* {@inheritdoc}
*/
public function getScriptNames($locale = null)
{
if ($this->stubbed) {
return parent::getScriptNames($locale);
}

if (null === $locale) {
$locale = \Locale::getDefault();
}

$scripts = parent::getScriptNames($locale);

$collator = new \Collator($locale);
$collator->asort($scripts);

return $scripts;
}
}
64 changes: 64 additions & 0 deletions src/Symfony/Component/Intl/Icu/IcuLocaleBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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\Icu;

use Symfony\Component\Intl\ResourceBundle\LocaleBundle;
use Symfony\Component\Intl\ResourceBundle\Reader\StructuredBundleReaderInterface;

/**
* An ICU-specific implementation of {@link \Symfony\Component\Intl\ResourceBundle\LocaleBundleInterface}.
*
* This class normalizes the data of the ICU .res files to satisfy the contract
* defined in {@link \Symfony\Component\Intl\ResourceBundle\LocaleBundleInterface}.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class IcuLocaleBundle extends LocaleBundle
{
private $stubbed;

public function __construct(StructuredBundleReaderInterface 8E4B $reader)
{
$this->stubbed = IcuData::isStubbed();

parent::__construct(realpath(IcuData::getResourceDirectory().'/locales'), $reader);
}

/**
* {@inheritdoc}
*/
public function getLocales()
{
return $this->stubbed ? array('en') : $this->readEntry('misc', array('Locales'));
}

/**
* {@inheritdoc}
*/
public function getLocaleNames($locale = null)
{
$locales = parent::getLocaleNames($locale);

if ($this->stubbed) {
return $locales;
}

if (null === $locale) {
$locale = \Locale::getDefault();
}

$collator = new \Collator($locale);
$collator->asort($locales);

return $locales;
}
}
Loading
0