Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e42e029

Browse filesBrowse files
committed
[Intl] Provide translated timezone names
1 parent b0fffac commit e42e029
Copy full SHA for e42e029

File tree

Expand file treeCollapse file tree

184 files changed

+37872
-0
lines changed
Filter options

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree

184 files changed

+37872
-0
lines changed
+163Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Intl\Data\Generator;
13+
14+
use Symfony\Component\Intl\Data\Bundle\Reader\BundleReaderInterface;
15+
use Symfony\Component\Intl\Data\Util\ArrayAccessibleResourceBundle;
16+
use Symfony\Component\Intl\Data\Bundle\Compiler\GenrbCompiler;
17+
use Symfony\Component\Intl\Data\Util\LocaleScanner;
18+
19+
/**
20+
* The rule for compiling the currency bundle.
21+
*
22+
* @internal
23+
*/
24+
class TimezoneDataGenerator extends AbstractDataGenerator
25+
{
26+
/**
27+
* Collects all available timezones.
28+
*
29+
* @var string[]
30+
*/
31+
private $timezones = array();
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
protected function scanLocales(LocaleScanner $scanner, $sourceDir)
37+
{
38+
return $scanner->scanLocales($sourceDir.'/zone');
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
protected function compileTemporaryBundles(GenrbCompiler $compiler, $sourceDir, $tempDir)
45+
{
46+
$compiler->compile($sourceDir.'/zone', $tempDir);
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
protected function preGenerate()
53+
{
54+
$this->timezones = array();
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
protected function generateDataForLocale(BundleReaderInterface $reader, $tempDir, $displayLocale)
61+
{
62+
$localeBundle = $reader->read($tempDir, $displayLocale);
63+
64+
if (isset($localeBundle['zoneStrings']) && null !== $localeBundle['zoneStrings']) {
65+
$data = array(
66+
'Version' => $localeBundle['Version'],
67+
'Names' => $this->generateTimezoneNames($localeBundle),
68+
);
69+
70+
$this->timezones = array_merge($this->timezones, array_keys($data['Names']));
71+
72+
return $data;
73+
}
74+
75+
return;
76+
}
77+
78+
/**
79+
* {@inheritdoc}
80+
*/
81+
protected function generateDataForRoot(BundleReaderInterface $reader, $tempDir)
82+
{
83+
$rootBundle = $reader->read($tempDir, 'root');
84+
85+
$names = $this->generateTimezoneNames($rootBundle);
86+
87+
foreach ($this->timezones as $timezone) {
88+
if (!isset($names[$timezone])) {
89+
$names[$timezone] = $this->getFallbackName($timezone);
90+
}
91+
}
92+
93+
ksort($names);
94+
95+
return array(
96+
'Version' => $rootBundle['Version'],
97+
'Names' => $names,
98+
);
99+
}
100+
101+
/**
102+
* {@inheritdoc}
103+
*/
104+
protected function generateDataForMeta(BundleReaderInterface $reader, $tempDir)
105+
{
106+
$rootBundle = $reader->read($tempDir, 'root');
107+
108+
$this->timezones = array_unique($this->timezones);
109+
110+
sort($this->timezones);
111+
112+
$data = array(
113+
'Version' => $rootBundle['Version'],
114+
'Timezones' => $this->timezones,
115+
);
116+
117+
return $data;
118+
}
119+
120+
/**
121+
* @param ArrayAccessibleResourceBundle $rootBundle
122+
*
123+
* @return array
124+
*/
125+
private function generateTimezoneNames(ArrayAccessibleResourceBundle $rootBundle)
126+
{
127+
$timezoneNames = array();
128+
foreach ($rootBundle['zoneStrings'] as $key => $zone) {
129+
if (strpos($key, ':') !== false && substr($key, 5) != 'meta:') {
130+
$identifier = str_replace(':', '/', $key);
131+
132+
$zone = iterator_to_array($zone);
133+
134+
if (isset($zone['ec'])) {
135+
$timezoneNames[$identifier] = $zone['ec'];
136+
}
137+
}
138+
}
139+
140+
return $timezoneNames;
141+
}
142+
143+
/**
144+
* Converts a timezone identifier to an English string.
145+
*
146+
* @param string $timezone
147+
*
148+
* @return string
149+
*/
150+
private function getFallbackName($timezone)
151+
{
152+
$parts = explode('/', $timezone);
153+
if (count($parts) > 2) {
154+
$name = $parts[2].', '.$parts[1];
155+
} elseif (count($parts) > 1) {
156+
$name = $parts[1];
157+
} else {
158+
$name = $parts[0];
159+
}
160+
161+
return str_replace('_', ' ', $name);
162+
}
163+
}
+80Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Intl\Data\Provider;
13+
14+
use Symfony\Component\Intl\Locale;
15+
use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReaderInterface;
16+
17+
/**
18+
* Data provider for timezone-related data.
19+
*
20+
* @internal
21+
*/
22+
class TimezoneDataProvider
23+
{
24+
/**
25+
* @var string
26+
*/
27+
private $path;
28+
29+
/**
30+
* @var BundleEntryReaderInterface
31+
*/
32+
private $reader;
33+
34+
/**
35+
* Creates a data provider that reads timezone-related data from a
36+
* resource bundle.
37+
*
38+
* @param string $path The path to the resource bundle.
39+
* @param BundleEntryReaderInterface $reader The reader for reading the resource bundle.
40+
*/
41+
public function __construct($path, BundleEntryReaderInterface $reader)
42+
{
43+
$this->path = $path;
44+
$this->reader = $reader;
45+
}
46+
47+
public function getIDs()
48+
{
49+
return $this->reader->readEntry($this->path, 'meta', array('Timezones'));
50+
}
51+
52+
public function getName($zoneID, $displayLocale = null)
53+
{
54+
if (null === $displayLocale) {
55+
$displayLocale = Locale::getDefault();
56+
}
57+
58+
return $this->reader->readEntry($this->path, $displayLocale, array('Names', $zoneID));
59+
}
60+
61+
public function getNames($displayLocale = null)
62+
{
63+
if (null === $displayLocale) {
64+
$displayLocale = Locale::getDefault();
65+
}
66+
67+
$names = $this->reader->readEntry($this->path, $displayLocale, array('Names'));
68+
69+
if ($names instanceof \Traversable) {
70+
$names = iterator_to_array($names);
71+
}
72+
73+
// Sorting by value cannot be done during bundle generation, because
74+
// binary bundles are always sorted by keys
75+
$collator = new \Collator($displayLocale);
76+
$collator->asort($names);
77+
78+
return $names;
79+
}
80+
}

‎src/Symfony/Component/Intl/Intl.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Intl/Intl.php
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use Symfony\Component\Intl\ResourceBundle\LocaleBundleInterface;
2525
use Symfony\Component\Intl\ResourceBundle\RegionBundle;
2626
use Symfony\Component\Intl\ResourceBundle\RegionBundleInterface;
27+
use Symfony\Component\Intl\ResourceBundle\TimezoneBundle;
28+
use Symfony\Component\Intl\ResourceBundle\TimezoneBundleInterface;
2729

2830
/**
2931
* Gives access to internationalization data.
@@ -63,6 +65,11 @@ final class Intl
6365
*/
6466
const REGION_DIR = 'regions';
6567

68+
/**
69+
* The directory name of the timezone data.
70+
*/
71+
const TIMEZONE_DIR = 'timezones';
72+
6673
/**
6774
* @var ResourceBundle\CurrencyBundleInterface
6875
*/
@@ -83,6 +90,11 @@ final class Intl
8390
*/
8491
private static $regionBundle;
8592

93+
/**
94+
* @var ResourceBundle\TimezoneBundleInterface
95+
*/
96+
private static $timezoneBundle;
97+
8698
/**
8799
* @var string|bool|null
88100
*/
@@ -183,6 +195,24 @@ public static function getRegionBundle()
183195
return self::$regionBundle;
184196
}
185197

198+
/**
199+
* Returns the bundle containing timezone information.
200+
*
201+
* @return TimezoneBundleInterface The timezone resource bundle.
202+
*/
203+
public static function getTimezoneBundle()
204+
{
205+
if (null === self::$timezoneBundle) {
206+
self::$timezoneBundle = new TimezoneBundle(
207+
self::getDataDirectory().'/'.self::TIMEZONE_DIR,
208+
self::getEntryReader(),
209+
self::getLocaleBundle()
210+
);
211+
}
212+
213+
return self::$timezoneBundle;
214+
}
215+
186216
/**
187217
* Returns the version of the installed ICU library.
188218
*
+87Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Intl\ResourceBundle;
13+
14+
use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReaderInterface;
15+
use Symfony\Component\Intl\Data\Provider\LocaleDataProvider;
16+
use Symfony\Component\Intl\Data\Provider\TimezoneDataProvider;
17+
use Symfony\Component\Intl\Exception\MissingResourceException;
18+
19+
/**
20+
* Default implementation of {@link TimezoneBundleInterface}.
21+
*
22+
* @internal
23+
*/
24+
class TimezoneBundle extends TimezoneDataProvider implements TimezoneBundleInterface
25+
{
26+
/**
27+
* Creates a new timezone bundle.
28+
*
29+
* @param string $path
30+
* @param BundleEntryReaderInterface $reader
31+
* @param LocaleDataProvider $localeProvider
32+
*/
33+
public function __construct($path, BundleEntryReaderInterface $reader, LocaleDataProvider $localeProvider)
34+
{
35+
parent::__construct($path, $reader);
36+
37+
$this->localeProvider = $localeProvider;
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function getIDs()
44+
{
45+
try {
46+
return parent::getIDs();
47+
} catch (MissingResourceException $e) {
48+
return array();
49+
}
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function getTimezoneName($zoneID, $displayLocale = null)
56+
{
57+
try {
58+
return $this->getName($zoneID, $displayLocale);
59+
} catch (MissingResourceException $e) {
60+
return;
61+
}
62+
}
63+
64+
/**
65+
* {@inheritdoc}
66+
*/
67+
public function getTimezoneNames($displayLocale = null)
68+
{
69+
try {
70+
return $this->getNames($displayLocale);
71+
} catch (MissingResourceException $e) {
72+
return array();
73+
}
74+
}
75+
76+
/**
77+
* {@inheritdoc}
78+
*/
79+
public function getLocales()
80+
{
81+
try {
82+
return $this->localeProvider->getLocales();
83+
} catch (MissingResourceException $e) {
84+
return array();
85+
}
86+
}
87+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.