root/trunk/uploadr/strings_import.php

Revision 332, 2.4 kB (checked in by rcrowley, 2 years ago)

Bugfix for strings_import and new strings

Line 
1 <?php
2
3 # Get rid of the PHP crap that comes back from translation and place the files appropriately
4
5 $locale = dirname(__FILE__) . '/MacUploadr.app/Contents/Resources/chrome/locale';
6
7 # Gotta have a source folder for all those other languages
8 if (!isset($argv[1])) {
9     die("Usage: $argv[0] <intl-directory>\n");
10 }
11
12 # Clean each directory's files
13 foreach (array('de-de', 'es-us', 'fr-fr', 'it-it', 'ko-kr', 'pt-br', 'zh-hk') as $l) {
14     $dir = opendir("$argv[1]/$l");
15     while (false !== $file = readdir($dir)) {
16
17         # Only act on DTD and PROPERTIES files
18         if (0 == preg_match('/^.*ext_uploadr3_(.*\.(?:dtd|properties)).txt.php$/', $file, $match)) {
19             continue;
20         }
21
22         # Make sure the destination directory exists
23         if (!file_exists("$locale/$l") && !mkdir("$locale/$l", 0755)) {
24             echo "[error] Can't create locale $l\n";
25             continue;
26         }
27
28         # Clean PHP garbage out
29         $text = file_get_contents("$argv[1]/$l/$file");
30         if (false === $text) {
31             echo "[error] Reading $file\n";
32             continue;
33         }
34         $text = trim(str_replace("\n\n", "\n", preg_replace('/^<\?php.*\?>$/ms', '', $text)));
35
36         # Process each string
37         $lines = explode("\n", $text);
38         $ii = sizeof($lines);
39         $replace = array();
40         for ($i = 0; $i < $ii; ++$i) {
41
42             # Escape & and " properly
43             if (preg_match('/^(<!ENTITY [a-zA-Z0-9.]+ ")(.+)(">)$/', $lines[$i], $s)) {
44                 $lines[$i] = $s[1] . str_replace('"', '&#34;',
45                     preg_replace('/&(?!#\d\d;)/', '&#38;', $s[2])) . $s[3];
46             }
47
48             # Split up combined complicated strings
49             if (preg_match('/^<!ENTITY ([a-zA-Z0-9.]+) "(.+)">$/', $lines[$i], $s)
50                 && preg_match('/^(.+)\.joined$/', $s[1], $k)) {
51                 $parts = explode('  ', $s[2]);
52                 if (2 == sizeof($parts)) {
53                     $replace["{$k[1]}.1"] = "{$parts[0]} ";
54                     $replace["{$k[1]}.2"] = " {$parts[1]}";
55                     $lines[$i] = '';
56                 }
57             }
58
59         }
60
61         # Replace split up versions with their translations
62         for ($i = 0; $i < $ii; ++$i) {
63             if (preg_match('/^<!ENTITY ([a-zA-Z0-9.]+) ".+">$/', $lines[$i], $s)
64                 && isset($replace[$s[1]])) {
65                 $lines[$i] = "<!ENTITY {$s[1]} \"{$replace[$s[1]]}\">";
66             }
67         }
68
69         # Write and save
70         $file = next($match);
71         $file_p = fopen("$locale/$l/$file", 'w');
72         if (false === $file_p) {
73             echo "[error] Opening $file\n";
74             continue;
75         }
76         if (false === fwrite($file_p, implode("\n",
77             array_diff($lines, array(''))))) {
78             echo "[error] Writing $file\n";
79         }
80         if (false === fclose($file_p)) {
81             echo "[error] Closing $file\n";
82         }
83
84     }
85     closedir($dir);
86 }
87
88 ?>
Note: See TracBrowser for help on using the browser.