droy | 007252f | 2008-07-11 20:20:48 +0000 | [diff] [blame] | 1 | <?php |
| 2 | /******************************************************************************* |
| 3 | * Copyright (c) 2007 Eclipse Foundation and others. |
| 4 | * All rights reserved. This program and the accompanying materials |
| 5 | * are made available under the terms of the Eclipse Public License v1.0 |
| 6 | * which accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Eclipse Foundation - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | # TODO: this is really rough and needs to be cleaned up |
| 14 | # but it will import translation strings from .properties files into the target project/version you specify below |
| 15 | # It also doesn't overwrite translations that were corrected by users, so you can |
| 16 | # run the import over later with little impact |
| 17 | # sub-structure: ./XX/eclipse/plugins/ where XX is the iso code for the language |
| 18 | # See: http://www.eclipse.org/babel/development/large_contributions.php |
atoulme | 08f51af | 2008-11-18 12:41:33 +0000 | [diff] [blame] | 19 | # For instructions on how to import the files, see here: |
| 20 | # http://wiki.eclipse.org/Babel_/_Large_Contribution_Import_Process |
droy | 007252f | 2008-07-11 20:20:48 +0000 | [diff] [blame] | 21 | |
droy | 03e8645 | 2008-07-18 14:04:21 +0000 | [diff] [blame] | 22 | # To run this, copy the file to the 'root' of html/ (where translate.php resides) |
droy | ef2077b | 2008-07-21 14:51:52 +0000 | [diff] [blame] | 23 | # and set the project, version and indir, then call the script from a browser, or wget |
| 24 | |
| 25 | # See convertiso.sh if the incoming files have various encodings. |
droy | 03e8645 | 2008-07-18 14:04:21 +0000 | [diff] [blame] | 26 | |
droy | 007252f | 2008-07-11 20:20:48 +0000 | [diff] [blame] | 27 | |
droy | a96c68b | 2008-11-19 19:58:49 +0000 | [diff] [blame] | 28 | error_reporting(E_ALL); ini_set("display_errors", true); |
| 29 | |
| 30 | |
droy | 007252f | 2008-07-11 20:20:48 +0000 | [diff] [blame] | 31 | header("Content-type: text/plain"); |
droy | 03e8645 | 2008-07-18 14:04:21 +0000 | [diff] [blame] | 32 | include("global.php"); |
droy | 007252f | 2008-07-11 20:20:48 +0000 | [diff] [blame] | 33 | InitPage(""); |
| 34 | |
atoulme | 4e313f1 | 2008-11-17 15:43:13 +0000 | [diff] [blame] | 35 | |
droy | 007252f | 2008-07-11 20:20:48 +0000 | [diff] [blame] | 36 | $headless = 1; |
| 37 | |
droy | a12ecce | 2008-11-19 19:39:08 +0000 | [diff] [blame] | 38 | # See http://wiki.eclipse.org/Babel_/_Large_Contribution_Import_Process |
| 39 | # |
| 40 | # !! IMPORTANT !! |
| 41 | # Set to 1 unless the translations were authored (and tested/reviewed in context) by professionals |
| 42 | # This doesn't mean all incoming translations will be fuzzy -- |
| 43 | # only those that are 'replacing' a non-fuzzy one |
| 44 | $fuzzy = 1; |
| 45 | |
droy | 007252f | 2008-07-11 20:20:48 +0000 | [diff] [blame] | 46 | |
atoulme | 12882d5 | 2009-01-26 18:39:17 +0000 | [diff] [blame] | 47 | require(dirname(__FILE__) . "/../file/file.class.php"); |
droy | 007252f | 2008-07-11 20:20:48 +0000 | [diff] [blame] | 48 | require_once("json_encode.php"); |
| 49 | |
| 50 | $pageTitle = "Babel - Import Translation archive"; |
| 51 | $pageKeywords = "import,properties,translation,language,nlpack,pack,eclipse,babel"; |
| 52 | |
atoulme | 08f51af | 2008-11-18 12:41:33 +0000 | [diff] [blame] | 53 | $USER = getGenieUser()->userid; |
droy | 007252f | 2008-07-11 20:20:48 +0000 | [diff] [blame] | 54 | $PROJECT_ID = "eclipse"; |
| 55 | $VERSION = "3.3.1"; |
| 56 | |
| 57 | # TODO |
atoulme | 4e313f1 | 2008-11-17 15:43:13 +0000 | [diff] [blame] | 58 | $indir = "/tmp/tmp-babel/import"; |
droy | 007252f | 2008-07-11 20:20:48 +0000 | [diff] [blame] | 59 | chdir($indir); |
| 60 | # sub-structure: ./XX/eclipse/plugins/ where XX is the iso code for the language |
| 61 | exec('find . -type f', $lines); |
| 62 | |
| 63 | # loop through files |
| 64 | foreach ($lines as $line) { |
| 65 | $line = trim($line); |
| 66 | echo $line . "<br />"; |
| 67 | if(preg_match("/^\.\/([a-zA-Z0-9_-]+)\/(.+\.properties)$/", $line, $matches)) { |
| 68 | $language = $matches[1]; |
| 69 | $file = $matches[2]; |
| 70 | |
| 71 | # Find a matching file |
| 72 | # File: eclipse/plugins/org.eclipse.jem.source/plugin.properties Language: ar |
| 73 | $file = preg_replace("/eclipse\/plugins\//", "", $file); |
| 74 | $first_part_file = substr($file, 0, strpos($file, "/")); |
| 75 | $second_part_file = substr($file, strpos($file, "/")); |
| 76 | $last_part_file = substr($file, strrpos($file, "/")); |
| 77 | echo "File: " . $file ." Language: $language\n"; |
| 78 | echo " first part: " . $first_part_file ." second part: $second_part_file last part: $last_part_file\n"; |
| 79 | $file_id = 0; |
| 80 | $language_id = 0; |
| 81 | |
| 82 | $SQL = "SELECT F.file_id, L.language_id |
| 83 | FROM files AS F, languages AS L WHERE F.is_active = 1 |
| 84 | AND F.project_id = '" . $PROJECT_ID . "' AND F.version = '" . $VERSION . "' |
| 85 | AND F.name LIKE '%" . $first_part_file . "%' AND F.name LIKE '%" . $second_part_file . "' AND L.iso_code = '" . $language . "'"; |
kitlo | 2c8d8a9 | 2018-04-19 13:25:09 -0400 | [diff] [blame] | 86 | $rs = mysqli_query($dbh, $SQL); |
kitlo | 1d02709 | 2018-04-19 17:44:07 -0400 | [diff] [blame] | 87 | if($myrow = mysqli_fetch_assoc($rs)) { |
droy | 007252f | 2008-07-11 20:20:48 +0000 | [diff] [blame] | 88 | $file_id = $myrow['file_id']; |
| 89 | $language_id = $myrow['language_id']; |
| 90 | echo " Found file: " . $file_id . "\n"; |
| 91 | } |
| 92 | else { |
| 93 | $SQL = "SELECT F.file_id, L.language_id |
| 94 | FROM files AS F, languages AS L WHERE F.is_active = 1 |
| 95 | AND F.project_id = '" . $PROJECT_ID . "' AND F.version = '" . $VERSION . "' |
| 96 | AND F.name LIKE '%" . $first_part_file . "%' AND F.name LIKE '%" . $last_part_file . "' AND L.iso_code = '" . $language . "'"; |
kitlo | 2c8d8a9 | 2018-04-19 13:25:09 -0400 | [diff] [blame] | 97 | $rs = mysqli_query($dbh, $SQL); |
kitlo | 1d02709 | 2018-04-19 17:44:07 -0400 | [diff] [blame] | 98 | if($myrow = mysqli_fetch_assoc($rs)) { |
droy | 007252f | 2008-07-11 20:20:48 +0000 | [diff] [blame] | 99 | $file_id = $myrow['file_id']; |
| 100 | $language_id = $myrow['language_id']; |
| 101 | echo " Found file: " . $file_id . "\n"; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if($file_id > 0 && $language_id > 0) { |
| 106 | # Get the list of translatable strings |
| 107 | |
| 108 | # Get the file contents |
| 109 | $fh = fopen($line, 'r'); |
| 110 | $size = filesize($line); |
| 111 | |
| 112 | $content = fread($fh, $size); |
| 113 | # echo $content . "<br/>"; |
| 114 | fclose($fh); |
| 115 | $previous_line = ""; |
| 116 | $lines = explode("\n", $content); |
| 117 | |
| 118 | # Loop through each string |
| 119 | foreach($lines as $line) { |
| 120 | # echo "Doing line: " . $line . "<br />"; |
| 121 | if(strlen($line) > 0 && $line[0] != "#" && $line[0] != ";") { |
| 122 | $line = trim($line); |
| 123 | |
| 124 | # Does line end with a \ ? |
| 125 | if(preg_match("/\\\\$/", $line)) { |
| 126 | # Line ends with \ |
| 127 | |
| 128 | # strip the backslash |
| 129 | $previous_line .= $line . "\n"; |
| 130 | } |
| 131 | else { |
| 132 | if($previous_line != "") { |
| 133 | $line = $previous_line . $line; |
| 134 | $previous_line = ""; |
| 135 | } |
| 136 | |
| 137 | $tags = explode("=", trim($line), 2); |
| 138 | if(count($tags) > 1) { |
| 139 | $tags[0] = trim($tags[0]); |
| 140 | $tags[1] = trim($tags[1]); |
atoulme | a44cda6 | 2008-12-15 14:56:50 +0000 | [diff] [blame] | 141 | $tags[1] = str_replace("\:", ":", $tags[1]); |
| 142 | $tags[1] = str_replace("\=", "=", $tags[1]); |
| 143 | $tags[1] = str_replace("\ ", " ", $tags[1]); |
droy | 007252f | 2008-07-11 20:20:48 +0000 | [diff] [blame] | 144 | # echo "Doing " . $tags[0] . " with value " . $tags[1] . " Unescaped: " . unescape($tags[1]); |
| 145 | |
| 146 | # Get the matching string name |
droy | a12ecce | 2008-11-19 19:39:08 +0000 | [diff] [blame] | 147 | $SQL = "SELECT s.string_id, s.value, tr.value as tr_last, tr.possibly_incorrect as tr_last_fuzzy, trv.value as ever_tr_value |
| 148 | FROM strings as s |
| 149 | left join translations as tr on (s.string_id = tr.string_id |
| 150 | and tr.language_id = $language_id |
| 151 | and tr.is_active) |
| 152 | left join translations as trv on (s.string_id = trv.string_id |
| 153 | and trv.language_id = $language_id |
| 154 | and trv.value = '" . addslashes(unescape($tags[1])) . "') |
| 155 | WHERE s.is_active = 1 AND s.non_translatable <> 1 AND s.file_id = " . $file_id . " AND s.name = '" . $tags[0] . "'"; |
kitlo | 2c8d8a9 | 2018-04-19 13:25:09 -0400 | [diff] [blame] | 156 | $rs_string = mysqli_query($dbh, $SQL); |
kitlo | 1d02709 | 2018-04-19 17:44:07 -0400 | [diff] [blame] | 157 | $myrow_string = mysqli_fetch_assoc($rs_string); |
droy | a12ecce | 2008-11-19 19:39:08 +0000 | [diff] [blame] | 158 | if($myrow_string['string_id'] > 0 # There is an English string |
| 159 | && $tags[1] != "" # With a non-null English value |
| 160 | && $myrow_string['ever_tr_value'] == "" # That's never been translated to this incoming value |
| 161 | && $tags[1] != $myrow_string['value'] # And the proposed translation is different from the English value |
droy | 64af78f | 2008-11-18 20:37:16 +0000 | [diff] [blame] | 162 | ) { |
droy | a12ecce | 2008-11-19 19:39:08 +0000 | [diff] [blame] | 163 | $insert_as_fuzzy = 0; |
| 164 | if($myrow_string['tr_last'] != "" && $fuzzy == 1 && $myrow_string['tr_last_fuzzy'] == 0) { |
| 165 | # This incoming translation is replacing an existing value that is *not* marked as fuzzy |
| 166 | # And the $fuzzy == 1, so we may be replacing a known good value !! |
| 167 | $insert_as_fuzzy = 1; |
| 168 | } |
| 169 | else { |
| 170 | ## Nothing. Insert as non-fuzzy. |
| 171 | ## If this is replacing a fuzzy value, then that's a good thing |
| 172 | } |
droy | 007252f | 2008-07-11 20:20:48 +0000 | [diff] [blame] | 173 | echo " Found string never translated to this value: " . $myrow_string['string_id'] . " value: " . $myrow_string['value'] . "\n"; |
| 174 | $SQL = "UPDATE translations set is_active = 0 where string_id = " . $myrow_string['string_id'] . " and language_id = '" . $language_id . "'"; |
kitlo | 2c8d8a9 | 2018-04-19 13:25:09 -0400 | [diff] [blame] | 175 | mysqli_query($dbh, $SQL); |
droy | 007252f | 2008-07-11 20:20:48 +0000 | [diff] [blame] | 176 | $SQL = "INSERT INTO translations (translation_id, string_id, language_id, version, value, possibly_incorrect, is_active, userid, created_on) |
| 177 | VALUES ( |
| 178 | NULL, " . $myrow_string['string_id'] . ", |
droy | a12ecce | 2008-11-19 19:39:08 +0000 | [diff] [blame] | 179 | " . $language_id . ", 0, '" . addslashes(unescape($tags[1])) . "', $insert_as_fuzzy, 1, " . $USER . ", NOW() |
droy | 007252f | 2008-07-11 20:20:48 +0000 | [diff] [blame] | 180 | )"; |
kitlo | 2c8d8a9 | 2018-04-19 13:25:09 -0400 | [diff] [blame] | 181 | mysqli_query($dbh, $SQL); |
droy | 007252f | 2008-07-11 20:20:48 +0000 | [diff] [blame] | 182 | # echo $SQL; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | else { |
| 190 | echo " Cannot find a file for: " . $line . "\n"; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | echo "Done.\n\n"; |
| 195 | ?> |