root/trunk/uploadr/strings_import.php

Revision 368, 2.5 kB (checked in by calh, 2 years ago)

don't strip newlines (it makes debugging difficultr). work with my windows version of php (wtf no argv?). die if you can't find the templates folder (since cygwin is being dumb today). print progress as you go

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