forked from taman777/ip-login-restrictor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile_mo.php
More file actions
63 lines (51 loc) · 1.94 KB
/
Copy pathcompile_mo.php
File metadata and controls
63 lines (51 loc) · 1.94 KB
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
<?php
/**
* Simple PO to MO converter script
* Based on basic Gettext MO format
*/
function compile_po_to_mo($po_file, $mo_file) {
if (!file_exists($po_file)) return false;
$contents = file_get_contents($po_file);
$items = [];
// Simple regex to extract msgid and msgstr pairs
// Note: This doesn't handle plural forms or multi-line strings perfectly,
// but works for standard single strings.
preg_match_all('/msgid\s+"(.*)"\s+msgstr\s+"(.*)"/u', $contents, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
if ($match[1] === "" && $match[2] !== "") continue; // skip header
$items[$match[1]] = $match[2];
}
ksort($items);
$num_items = count($items);
// MO file header
$output = pack('I*', 0x950412de, 0, $num_items, 28, 28 + ($num_items * 8), 0, 0);
$ids_table = '';
$strs_table = '';
$ids_offsets = [];
$strs_offsets = [];
$current_id_offset = 28 + ($num_items * 16);
foreach ($items as $id => $str) {
$ids_offsets[] = [strlen($id), $current_id_offset];
$ids_table .= $id . "\0";
$current_id_offset += strlen($id) + 1;
}
$current_str_offset = $current_id_offset;
foreach ($items as $id => $str) {
$strs_offsets[] = [strlen($str), $current_str_offset];
$strs_table .= $str . "\0";
$current_str_offset += strlen($str) + 1;
}
// Build tables
$tables = '';
foreach ($ids_offsets as $offset) $tables .= pack('II', $offset[0], $offset[1]);
foreach ($strs_offsets as $offset) $tables .= pack('II', $offset[0], $offset[1]);
return file_put_contents($mo_file, $output . $tables . $ids_table . $strs_table);
}
$po = __DIR__ . "/languages/ip-login-restrictor-ja.po";
$mo = __DIR__ . "/languages/ip-login-restrictor-ja.mo";
if (compile_po_to_mo($po, $mo)) {
echo "Successfully generated $mo\n";
} else {
echo "Failed to generate $mo\n";
exit(1);
}