/
Z_poEditLangModule.php
238 lines (180 loc) · 7.48 KB
/
Z_poEditLangModule.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/* $Revision: 1.10 $ */
/* Steve Kitchen */
/* This code is really ugly ... */
$PageSecurity = 15;
include ('includes/session.inc');
$title = _('Edit Module');
include('includes/header.inc');
/* Your webserver user MUST have read/write access to here,
otherwise you'll be wasting your time */
$PathToLanguage = './locale/' . $_SESSION['Language'] . '/LC_MESSAGES/messages.po';
$PathToNewLanguage = './locale/' . $_SESSION['Language'] . '/LC_MESSAGES/messages.po.new';
echo "<br> <a href='" . $rootpath . "/Z_poAdmin.php'>" . _('Back to the translation menu') . "</a>";
echo '<br><br> ' . _('Utility to edit a language file module');
echo '<br> ' . _('Current language is') . ' ' . $_SESSION['Language'];
echo '<br><br> ' . _('To change language click on the user name at the top left, change to language desired and click Modify');
echo '<br> ' . _('Make sure you have selected the correct language to translate!');
if (isset($_POST['ReMergePO'])){
/*update the messages.po file with any new strings */
/*first rebuild the en_GB default with xgettext */
$PathToDefault = './locale/en_GB/LC_MESSAGES/messages.po';
$FilesToInclude = '*php includes/*.php includes/*.inc';
$xgettextCmd = 'xgettext --no-wrap -L php -o ' . $PathToDefault . ' ' . $FilesToInclude;
system($xgettextCmd);
/*now merge the translated file with the new template to get new strings*/
$msgMergeCmd = 'msgmerge --no-wrap --update ' . $PathToLanguage . ' ' . $PathToDefault;
system($msgMergeCmd);
//$Result = rename($PathToNewLanguage, $PathToLanguage);
exit;
}
if (isset($_POST['module'])) {
// a module has been selected and is being modified
$PathToLanguage_mo = substr($PathToLanguage,0,strrpos($PathToLanguage,'.')) . '.mo';
/* now read in the language file */
$LangFile = file($PathToLanguage);
$LangFileEntries = sizeof($LangFile);
if (isset($_POST['submit'])) {
// save the modifications
echo '<br><table><tr><td>';
echo '<form method="post" action=' . $_SERVER['PHP_SELF'] . '?' . SID . '>';
/* write the new language file */
prnMsg (_('Writing the language file') . '.....<br>', 'info', ' ');
for ($i=17; $i<=$LangFileEntries; $i++) {
if (isset($_POST['msgstr_'.$i])) {
$LangFile[$i] = 'msgstr "' . htmlentities($_POST['moduletext_'.$i]) . '"' . "\n";
}
}
$fpOut = fopen($PathToNewLanguage, 'w');
for ($i=0; $i<=$LangFileEntries; $i++) {
$Result = fputs($fpOut, $LangFile[$i]);
}
$Result = fclose($fpOut);
/* Done writing, now move the original file to a .old */
/* and the new one to the default */
if (file_exists($PathToLanguage . '.old')) {
$Result = rename($PathToLanguage . '.old', $PathToLanguage . '.bak');
}
$Result = rename($PathToLanguage, $PathToLanguage . '.old');
$Result = rename($PathToNewLanguage, $PathToLanguage);
if (file_exists($PathToLanguage . '.bak')) {
$Result = unlink($PathToLanguage . '.bak');
}
/*now need to create the .mo file from the .po file */
$msgfmtCommand = 'msgfmt ' . $PathToLanguage . ' -o ' . $PathToLanguage_mo;
system($msgfmtCommand);
prnMsg (_('Done') . '<br>', 'info', ' ');
echo '</form>';
echo '</td></tr></table>';
/* End of Submit block */
} else {
/* now we need to parse the resulting array into something we can show the user */
$j = 1;
for ($i=17; $i<=$LangFileEntries; $i++) { /* start at line 18 to skip the header */
if (substr($LangFile[$i], 0, 2) == '#:') { /* it's a module reference */
$AlsoIn[$j] .= str_replace(' ','<br>', substr($LangFile[$i],3)) . '<br>';
} elseif (substr($LangFile[$i], 0 , 5) == 'msgid') {
$DefaultText[$j] = substr($LangFile[$i], 7, strlen($LangFile[$i])-9);
} elseif (substr($LangFile[$i], 0 , 6) == 'msgstr') {
$ModuleText[$j] = substr($LangFile[$i], 8, strlen($LangFile[$i])-10);
$msgstr[$j] = $i;
$j++;
}
}
$TotalLines = $j - 1;
/* stick it on the screen */
echo '<br> ' . _('When finished modifying you must click on Modify at the bottom in order to save changes');
echo '<div class="centre">';
echo '<br>';
prnMsg (_('Your existing translation file (messages.po) will be saved as messages.po.old') . '<br>', 'info', _('PLEASE NOTE'));
echo '<br>';
echo '<form method="post" action=' . $_SERVER['PHP_SELF'] . '?' . SID . '>';
echo '</div';
echo '<table>';
echo '<tr><th ALIGN="center">' . _('Language File for') . ' "' . $_POST['language'] . '"</th></tr>';
echo '<tr><td ALIGN="center">' . _('Module') . ' "' . $_POST['module'] . '"</td></tr>';
echo '<tr><td></td></tr>';
echo '<tr><td>';
echo '<table WIDTH="100%">';
echo '<tr>';
echo '<th>' . _('Default text') . '</th>';
echo '<th>' . _('Translation') . '</th>';
echo '<th>' . _('Exists in') . '</th>';
echo '</tr>' . "\n";
for ($i=1; $i<=$TotalLines; $i++) {
$b = strpos($AlsoIn[$i], $_POST['module']);
if ($b === False) {
/* skip it */
} else {
echo '<tr>';
echo '<td VALIGN="top"><I>' . $DefaultText[$i] . '</I></td>';
echo '<td VALIGN="top"><input type="text" size="60" name="moduletext_' . $msgstr[$i] . '" VALUE="' . $ModuleText[$i] . '"></td>';
echo '<td VALIGN="top">' . $AlsoIn[$i] . '<input type="hidden" name="msgstr_' . $msgstr[$i] . '" VALUE="' . $msgstr[$i] . '"></td>';
echo '</tr>';
echo '<tr><th colspan="3"></th></tr>';
}
}
echo '</table>';
echo '</td></tr>';
echo '</table>';
echo '<br><div class="centre">';
echo '<input type="Submit" name="submit" VALUE="' . _('Modify') . '"> ';
echo '<input type="hidden" name="module" VALUE="' . $_POST['module'] . '">';
echo '</form>';
echo '</div>';
}
} else {
/* get available modules */
/* This is a messy way of producing a directory listing of ./locale to fish out */
/* the language directories that have been set up */
/* The other option would be to define an array of the languages you want */
/* and check for the existance of the directory */
/* $ListDirCmd should probably be defined in config.php as a global value */
/* You'll need to change it if you are running a Windows server - sorry !! */
if ($handle = opendir('.')) {
$i=0;
while (false !== ($file = readdir($handle))) {
if ((substr($file, 0, 1) != ".") && (!is_dir($file))) {
$AvailableModules[$i] = $file;
$i += 1;
}
}
closedir($handle);
}
if ($handle = opendir(".//includes")) {
while (false !== ($file = readdir($handle))) {
if ((substr($file, 0, 1) != ".") && (!is_dir($file))) {
$AvailableModules[$i] = $file;
$i += 1;
}
}
closedir($handle);
}
sort($AvailableModules);
$NumberOfModules = sizeof($AvailableModules) - 1;
if (!is_writable('./locale/' . $_SESSION['Language'])) {
prnMsg(_('You do not have write access to the required files please contact your system administrator'),'error');
}
else
{
echo '<br><table><tr><td>';
echo '<form method="post" action=' . $_SERVER['PHP_SELF'] . '?' . SID . '>';
echo '<table>';
echo '<tr><td>' . _('Select the module to edit') . '</td>';
echo '<td><select name="module">';
for ($i=0; $i<$NumberOfModules; $i++) {
echo '<option>' . $AvailableModules[$i] . '</option>';
}
echo '</select></td>';
echo '</tr></table>';
echo '<br>';
echo '<div class="centre">';
echo '<input type="Submit" name="proceed" VALUE="' . _('Proceed') . '"> ';
echo '<br><br><input type="Submit" name="ReMergePO" VALUE="' . _('Refresh messages with latest strings') . '">';
echo '</div>';
echo '</form>';
echo '</td></tr></table>';
}
}
include('includes/footer.inc');
?>