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