[Form][Locale] Implemented LocaleField and added script for updating ICU data

This commit is contained in:
Bernhard Schussek 2011-01-02 00:24:51 +01:00 committed by Fabien Potencier
parent d8b8ae0608
commit b9c2e98315
585 changed files with 421 additions and 6 deletions

View File

@ -0,0 +1,23 @@
<?php
namespace Symfony\Component\Form;
use Symfony\Component\Locale\Locale;
/**
* A field for selecting from a list of locales
*
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
*/
class LocaleField extends ChoiceField
{
/**
* @inheritDoc
*/
protected function configure()
{
$this->addOption('choices', Locale::getDisplayLocales($this->locale));
parent::configure();
}
}

View File

@ -25,6 +25,12 @@ class Locale extends \Locale
*/
protected static $languages = array();
/**
* Caches the different locales
* @var array
*/
protected static $locales = array();
/**
* Returns the country names for a locale
*
@ -116,4 +122,46 @@ class Locale extends \Locale
{
return array_keys(self::getDisplayLanguages(self::getDefault()));
}
/**
* Returns the locale names for a locale
*
* @param string $locale The locale to use for the locale names
* @return array The locale names with their codes as keys
* @throws RuntimeException When the resource bundles cannot be loaded
*/
public static function getDisplayLocales($locale)
{
if (!isset(self::$locales[$locale])) {
$bundle = new \ResourceBundle($locale, __DIR__.'/Resources/data/names');
if (null === $bundle) {
throw new \RuntimeException('The locale resource bundle could not be loaded');
}
$collator = new \Collator($locale);
$locales = array();
foreach ($bundle->get('Locales') as $code => $name) {
$locales[$code] = $name;
}
$collator->asort($locales);
self::$locales[$locale] = $locales;
}
return self::$locales[$locale];
}
/**
* Returns all available locale codes
*
* @return array The locale codes
* @throws RuntimeException When the resource bundles cannot be loaded
*/
public static function getLocales()
{
return array_keys(self::getDisplayLocales(self::getDefault()));
}
}

View File

@ -5,13 +5,9 @@ How to update the ICU data
$ svn co http://source.icu-project.org/repos/icu/icu/trunk/source/data icu-data
2. Build the region resource bundles
2. Execute the build script
$ cd icu-data/region
$ mkdir build
$ genrb -d build *.txt
3a. Replace the *.res files bundled with Symfony2 with the new ones.
$ php update-data.php /path/to/icu-data
.dat-package
------------

Some files were not shown because too many files have changed in this diff Show More