[Form] Implemented CountryField

This commit is contained in:
Bernhard Schussek 2010-12-15 17:44:36 +01:00 committed by Fabien Potencier
parent 78b69876d4
commit 9db7db4439
236 changed files with 132 additions and 0 deletions

View File

@ -0,0 +1,62 @@
<?php
namespace Symfony\Component\Form;
/**
* A field for selecting from a list of countries
*
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
*/
class CountryField extends ChoiceField
{
/**
* Caches the country choices in different locales
* @var array
*/
protected static $countries;
/**
* @inheritDoc
*/
protected function configure()
{
$this->addOption('choices', self::getCountryChoices($this->locale));
parent::configure();
}
/**
* Returns the list of countries for a locale
*
* @param string $locale The locale to use for the country names
* @throws RuntimeException When the resource bundles cannot be loaded
*/
protected static function getCountryChoices($locale)
{
if (!isset(self::$countries[$locale])) {
$bundle = new \ResourceBundle($locale, __DIR__.'/Resources/data/region');
if ($bundle === null) {
throw new RuntimeException('The region resource bundle could not be loaded');
}
$collator = new \Collator($locale);
$countries = array();
foreach ($bundle->get('Countries') as $code => $name) {
// Global regions (f.i. "America") have numeric codes
// Countries have alphabetic codes
// "ZZ" is the code for unknown region
if (ctype_alpha($code) && $code !== 'ZZ') {
$countries[$code] = $name;
}
}
$collator->asort($countries);
self::$countries[$locale] = $countries;
}
return self::$countries[$locale];
}
}

View File

@ -0,0 +1,34 @@
How to update the ICU data
==========================
1. Checkout the current version of the ICU data files
$ svn co http://source.icu-project.org/repos/icu/icu/trunk/source/data icu-data
2. Build the region resource bundles
$ cd icu-data/region
$ mkdir build
$ genrb -d build *.txt
3a. Replace the *.res files bundled with Symfony2 with the new ones.
.dat-package
------------
The individual *.res files can be combined into a single .dat-file.
Unfortunately, PHP's `ResourceBundle` class is currently not able to handle
.dat-files.
Once it is, the following steps have to be followed to build the .dat-file:
3b. Package the resource bundles into a single file
$ find . -name *.res | sed -e "s/\.\///g" > packagelist.txt
$ pkgdata -p region -T build -d . packagelist.txt
4. Clean up
$ rm -rf build packagelist.txt
5. You can now move region.dat to replace the version bundled with Symfony2.

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