Skip to content

Commit

Permalink
[Intl] Add TimezoneBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
ro0NL committed Oct 12, 2018
1 parent 91f6e69 commit ec63856
Show file tree
Hide file tree
Showing 9 changed files with 1,940 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Symfony/Component/Intl/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Resources/data/zones/*.json
!Resources/data/zones/de.json
!Resources/data/zones/en.json
!Resources/data/zones/fr.json
!Resources/data/zones/meta.json
!Resources/data/zones/nl.json
vendor/
composer.lock
phpunit.xml
173 changes: 173 additions & 0 deletions src/Symfony/Component/Intl/Data/Generator/ZoneDataGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?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\Data\Generator;

use Symfony\Component\Intl\Data\Bundle\Compiler\GenrbCompiler;
use Symfony\Component\Intl\Data\Bundle\Reader\BundleReaderInterface;
use Symfony\Component\Intl\Data\Util\ArrayAccessibleResourceBundle;
use Symfony\Component\Intl\Data\Util\LocaleScanner;

/**
* The rule for compiling the zone bundle.
*
* @author Roland Franssen <franssen.roland@gmail.com>
*
* @internal
*/
class ZoneDataGenerator extends AbstractDataGenerator
{
private const UNKNOWN_ZONE = 'Etc:Unknown';

/**
* Collects all available zone codes.
*
* @var string[]
*/
private $zoneCodes = array();

/**
* {@inheritdoc}
*/
protected function scanLocales(LocaleScanner $scanner, $sourceDir)
{
return $scanner->scanLocales($sourceDir.'/zone');
}

/**
* {@inheritdoc}
*/
protected function compileTemporaryBundles(GenrbCompiler $compiler, $sourceDir, $tempDir)
{
$compiler->compile($sourceDir.'/zone', $tempDir);
$compiler->compile($sourceDir.'/misc/timezoneTypes.txt', $tempDir);
$compiler->compile($sourceDir.'/misc/metaZones.txt', $tempDir);
}

/**
* {@inheritdoc}
*/
protected function preGenerate()
{
$this->zoneCodes = array();
}

/**
* {@inheritdoc}
*/
protected function generateDataForLocale(BundleReaderInterface $reader, $tempDir, $displayLocale)
{
$localeBundle = $reader->read($tempDir, $displayLocale);

if (isset($localeBundle['zoneStrings']) && null !== $localeBundle['zoneStrings']) {
$data = array(
'Version' => $localeBundle['Version'],
'Names' => self::generateZones(
$reader->read($tempDir, 'timezoneTypes'),
$reader->read($tempDir, 'metaZones'),
$localeBundle
),
);

$this->zoneCodes = array_merge($this->zoneCodes, array_keys($data['Names']));

return $data;
}
}

/**
* {@inheritdoc}
*/
protected function generateDataForRoot(BundleReaderInterface $reader, $tempDir)
{
$rootBundle = $reader->read($tempDir, 'root');

return array(
'Version' => $rootBundle['Version'],
'Names' => self::generateZones(
$reader->read($tempDir, 'timezoneTypes'),
$reader->read($tempDir, 'metaZones'),
$rootBundle
),
);
}

/**
* {@inheritdoc}
*/
protected function generateDataForMeta(BundleReaderInterface $reader, $tempDir)
{
$rootBundle = $reader->read($tempDir, 'root');

$this->zoneCodes = array_unique($this->zoneCodes);

sort($this->zoneCodes);

$data = array(
'Version' => $rootBundle['Version'],
'Zones' => $this->zoneCodes,
);

return $data;
}

private static function generateZones(ArrayAccessibleResourceBundle $typeBundle, ArrayAccessibleResourceBundle $metaBundle, ArrayAccessibleResourceBundle $localeBundle): array
{
$available = array();
foreach ($typeBundle['typeMap']['timezone'] as $zone => $_) {
if (self::UNKNOWN_ZONE === $zone) {
continue;
}

$available[self::normalizeZone($zone)] = true;
}

$metazones = array();
foreach ($metaBundle['metazoneInfo'] as $zone => $info) {
$zone = self::normalizeZone($zone);
foreach ($info as $metazone) {
$metazones[$zone] = $metazone->get(0);
}
}

$zones = array();

foreach ($localeBundle['zoneStrings'] as $zone => $data) {
if (!isset($available[$zone = self::normalizeZone($zone)])) {
continue;
}

$name = null;
if (isset($metazones[$zone]) && isset($localeBundle['zoneStrings']['meta:'.$metazones[$zone]])) {
$metadata = $localeBundle['zoneStrings']['meta:'.$metazones[$zone]];
$name = $metadata->get('ls') ?? $metadata->get('lg') ?? $metadata->get('ld');
}

$name = $data->get('ls') ?? $data->get('lg') ?? $data->get('ld') ?? $name;
if (null !== $city = $data->get('ec')) {
if (null === $name) {
$name = str_replace('{0}', $city, $localeBundle['zoneStrings']['regionFormat']);
} else {
$name .= ' ('.$city.')';
}
}

$zones[$zone] = $name ?? $metazones[$zone] ?? $zone;
}

return $zones;
}

private static function normalizeZone(string $zone): string
{
return str_replace(':', '/', $zone);
}
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/Intl/Intl.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ final class Intl
*/
const REGION_DIR = 'regions';

/**
* The directory name of the zone data.
*/
const ZONE_DIR = 'zones';

/**
* @var ResourceBundle\CurrencyBundleInterface
*/
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/Intl/Resources/bin/update-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Intl\Data\Generator\LocaleDataGenerator;
use Symfony\Component\Intl\Data\Generator\RegionDataGenerator;
use Symfony\Component\Intl\Data\Generator\ScriptDataGenerator;
use Symfony\Component\Intl\Data\Generator\ZoneDataGenerator;
use Symfony\Component\Intl\Data\Provider\LanguageDataProvider;
use Symfony\Component\Intl\Data\Provider\RegionDataProvider;
use Symfony\Component\Intl\Data\Provider\ScriptDataProvider;
Expand Down Expand Up @@ -195,6 +196,7 @@
$targetDir.'/'.Intl::LOCALE_DIR,
$targetDir.'/'.Intl::REGION_DIR,
$targetDir.'/'.Intl::SCRIPT_DIR,
$targetDir.'/'.Intl::ZONE_DIR,
));
}

Expand Down Expand Up @@ -253,6 +255,15 @@
//echo "Compiling...\n";
//
//$compiler->compile($txtDir.'/'.Intl::LOCALE_DIR, $resDir.'/'.Intl::LOCALE_DIR);

echo "Generating zone data...\n";

$generator = new ZoneDataGenerator($compiler, Intl::ZONE_DIR);
$generator->generateData($config);

//echo "Compiling...\n";
//
//$compiler->compile($txtDir.'/'.Intl::CURRENCY_DIR, $resDir.'/'.Intl::CURRENCY_DIR);
//
//$filesystem->remove($txtDir);

Expand Down
Loading

0 comments on commit ec63856

Please sign in to comment.