|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the {@link Localization_Writer} class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Localization |
|
6
|
|
|
* @subpackage Translator |
|
7
|
|
|
* @see Localization_Writer |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace AppLocalize; |
|
11
|
|
|
|
|
12
|
|
|
use AppUtils\FileHelper; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Utility used to write localized strings to an ini file. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Localization |
|
18
|
|
|
* @subpackage Translator |
|
19
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class Localization_Writer |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var boolean |
|
25
|
|
|
*/ |
|
26
|
|
|
private $editable = false; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array<string,string> |
|
30
|
|
|
*/ |
|
31
|
|
|
private $hashes = array(); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var Localization_Locale |
|
35
|
|
|
*/ |
|
36
|
|
|
private $locale; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
private $fileType; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
private $filePath; |
|
47
|
|
|
|
|
48
|
|
|
public function __construct(Localization_Locale $locale, string $fileType, string $filePath) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->locale = $locale; |
|
51
|
|
|
$this->fileType = $fileType; |
|
52
|
|
|
$this->filePath = $filePath; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function makeEditable() : Localization_Writer |
|
56
|
|
|
{ |
|
57
|
|
|
$this->editable = true; |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function addHash(string $hash, string $text) : Localization_Writer |
|
63
|
|
|
{ |
|
64
|
|
|
$this->hashes[$hash] = $text; |
|
65
|
|
|
|
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function addHashes(array $hashes) : Localization_Writer |
|
70
|
|
|
{ |
|
71
|
|
|
foreach($hashes as $hash => $text) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->addHash($hash, $text); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function writeFile() : void |
|
80
|
|
|
{ |
|
81
|
|
|
$content = |
|
82
|
|
|
$this->renderHead(). |
|
83
|
|
|
$this->renderHashes(); |
|
84
|
|
|
|
|
85
|
|
|
FileHelper::saveFile($this->filePath, $content); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private function renderHashes() : string |
|
89
|
|
|
{ |
|
90
|
|
|
$hashes = $this->compileHashes(); |
|
91
|
|
|
$lines = array(); |
|
92
|
|
|
|
|
93
|
|
|
foreach($hashes as $entry) |
|
94
|
|
|
{ |
|
95
|
|
|
$lines[] = sprintf( |
|
96
|
|
|
'%s= "%s"', |
|
97
|
|
|
$entry['hash'], |
|
98
|
|
|
addslashes($entry['text']) |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$lines[] = ''; |
|
103
|
|
|
|
|
104
|
|
|
return implode(PHP_EOL, $lines); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
private function renderHead() : string |
|
108
|
|
|
{ |
|
109
|
|
|
$title = strtoupper($this->fileType).' TRANSLATION FILE FOR ' . strtoupper($this->locale->getLabel()); |
|
110
|
|
|
|
|
111
|
|
|
$lines = array(); |
|
112
|
|
|
|
|
113
|
|
|
$lines[] = '; -------------------------------------------------------'; |
|
114
|
|
|
$lines[] = '; '. $title; |
|
115
|
|
|
$lines[] = '; -------------------------------------------------------'; |
|
116
|
|
|
$lines[] = '; '; |
|
117
|
|
|
|
|
118
|
|
|
if($this->editable) |
|
119
|
|
|
{ |
|
120
|
|
|
$lines[] = '; You may edit text directly in this file under the following conditions:'; |
|
121
|
|
|
$lines[] = '; '; |
|
122
|
|
|
$lines[] = '; 1) Do not to modify the keys (left hand side of the = sign)'; |
|
123
|
|
|
$lines[] = '; 2) Save the file as UTF-8 without BOM'; |
|
124
|
|
|
} |
|
125
|
|
|
else |
|
126
|
|
|
{ |
|
127
|
|
|
$lines[] = '; Do NOT edit this file directly! It depends on the main translation file'; |
|
128
|
|
|
$lines[] = '; and any changes will be lost. Edit the main file instead.'; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$lines[] = PHP_EOL; |
|
132
|
|
|
|
|
133
|
|
|
return implode(PHP_EOL, $lines); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
private function compileHashes() : array |
|
137
|
|
|
{ |
|
138
|
|
|
$hashes = array(); |
|
139
|
|
|
|
|
140
|
|
|
foreach($this->hashes as $hash => $text) |
|
141
|
|
|
{ |
|
142
|
|
|
$hashes[] = array( |
|
143
|
|
|
'hash' => $hash, |
|
144
|
|
|
'text' => $text |
|
145
|
|
|
); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
usort($hashes, array($this, 'callback_sortStrings')); |
|
149
|
|
|
|
|
150
|
|
|
return $hashes; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Sort the strings to ensure they always appear in the same order: |
|
155
|
|
|
* first by text, and same strings by their hashes. This is important |
|
156
|
|
|
* for strings that have the same translation to avoid them changing |
|
157
|
|
|
* order between sorts. |
|
158
|
|
|
* |
|
159
|
|
|
* @param array $a |
|
160
|
|
|
* @param array $b |
|
161
|
|
|
* @return number |
|
162
|
|
|
*/ |
|
163
|
|
|
public function callback_sortStrings(array $a, array $b) : int |
|
164
|
|
|
{ |
|
165
|
|
|
$result = strnatcasecmp($a['text'], $b['text']); |
|
166
|
|
|
|
|
167
|
|
|
if($result === 0) |
|
168
|
|
|
{ |
|
169
|
|
|
return strnatcmp($a['hash'], $b['hash']); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $result; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|