[Intl] Made the $locale parameter optional in the bundle interfaces

This commit is contained in:
Bernhard Schussek 2013-03-15 16:15:53 +01:00
parent b9e9cb27b9
commit 2cd1be8df3
14 changed files with 114 additions and 59 deletions

View File

@ -300,7 +300,7 @@ class StubNumberFormatter
return $this->format($value);
}
$symbol = Intl::getCurrencyBundle()->getCurrencySymbol('en', $currency);
$symbol = Intl::getCurrencyBundle()->getCurrencySymbol($currency, 'en');
$fractionDigits = Intl::getCurrencyBundle()->getFractionDigits($currency);
$value = $this->roundCurrency($value, $currency);

View File

@ -55,13 +55,13 @@ Numbers can be formatted with the [`\NumberFormatter`] [3] class. The following
methods are supported. All other methods are not supported and will throw an
exception when used.
##### __construct($locale = 'en', $style = null, $pattern = null)
##### __construct($locale = $style = null, $pattern = null)
The only supported locale is "en". The supported styles are
`\NumberFormatter::DECIMAL` and `\NumberFormatter::CURRENCY`. The argument
`$pattern` may not be used.
##### ::create($locale = 'en', $style = null, $pattern = null)
##### ::create($locale = $style = null, $pattern = null)
See `__construct()`.
@ -115,19 +115,19 @@ Languages and Scripts
The translations of language and script names can be found in the language
bundle.
$languages = Intl::getLanguageBundle()->getLanguageNames('en');
$languages = Intl::getLanguageBundle()->getLanguageNames();
// => array('ab' => 'Abkhazian', ...)
$language = Intl::getLanguageBundle()->getLanguageName('en', 'de');
$language = Intl::getLanguageBundle()->getLanguageName('de');
// => 'German'
$language = Intl::getLanguageBundle()->getLanguageName('en', 'de', 'AT);
$language = Intl::getLanguageBundle()->getLanguageName('de', 'AT);
// => 'Austrian German'
$scripts = Intl::getLanguageBundle()->getScriptNames('en');
$scripts = Intl::getLanguageBundle()->getScriptNames();
// => array('Arab' => 'Arabic', ...)
$script = Intl::getLanguageBundle()->getScriptName('en', 'Hans');
$script = Intl::getLanguageBundle()->getScriptName('Hans');
// => 'Simplified'
Countries
@ -135,10 +135,10 @@ Countries
The translations of country names can be found in the region bundle.
$countries = Intl::getRegionBundle()->getCountryNames('en');
$countries = Intl::getRegionBundle()->getCountryNames();
// => array('AF' => 'Afghanistan', ...)
$country = Intl::getRegionBundle()->getCountryName('en', 'GB');
$country = Intl::getRegionBundle()->getCountryName('GB');
// => 'United Kingdom'
Locales
@ -146,10 +146,10 @@ Locales
The translations of locale names can be found in the locale bundle.
$locales = Intl::getLocaleBundle()->getLocaleNames('en');
$locales = Intl::getLocaleBundle()->getLocaleNames();
// => array('af' => 'Afrikaans', ...)
$locale = Intl::getLocaleBundle()->getLocaleName('en', 'zh_Hans_MO');
$locale = Intl::getLocaleBundle()->getLocaleName('zh_Hans_MO');
// => 'Chinese (Simplified, Macau SAR China)'
Currencies
@ -158,13 +158,13 @@ Currencies
The translations of currency names and other currency-related information can
be found in the currency bundle.
$currencies = Intl::getCurrencyBundle()->getCurrencyNames('en');
$currencies = Intl::getCurrencyBundle()->getCurrencyNames();
// => array('AFN' => 'Afghan Afghani', ...)
$currency = Intl::getCurrencyBundle()->getCurrencyNames('en', 'INR');
$currency = Intl::getCurrencyBundle()->getCurrencyNames('INR');
// => 'Indian Rupee'
$symbol = Intl::getCurrencyBundle()->getCurrencyNames('en', 'INR');
$symbol = Intl::getCurrencyBundle()->getCurrencyNames('INR');
// => '₹'
$fractionDigits = Intl::getCurrencyBundle()->getFractionDigits('INR');

View File

@ -29,24 +29,36 @@ class CurrencyBundle extends AbstractBundle implements CurrencyBundleInterface
/**
* {@inheritdoc}
*/
public function getCurrencySymbol($locale, $currency)
public function getCurrencySymbol($currency, $locale = null)
{
if (null === $locale) {
$locale = \Locale::getDefault();
}
return $this->readEntry($locale, array('Currencies', $currency, static::INDEX_SYMBOL));
}
/**
* {@inheritdoc}
*/
public function getCurrencyName($locale, $currency)
public function getCurrencyName($currency, $locale = null)
{
if (null === $locale) {
$locale = \Locale::getDefault();
}
return $this->readEntry($locale, array('Currencies', $currency, static::INDEX_NAME));
}
/**
* {@inheritdoc}
*/
public function getCurrencyNames($locale)
public function getCurrencyNames($locale = null)
{
if (null === $locale) {
$locale = \Locale::getDefault();
}
if (null === ($currencies = $this->readEntry($locale, array('Currencies')))) {
return array();
}

View File

@ -21,31 +21,34 @@ interface CurrencyBundleInterface extends ResourceBundleInterface
/**
* Returns the symbol used for a currency.
*
* @param string $locale The locale to return the result in.
* @param string $currency A currency code (e.g. "EUR").
* @param string $locale Optional. The locale to return the result in.
* Defaults to {@link \Locale::getLocale()}.
*
* @return string|null The currency symbol or NULL if not found.
*/
public function getCurrencySymbol($locale, $currency);
public function getCurrencySymbol($currency, $locale = null);
/**
* Returns the name of a currency.
*
* @param string $locale The locale to return the name in.
* @param string $currency A currency code (e.g. "EUR").
* @param string $locale Optional. The locale to return the name in.
* Defaults to {@link \Locale::getLocale()}.
*
* @return string|null The name of the currency or NULL if not found.
*/
public function getCurrencyName($locale, $currency);
public function getCurrencyName($currency, $locale = null);
/**
* Returns the names of all known currencies.
*
* @param string $locale The locale to return the names in.
* @param string $locale Optional. The locale to return the names in.
* Defaults to {@link \Locale::getLocale()}.
*
* @return string[] A list of currency names indexed by currency codes.
*/
public function getCurrencyNames($locale);
public function getCurrencyNames($locale = null);
/**
* Returns the number of digits after the comma of a currency.

View File

@ -21,8 +21,12 @@ class LanguageBundle extends AbstractBundle implements LanguageBundleInterface
/**
* {@inheritdoc}
*/
public function getLanguageName($locale, $lang, $region = null)
public function getLanguageName($lang, $region = null, $locale = null)
{
if (null === $locale) {
$locale = \Locale::getDefault();
}
if (null === ($languages = $this->readEntry($locale, array('Languages')))) {
return array();
}
@ -39,8 +43,12 @@ class LanguageBundle extends AbstractBundle implements LanguageBundleInterface
/**
* {@inheritdoc}
*/
public function getLanguageNames($locale)
public function getLanguageNames($locale = null)
{
if (null === $locale) {
$locale = \Locale::getDefault();
}
if (null === ($languages = $this->readEntry($locale, array('Languages')))) {
return array();
}
@ -55,8 +63,12 @@ class LanguageBundle extends AbstractBundle implements LanguageBundleInterface
/**
* {@inheritdoc}
*/
public function getScriptName($locale, $script, $lang = null)
public function getScriptName($script, $lang = null, $locale = null)
{
if (null === $locale) {
$locale = \Locale::getDefault();
}
$data = $this->read($locale);
// Some languages are translated together with their script,
@ -84,8 +96,12 @@ class LanguageBundle extends AbstractBundle implements LanguageBundleInterface
/**
* {@inheritdoc}
*/
public function getScriptNames($locale)
public function getScriptNames($locale = null)
{
if (null === $locale) {
$locale = \Locale::getDefault();
}
if (null === ($scripts = $this->readEntry($locale, array('Scripts')))) {
return array();
}

View File

@ -21,40 +21,44 @@ interface LanguageBundleInterface extends ResourceBundleInterface
/**
* Returns the name of a language.
*
* @param string $locale The locale to return the name in.
* @param string $lang A language code (e.g. "en").
* @param string|null $region Optional. A region code (e.g. "US").
* @param string $locale Optional. The locale to return the name in.
* Defaults to {@link \Locale::getLocale()}.
*
* @return string|null The name of the language or NULL if not found.
*/
public function getLanguageName($locale, $lang, $region = null);
public function getLanguageName($lang, $region = null, $locale = null);
/**
* Returns the names of all known languages.
*
* @param string $locale The locale to return the names in.
* @param string $locale Optional. The locale to return the names in.
* Defaults to {@link \Locale::getLocale()}.
*
* @return string[] A list of language names indexed by language codes.
*/
public function getLanguageNames($locale);
public function getLanguageNames($locale = null);
/**
* Returns the name of a script.
*
* @param string $locale The locale to return the name in.
* @param string $script A script code (e.g. "Hans").
* @param string $lang Optional. A language code (e.g. "zh").
* @param string $locale Optional. The locale to return the name in.
* Defaults to {@link \Locale::getLocale()}.
*
* @return string|null The name of the script or NULL if not found.
*/
public function getScriptName($locale, $script, $lang = null);
public function getScriptName($script, $lang = null, $locale = null);
/**
* Returns the names of all known scripts.
*
* @param string $locale The locale to return the names in.
* @param string $locale Optional. The locale to return the names in.
* Defaults to {@link \Locale::getLocale()}.
*
* @return string[] A list of script names indexed by script codes.
*/
public function getScriptNames($locale);
public function getScriptNames($locale = null);
}

View File

@ -21,16 +21,24 @@ class LocaleBundle extends AbstractBundle implements LocaleBundleInterface
/**
* {@inheritdoc}
*/
public function getLocaleName($locale, $ofLocale)
public function getLocaleName($ofLocale, $locale = null)
{
if (null === $locale) {
$locale = \Locale::getDefault();
}
return $this->readEntry($locale, array('Locales', $ofLocale));
}
/**
* {@inheritdoc}
*/
public function getLocaleNames($locale)
public function getLocaleNames($locale = null)
{
if (null === $locale) {
$locale = \Locale::getDefault();
}
if (null === ($locales = $this->readEntry($locale, array('Locales')))) {
return array();
}

View File

@ -21,19 +21,21 @@ interface LocaleBundleInterface extends ResourceBundleInterface
/**
* Returns the name of a locale.
*
* @param string $locale The locale to return the name in.
* @param string $ofLocale The locale to return the name of (e.g. "de_AT").
* @param string $locale Optional. The locale to return the name in.
* Defaults to {@link \Locale::getLocale()}.
*
* @return string|null The name of the locale or NULL if not found.
*/
public function getLocaleName($locale, $ofLocale);
public function getLocaleName($ofLocale, $locale = null);
/**
* Returns the names of all known locales.
*
* @param string $locale The locale to return the name in.
* @param string $locale Optional. The locale to return the names in.
* Defaults to {@link \Locale::getLocale()}.
*
* @return string[] A list of locale names indexed by locale codes.
*/
public function getLocaleNames($locale);
public function getLocaleNames($locale = null);
}

View File

@ -21,16 +21,24 @@ class RegionBundle extends AbstractBundle implements RegionBundleInterface
/**
* {@inheritdoc}
*/
public function getCountryName($locale, $country)
public function getCountryName($country, $locale = null)
{
if (null === $locale) {
$locale = \Locale::getDefault();
}
return $this->readEntry($locale, array('Countries', $country));
}
/**
* {@inheritdoc}
*/
public function getCountryNames($locale)
public function getCountryNames($locale = null)
{
if (null === $locale) {
$locale = \Locale::getDefault();
}
if (null === ($countries = $this->readEntry($locale, array('Countries')))) {
return array();
}

View File

@ -21,19 +21,21 @@ interface RegionBundleInterface extends ResourceBundleInterface
/**
* Returns the name of a country.
*
* @param string $locale The locale to return the name in.
* @param string $country A country code (e.g. "US").
* @param string $locale Optional. The locale to return the name in.
* Defaults to {@link \Locale::getLocale()}.
*
* @return string|null The name of the country or NULL if not found.
*/
public function getCountryName($locale, $country);
public function getCountryName($country, $locale = null);
/**
* Returns the names of all known countries.
*
* @param string $locale The locale to return the names in.
* @param string $locale Optional. The locale to return the names in.
* Defaults to {@link \Locale::getLocale()}.
*
* @return string[] A list of country names indexed by country codes.
*/
public function getCountryNames($locale);
public function getCountryNames($locale = null);
}

View File

@ -43,7 +43,7 @@ class CurrencyBundleTest extends \PHPUnit_Framework_TestCase
->with(self::RES_DIR, 'en', array('Currencies', 'EUR', 1))
->will($this->returnValue('€'));
$this->assertSame('€', $this->bundle->getCurrencySymbol('en', 'EUR'));
$this->assertSame('€', $this->bundle->getCurrencySymbol('EUR', 'en'));
}
public function testGetCurrencyName()
@ -53,7 +53,7 @@ class CurrencyBundleTest extends \PHPUnit_Framework_TestCase
->with(self::RES_DIR, 'en', array('Currencies', 'EUR', 0))
->will($this->returnValue('Euro'));
$this->assertSame('Euro', $this->bundle->getCurrencyName('en', 'EUR'));
$this->assertSame('Euro', $this->bundle->getCurrencyName('EUR', 'en'));
}
public function testGetCurrencyNames()

View File

@ -48,7 +48,7 @@ class LanguageBundleTest extends \PHPUnit_Framework_TestCase
->with(self::RES_DIR, 'en', array('Languages'))
->will($this->returnValue($languages));
$this->assertSame('German', $this->bundle->getLanguageName('en', 'de'));
$this->assertSame('German', $this->bundle->getLanguageName('de', 'en'));
}
public function testGetLanguageNameWithRegion()
@ -64,7 +64,7 @@ class LanguageBundleTest extends \PHPUnit_Framework_TestCase
->with(self::RES_DIR, 'en', array('Languages'))
->will($this->returnValue($languages));
$this->assertSame('British English', $this->bundle->getLanguageName('en', 'en', 'GB'));
$this->assertSame('British English', $this->bundle->getLanguageName('en', 'GB', 'en'));
}
public function testGetLanguageNameWithUntranslatedRegion()
@ -79,7 +79,7 @@ class LanguageBundleTest extends \PHPUnit_Framework_TestCase
->with(self::RES_DIR, 'en', array('Languages'))
->will($this->returnValue($languages));
$this->assertSame('English', $this->bundle->getLanguageName('en', 'en', 'US'));
$this->assertSame('English', $this->bundle->getLanguageName('en', 'US', 'en'));
}
public function testGetLanguageNames()
@ -115,7 +115,7 @@ class LanguageBundleTest extends \PHPUnit_Framework_TestCase
->with(self::RES_DIR, 'en')
->will($this->returnValue($data));
$this->assertSame('latin', $this->bundle->getScriptName('en', 'Latn'));
$this->assertSame('latin', $this->bundle->getScriptName('Latn', 'en'));
}
public function testGetScriptNameIncludedInLanguage()
@ -138,7 +138,7 @@ class LanguageBundleTest extends \PHPUnit_Framework_TestCase
->will($this->returnValue($data));
// Null because the script is included in the language anyway
$this->assertNull($this->bundle->getScriptName('en', 'Hans', 'zh'));
$this->assertNull($this->bundle->getScriptName('Hans', 'zh', 'en'));
}
public function testGetScriptNameIncludedInLanguageInBraces()
@ -160,7 +160,7 @@ class LanguageBundleTest extends \PHPUnit_Framework_TestCase
->with(self::RES_DIR, 'en')
->will($this->returnValue($data));
$this->assertSame('simplified', $this->bundle->getScriptName('en', 'Hans', 'zh'));
$this->assertSame('simplified', $this->bundle->getScriptName('Hans', 'zh', 'en'));
}
public function testGetScriptNameNoScriptsBlock()
@ -177,7 +177,7 @@ class LanguageBundleTest extends \PHPUnit_Framework_TestCase
->with(self::RES_DIR, 'en')
->will($this->returnValue($data));
$this->assertNull($this->bundle->getScriptName('en', 'Latn'));
$this->assertNull($this->bundle->getScriptName('Latn', 'en'));
}
public function testGetScriptNames()

View File

@ -44,7 +44,7 @@ class LocaleBundleTest extends \PHPUnit_Framework_TestCase
->will($this->returnValue('German (Austria)'));
$this->assertSame('German (Austria)', $this->bundle->getLocaleName('en', 'de_AT'));
$this->assertSame('German (Austria)', $this->bundle->getLocaleName('de_AT', 'en'));
}
public function testGetLocaleNames()

View File

@ -43,7 +43,7 @@ class RegionBundleTest extends \PHPUnit_Framework_TestCase
->with(self::RES_DIR, 'en', array('Countries', 'AT'))
->will($this->returnValue('Austria'));
$this->assertSame('Austria', $this->bundle->getCountryName('en', 'AT'));
$this->assertSame('Austria', $this->bundle->getCountryName('AT', 'en'));
}
public function testGetCountryNames()