| 1 | <?php |
|---|
| 2 | include_once('intl/en.php'); |
|---|
| 3 | |
|---|
| 4 | $EXCLUDE = '#(intl|extlib|dist)#'; |
|---|
| 5 | |
|---|
| 6 | //Convert('.'); |
|---|
| 7 | // buildPOs(); |
|---|
| 8 | |
|---|
| 9 | function __callback__($matches) { |
|---|
| 10 | if (defined($matches[1])) { |
|---|
| 11 | return '__(\''.constant($matches[1]) .'\')'; |
|---|
| 12 | } else { |
|---|
| 13 | echo "??? " . print_r($matches,true) . "\n"; |
|---|
| 14 | } |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | function buildPOs() { |
|---|
| 18 | $files=array_keys(preg_find('/\.php$/','intl',PREG_FIND_RECURSIVE | PREG_FIND_FULLPATH|PREG_FIND_DIRONLY)); |
|---|
| 19 | if (($i=array_search('intl/en.php',$files)) !== FALSE) { |
|---|
| 20 | unset($files[$i]); |
|---|
| 21 | } |
|---|
| 22 | foreach($files as $file) { |
|---|
| 23 | echo "Now handling $file ..."; |
|---|
| 24 | $translations = array(); |
|---|
| 25 | $content = file_get_contents($file); |
|---|
| 26 | if(preg_match_all('#define\s*\([\'"](LOCALE_LINUX)[\'"][^,]*,\s*[\'"](.+)[\'"]\s*\);#',$content,$matches,PREG_SET_ORDER)) { |
|---|
| 27 | $locale = $matches[0][2]; |
|---|
| 28 | if (!file_exists("intl/$locale/LC_MESSAGES")) { |
|---|
| 29 | mkdir("intl/$locale",0755); |
|---|
| 30 | mkdir("intl/$locale/LC_MESSAGES",0755); |
|---|
| 31 | } |
|---|
| 32 | $fp = fopen("intl/$locale/LC_MESSAGES/messages.po",'w'); |
|---|
| 33 | if(!$fp) { |
|---|
| 34 | echo "\tERROR, Couldn't create output file!\n"; |
|---|
| 35 | continue; |
|---|
| 36 | } |
|---|
| 37 | $poheader='' |
|---|
| 38 | .'# |
|---|
| 39 | # |
|---|
| 40 | msgid "" |
|---|
| 41 | msgstr "" |
|---|
| 42 | "Project-Id-Version: Gregarius 0.5.5\n" |
|---|
| 43 | "Report-Msgid-Bugs-To: \n" |
|---|
| 44 | "POT-Creation-Date: \n" |
|---|
| 45 | "PO-Revision-Date: \n" |
|---|
| 46 | "Last-Translator: \n" |
|---|
| 47 | "Language-Team: \n" |
|---|
| 48 | "MIME-Version: 1.0\n" |
|---|
| 49 | "Content-Type: text/plain; charset=utf-8\n" |
|---|
| 50 | "Content-Transfer-Encoding: 8bit\n" |
|---|
| 51 | '; |
|---|
| 52 | fwrite($fp,$poheader."\n"); |
|---|
| 53 | } else { |
|---|
| 54 | echo "\tERROR, Unknown locale!\n"; |
|---|
| 55 | continue; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | if (preg_match_all('#define\s*\([\'"](LBL_[^\'"]+)[\'"][^,]*,\s*[\'"](.+)[\'"]\s*\);#',$content,$matches,PREG_SET_ORDER)) { |
|---|
| 59 | foreach($matches as $match) { |
|---|
| 60 | if (defined($match[1])) { |
|---|
| 61 | |
|---|
| 62 | fwrite($fp,"#: $match[1]\n"); |
|---|
| 63 | $en=str_replace('"','\"',stripslashes(stripslashes(constant($match[1])))); |
|---|
| 64 | $intl=str_replace('"','\"',stripslashes(stripslashes($match[2]))); |
|---|
| 65 | fwrite($fp,"msgid \"$en\"\n"); |
|---|
| 66 | fwrite($fp,"msgstr \"$intl\"\n\n"); |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | fwrite($fp,"#### END Automatic translation - ". date('r')." #\n"); |
|---|
| 70 | echo "\tdone!\n"; |
|---|
| 71 | } else { |
|---|
| 72 | echo "\tERROR, couldn't extract labels\n"; |
|---|
| 73 | } |
|---|
| 74 | if ($fp) { |
|---|
| 75 | @fclose($fp); |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | function Convert($dir) |
|---|
| 81 | { |
|---|
| 82 | global $EXCLUDE; |
|---|
| 83 | |
|---|
| 84 | $files=array_keys(preg_find('/\.php$/',$dir,PREG_FIND_RECURSIVE | PREG_FIND_FULLPATH)); |
|---|
| 85 | foreach($files as $file) { |
|---|
| 86 | if (preg_match($EXCLUDE,$file)) { |
|---|
| 87 | echo "Skiping $file\n"; |
|---|
| 88 | continue; |
|---|
| 89 | } |
|---|
| 90 | echo "Now handling $file ... "; |
|---|
| 91 | |
|---|
| 92 | $oldfile=$file; |
|---|
| 93 | $newfile=$file.'.new'; |
|---|
| 94 | $old = fopen($oldfile, 'r'); |
|---|
| 95 | $new = fopen($newfile, 'w'); |
|---|
| 96 | if (! ($old && $new)){ |
|---|
| 97 | die("Failed opening $oldfile and $newfile\n"); |
|---|
| 98 | } |
|---|
| 99 | while (!feof($old)) { |
|---|
| 100 | fwrite($new, preg_replace_callback('#(LBL_[A-Z0-9_]+)#','__callback__', fgets($old))); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | flush($new); |
|---|
| 104 | fclose($old); |
|---|
| 105 | fclose($new); |
|---|
| 106 | |
|---|
| 107 | rename($oldfile, $oldfile . '.old'); |
|---|
| 108 | rename($newfile, $oldfile); |
|---|
| 109 | |
|---|
| 110 | echo "\tdone!\n"; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | /* |
|---|
| 116 | * Find files in a directory matching a pattern |
|---|
| 117 | * |
|---|
| 118 | * |
|---|
| 119 | * Paul Gregg <pgregg@pgregg.com> |
|---|
| 120 | * 20 March 2004, Updated 20 April 2004 |
|---|
| 121 | * |
|---|
| 122 | * Open Source Code: If you use this code on your site for public |
|---|
| 123 | * access (i.e. on the Internet) then you must attribute the author and |
|---|
| 124 | * source web site: http://www.pgregg.com/projects/php/code/preg_find.phps |
|---|
| 125 | * Working example: http://www.pgregg.com/projects/php/code/preg_find_ex.phps |
|---|
| 126 | * |
|---|
| 127 | */ |
|---|
| 128 | |
|---|
| 129 | define('PREG_FIND_RECURSIVE', 1); |
|---|
| 130 | define('PREG_FIND_DIRMATCH', 2); |
|---|
| 131 | define('PREG_FIND_FULLPATH', 4); |
|---|
| 132 | define('PREG_FIND_NEGATE', 8); |
|---|
| 133 | define('PREG_FIND_DIRONLY', 16); |
|---|
| 134 | define('PREG_FIND_RETURNASSOC', 32); |
|---|
| 135 | |
|---|
| 136 | // PREG_FIND_RECURSIVE - go into subdirectorys looking for more files |
|---|
| 137 | // PREG_FIND_DIRMATCH - return directorys that match the pattern also |
|---|
| 138 | // PREG_FIND_DIRONLY - return only directorys that match the pattern (no files) |
|---|
| 139 | // PREG_FIND_FULLPATH - search for the pattern in the full path (dir+file) |
|---|
| 140 | // PREG_FIND_NEGATE - return files that don't match the pattern |
|---|
| 141 | // PREG_FIND_RETURNASSOC - Instead of just returning a plain array of matches, |
|---|
| 142 | // return an associative array with file stats |
|---|
| 143 | // to use more than one simply seperate them with a | character |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | // Search for files matching $pattern in $start_dir. |
|---|
| 148 | // if args contains PREG_FIND_RECURSIVE then do a recursive search |
|---|
| 149 | // return value is an associative array, the key of which is the path/file |
|---|
| 150 | // and the value is the stat of the file. |
|---|
| 151 | Function preg_find($pattern, $start_dir='.', $args=NULL) { |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | $files_matched = array(); |
|---|
| 155 | |
|---|
| 156 | $fh = opendir($start_dir); |
|---|
| 157 | |
|---|
| 158 | while (($file = readdir($fh)) !== false) { |
|---|
| 159 | if (strcmp($file, '.')==0 || strcmp($file, '..')==0) continue; |
|---|
| 160 | $filepath = $start_dir . '/' . $file; |
|---|
| 161 | if (preg_match($pattern, |
|---|
| 162 | ($args & PREG_FIND_FULLPATH) ? $filepath : $file)) { |
|---|
| 163 | $doadd = is_file($filepath) |
|---|
| 164 | || (is_dir($filepath) && ($args & PREG_FIND_DIRMATCH)) |
|---|
| 165 | || (is_dir($filepath) && ($args & PREG_FIND_DIRONLY)); |
|---|
| 166 | if ($args & PREG_FIND_DIRONLY && $doadd && !is_dir($filepath)) $doadd = false; |
|---|
| 167 | if ($args & PREG_FIND_NEGATE) $doadd = !$doadd; |
|---|
| 168 | if ($doadd) { |
|---|
| 169 | if ($args & PREG_FIND_RETURNASSOC) { // return more than just the filenames |
|---|
| 170 | $fileres = array(); |
|---|
| 171 | if (function_exists('stat')) { |
|---|
| 172 | $fileres['stat'] = stat($filepath); |
|---|
| 173 | $fileres['du'] = $fileres['stat']['blocks'] * 512; |
|---|
| 174 | } |
|---|
| 175 | if (function_exists('fileowner')) $fileres['uid'] = fileowner($filepath); |
|---|
| 176 | if (function_exists('filegroup')) $fileres['gid'] = filegroup($filepath); |
|---|
| 177 | if (function_exists('filetype')) $fileres['filetype'] = filetype($filepath); |
|---|
| 178 | if (function_exists('mime_content_type')) $fileres['mimetype'] = mime_content_type($filepath); |
|---|
| 179 | if (function_exists('dirname')) $fileres['dirname'] = dirname($filepath); |
|---|
| 180 | if (function_exists('basename')) $fileres['basename'] = basename($filepath); |
|---|
| 181 | if (isset($fileres['uid']) && function_exists('posix_getpwuid ')) $fileres['owner'] = posix_getpwuid ($fileres['uid']); |
|---|
| 182 | $files_matched[$filepath] = $fileres; |
|---|
| 183 | } else |
|---|
| 184 | array_push($files_matched, $filepath); |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | if ( is_dir($filepath) && ($args & PREG_FIND_RECURSIVE) ) { |
|---|
| 188 | $files_matched = array_merge($files_matched, |
|---|
| 189 | preg_find($pattern, $filepath, $args)); |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | closedir($fh); |
|---|
| 194 | return $files_matched; |
|---|
| 195 | |
|---|
| 196 | } |
|---|
| 197 | ?> |
|---|