-
Notifications
You must be signed in to change notification settings - Fork 64
/
generate_pattern_files.php
103 lines (90 loc) · 2.79 KB
/
generate_pattern_files.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace MRBS;
use IntlDateFormatter;
use IntlDatePatternGenerator;
function write_line($fp, string $key, string $value) : void
{
$keywords = ['no', 'yes', 'true', 'false', 'on', 'off', 'null', 'none'];
if (in_arrayi($key, $keywords))
{
// If it's a keyword we put a space before the key because it is a keyword and otherwise
// parse_ini_file() will fail. The space is trimmed when it is parsed.
// See https://stackoverflow.com/questions/6142980/using-a-keyword-as-variable-name-in-an-ini-file
$key = " $key";
$comment = "; The following line has a space before the key because it is a keyword\n";
fwrite($fp, $comment);
}
fwrite($fp, "$key = \"$value\"\n");
}
if (!extension_loaded('intl'))
{
die("The 'intl' extension needs to be loaded.");
}
require "defaultincludes.inc";
$dir = 'tmp';
$locales = \ResourceBundle::getLocales('');
echo "<h2>Skeleton files</h2>\n";
$skeletons = array(
'd',
'dEMMM',
'dMMM',
'dMMMM',
'MMMMy'
);
foreach ($skeletons as $skeleton) {
$filename = "$dir/skeletons/$skeleton.ini";
echo "Generating $filename ...";
$fp = fopen($filename, 'w');
foreach ($locales as $locale)
{
$locale = convert_to_BCP47($locale);
$pattern_generator = new IntlDatePatternGenerator($locale);
$pattern = $pattern_generator->getBestPattern($skeleton);
write_line($fp, $locale, $pattern);
// Fix up for some locales
if (($locale == 'zh-Hans-CN') && !in_array('zh-CN', $locales)) {
write_line($fp, 'zh-CN', $pattern);
}
if (($locale == 'zh-Hant-TW') && !in_array('zh-TW', $locales)) {
write_line($fp, 'zh-TW', $pattern);
}
}
fclose($fp);
echo " done<br>\n";
}
echo "<h2>Type files</h2>\n";
$types = array(
'full' => IntlDateFormatter::FULL,
'long' => IntlDateFormatter::LONG,
'medium' => IntlDateFormatter::MEDIUM,
'short' => IntlDateFormatter::SHORT,
'none' => IntlDateFormatter::NONE
);
foreach ($types as $date_key => $date_value)
{
foreach ($types as $time_key => $time_value)
{
if (($date_value === IntlDateFormatter::NONE) && ($time_value === IntlDateFormatter::NONE))
{
continue;
}
$filename = "$dir/types/{$date_key}_{$time_key}.ini";
echo "Generating $filename ...";
$fp = fopen($filename, 'w');
foreach ($locales as $locale) {
$locale = convert_to_BCP47($locale);
$formatter = new IntlDateFormatter($locale, $date_value, $time_value);
$pattern = $formatter->getPattern();
write_line($fp, $locale, $pattern);
// Fix up for some locales
if (($locale == 'zh-Hans-CN') && !in_array('zh-CN', $locales)) {
write_line($fp, 'zh-CN', $pattern);
}
if (($locale == 'zh-Hant-TW') && !in_array('zh-TW', $locales)) {
write_line($fp, 'zh-TW', $pattern);
}
}
fclose($fp);
echo " done<br>\n";
}
}