-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCatalog.php
195 lines (162 loc) · 6.55 KB
/
Catalog.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
namespace Kunststube\POTools;
class Catalog implements \IteratorAggregate {
protected $strings = array(),
$projectIdVersion = 'PACKAGE VERSION',
$reportMsgidBugsTo = null,
$potCreationDate = null,
$poRevisionDate = 'YEAR-MO-DA HO:MI+ZONE',
$lastTranslator = 'FULL NAME <EMAIL@ADDRESS>',
$languageTeam = 'LANGUAGE <EMAIL@ADDRESS>',
$language = null,
$pluralForms = null,
$encoding = 'UTF-8';
/**
* @param string $projectIdVersion Name and version of the project
* @param string $language ISO 639/ISO 3166 language code,
* see http://www.gnu.org/software/gettext/manual/gettext.html#Language-Codes
* @param string $languageTeam Name of translation team (typically full language name) and contact address,
* for example: "French <[email protected]>"
* @param string $encoding Encoding of strings/source material
*/
public function __construct($projectIdVersion = null, $language = null, $languageTeam = null, $encoding = 'UTF-8') {
$this->projectIdVersion = $projectIdVersion;
$this->language = $language;
$this->languageTeam = $languageTeam;
$this->encoding = $encoding;
$this->potCreationDate = date('Y-m-d H:iO');
}
/**
* @param string $projectIdVersion Name and version of the project
*/
public function setProjectIdVersion($projectIdVersion) {
$this->projectIdVersion = $projectIdVersion;
}
public function getProjectIdVersion() {
return $this->projectIdVersion;
}
/**
* @param string $address Address that translation teams can report problems with the source material to.
*/
public function setMsgidBugReportingAddress($address) {
$this->reportMsgidBugsTo = $address;
}
public function getMsgidBugReportingAddress() {
return $this->reportMsgidBugsTo;
}
/**
* @param string $date Date of creation of the POT file in "Y-m-d H:iO" format.
*/
public function setPotCreationDate($date) {
$this->potCreationDate = $date;
}
public function getPotCreationDate() {
return $this->potCreationDate;
}
/**
* @param string $date Date of last change to PO file in "Y-m-d H:iO" format.
*/
public function setPoRevisionDate($date) {
$this->poRevisionDate = $date;
}
public function getPoRevisionDate() {
return $this->poRevisionDate;
}
/**
* @param string $nameAndEmail Name and email of last translator, e.g. "John Doe <[email protected]>"
*/
public function setLastTranslator($nameAndEmail) {
$this->lastTranslator = $nameAndEmail;
}
public function getLastTranslator() {
return $this->lastTranslator;
}
/**
* @param string $languageTeamAndAddress Name of translation team and contact address.
*/
public function setLanguageTeam($languageNameAndAddress) {
$this->languageTeam = $languageNameAndAddress;
}
public function getLanguageTeam() {
return $this->languageTeam;
}
/**
* @param string $languageCode ISO 639/ISO 3166 language code
*/
public function setLanguage($languageCode) {
$this->language = $languageCode;
}
public function getLanguage() {
return $this->language;
}
/**
* @param string $pluralForms See http://translate.sourceforge.net/wiki/l10n/pluralforms
*/
public function setPluralForms($pluralForms) {
$this->pluralForms = $pluralForms;
}
public function getPluralForms() {
return $this->pluralForms;
}
/**
* @param string $encoding Encoding of strings/source material
*/
public function setEncoding($encoding) {
$this->encoding = $encoding;
}
public function getEncoding() {
return $this->encoding;
}
/**
* Add POString instance to catalog. If identical string already exists,
* reference and extracted comment attributes will be merged.
*/
public function add(POString $string) {
$id = $string->getMsgctxt() . "\04" . $string->getMsgid();
$category = $string->getCategoryText();
$domain = $string->getDomain();
if (isset($this->strings[$category][$domain][$id])) {
array_map(array($this->strings[$category][$domain][$id], 'addReference'), $string->getReferences());
array_map(array($this->strings[$category][$domain][$id], 'addExtractedComment'), $string->getExtractedComments());
} else {
$this->strings[$category][$domain][$id] = $string;
}
}
/**
* Get a flat iterable list of all strings in the catalog.
*/
public function getIterator() {
require_once __DIR__ . DIRECTORY_SEPARATOR . 'RecursiveArrayOnlyIterator.php';
return new \RecursiveIteratorIterator(new RecursiveArrayOnlyIterator($this->strings));
}
/**
* Write the catalog to a directory. Entries will be split into subdirectories and
* files based on category and domain.
*
* @param string $path Path to output directory. Requires permissions to create new directories and files within.
* @param POWriterFactory $writerFactory Instance of factory to instantiate individual POWriter objects for each file.
*/
public function writeToDirectory($path, POWriterFactory $writerFactory = null) {
if (!$writerFactory) {
require_once __DIR__ . DIRECTORY_SEPARATOR . 'POWriterFactory.php';
$writerFactory = new POWriterFactory;
}
$path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
if (!file_exists($path) || !is_dir($path)) {
throw new \InvalidArgumentException("Cannot access path $path");
}
foreach ($this->strings as $category => $domains) {
if (!file_exists($path . $category)) {
mkdir($path . $category);
}
foreach ($domains as $domain => $strings) {
$file = fopen($path . $category . DIRECTORY_SEPARATOR . "$domain.pot", 'w');
$writer = $writerFactory->construct($file, $this);
foreach ($strings as $string) {
$writer->write($string);
}
fclose($file);
}
}
}
}