/
generate.php
134 lines (106 loc) · 3.76 KB
/
generate.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
* Search through sets of possible translation directories for JSON files
* which define (key => value) mappings for I18n strings, and generate
* out a list of all discovered mappings locally.
*/
require(__DIR__ . "/functions.php");
if (count($argv) < 2) {
throw new Exception("Needs file root parameter");
}
$root = $argv[1];
if (!file_exists($root . "/translation-discovery.json")) {
throw new Exception("No translation-discovery.json found in '$root'");
}
$json = json_decode(file_get_contents($root . "/translation-discovery.json"), true /* assoc */);
// add default parameters
$json += array(
'src' => 'vendor/*/*',
'translations' => array(
// 'fr' => 'generated/translation/fr.json',
),
'generate_php' => true,
'depth' => 3
);
if (!is_array($json['src'])) {
$json['src'] = array($json['src']);
}
if (!$json['translations']) {
throw new Exception("No translations defined in translation-discovery.json");
}
// make target directories as necessary
make_target_directories($json['translations']);
// now load all of the components
$all_dirs = get_all_directories($root, $json['depth']);
echo "Found " . count($all_dirs) . " potential subdirectories\n";
$selected_dirs = array();
foreach ($json['src'] as $pattern) {
$selected_dirs = array_merge($selected_dirs, get_directories_to_search($all_dirs, $pattern));
}
echo "Filtered to " . count($selected_dirs) . " matching paths\n";
// default translation strings are empty
$translations = array();
foreach ($json['translations'] as $code => $ignored) {
$translations[$code] = array();
}
if ($selected_dirs) {
$filename = "translations.json";
echo "Processing translation components...\n";
$count = 0;
foreach ($selected_dirs as $dir) {
if (file_exists($dir . "/" . $filename)) {
$assets = json_decode(file_get_contents($dir . "/" . $filename), true);
if (!$assets) {
throw new Exception("Could not load JSON from '$dir/$filename'");
}
foreach ($json['translations'] as $code => $ignored) {
if (isset($assets[$code])) {
if (!is_array($assets[$code])) {
$assets[$code] = array($assets[$code]);
}
foreach ($assets[$code] as $path) {
$translations[$code] = array_merge($translations[$code], get_matching_paths($dir, $path));
}
}
}
$count++;
}
}
echo "Found $count translation-producing components\n";
}
foreach ($translations as $code => $includes) {
echo "Processing " . count($includes) . " '$code' includes...\n";
$result = array();
foreach ($includes as $include) {
$result = array_merge($result, json_decode(file_get_contents($include), true /* as assoc */));
}
// sort it
ksort($result);
// write it out
$out = $json['translations'][$code];
$fp = fopen($out, "w");
if (!$fp) {
throw new Exception("Could not open destination file '" . $out . "' for writing");
}
// we write this out manually so we can format it
write_out_formatted_json($fp, $result);
fclose($fp);
echo "Wrote " . count($result) . " $code strings to '$out'\n";
if ($json['generate_php']) {
$php = str_replace(".json", ".php", $out);
$fp = fopen($php, "w");
if (!$fp) {
throw new Exception("Could not open destination file '" . $php . "' for writing");
}
// we write this out manually so we can format it
$first = true;
fwrite($fp, "<?php\n\n/**\n * $code template file\n * Generated by translation-discovery at " . date('r') . "\n */\n\n");
fwrite($fp, '$' . "result = array(\n");
foreach ($result as $key => $value) {
fwrite($fp, "\t\"" . phpescapestring($key) . "\" => \"" . phpescapestring($value) . "\",\n");
}
fwrite($fp, ");\n");
fclose($fp);
echo "Wrote " . count($result) . " $code strings to '$php'\n";
}
}