blob: 8f4a2256fb3f0ac0a46ad102960cdd4130855dfd [file] [log] [blame]
droy007252f2008-07-11 20:20:48 +00001<?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
atoulme08f51af2008-11-18 12:41:33 +000019# For instructions on how to import the files, see here:
20# http://wiki.eclipse.org/Babel_/_Large_Contribution_Import_Process
droy007252f2008-07-11 20:20:48 +000021
droy03e86452008-07-18 14:04:21 +000022# To run this, copy the file to the 'root' of html/ (where translate.php resides)
droyef2077b2008-07-21 14:51:52 +000023# 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.
droy03e86452008-07-18 14:04:21 +000026
droy007252f2008-07-11 20:20:48 +000027
droya96c68b2008-11-19 19:58:49 +000028error_reporting(E_ALL); ini_set("display_errors", true);
29
30
droy007252f2008-07-11 20:20:48 +000031header("Content-type: text/plain");
droy03e86452008-07-18 14:04:21 +000032include("global.php");
droy007252f2008-07-11 20:20:48 +000033InitPage("");
34
atoulme4e313f12008-11-17 15:43:13 +000035
droy007252f2008-07-11 20:20:48 +000036$headless = 1;
37
droya12ecce2008-11-19 19:39:08 +000038# 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
droy007252f2008-07-11 20:20:48 +000046
atoulme12882d52009-01-26 18:39:17 +000047require(dirname(__FILE__) . "/../file/file.class.php");
droy007252f2008-07-11 20:20:48 +000048require_once("json_encode.php");
49
50$pageTitle = "Babel - Import Translation archive";
51$pageKeywords = "import,properties,translation,language,nlpack,pack,eclipse,babel";
52
atoulme08f51af2008-11-18 12:41:33 +000053$USER = getGenieUser()->userid;
droy007252f2008-07-11 20:20:48 +000054$PROJECT_ID = "eclipse";
55$VERSION = "3.3.1";
56
57# TODO
atoulme4e313f12008-11-17 15:43:13 +000058$indir = "/tmp/tmp-babel/import";
droy007252f2008-07-11 20:20:48 +000059chdir($indir);
60# sub-structure: ./XX/eclipse/plugins/ where XX is the iso code for the language
61exec('find . -type f', $lines);
62
63# loop through files
64foreach ($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 . "'";
kitlo2c8d8a92018-04-19 13:25:09 -040086 $rs = mysqli_query($dbh, $SQL);
kitlo1d027092018-04-19 17:44:07 -040087 if($myrow = mysqli_fetch_assoc($rs)) {
droy007252f2008-07-11 20:20:48 +000088 $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 . "'";
kitlo2c8d8a92018-04-19 13:25:09 -040097 $rs = mysqli_query($dbh, $SQL);
kitlo1d027092018-04-19 17:44:07 -040098 if($myrow = mysqli_fetch_assoc($rs)) {
droy007252f2008-07-11 20:20:48 +000099 $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]);
atoulmea44cda62008-12-15 14:56:50 +0000141 $tags[1] = str_replace("\:", ":", $tags[1]);
142 $tags[1] = str_replace("\=", "=", $tags[1]);
143 $tags[1] = str_replace("\ ", " ", $tags[1]);
droy007252f2008-07-11 20:20:48 +0000144 # echo "Doing " . $tags[0] . " with value " . $tags[1] . " Unescaped: " . unescape($tags[1]);
145
146 # Get the matching string name
droya12ecce2008-11-19 19:39:08 +0000147 $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] . "'";
kitlo2c8d8a92018-04-19 13:25:09 -0400156 $rs_string = mysqli_query($dbh, $SQL);
kitlo1d027092018-04-19 17:44:07 -0400157 $myrow_string = mysqli_fetch_assoc($rs_string);
droya12ecce2008-11-19 19:39:08 +0000158 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
droy64af78f2008-11-18 20:37:16 +0000162 ) {
droya12ecce2008-11-19 19:39:08 +0000163 $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 }
droy007252f2008-07-11 20:20:48 +0000173 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 . "'";
kitlo2c8d8a92018-04-19 13:25:09 -0400175 mysqli_query($dbh, $SQL);
droy007252f2008-07-11 20:20:48 +0000176 $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'] . ",
droya12ecce2008-11-19 19:39:08 +0000179 " . $language_id . ", 0, '" . addslashes(unescape($tags[1])) . "', $insert_as_fuzzy, 1, " . $USER . ", NOW()
droy007252f2008-07-11 20:20:48 +0000180 )";
kitlo2c8d8a92018-04-19 13:25:09 -0400181 mysqli_query($dbh, $SQL);
droy007252f2008-07-11 20:20:48 +0000182 # echo $SQL;
183 }
184 }
185 }
186 }
187 }
188 }
189 else {
190 echo " Cannot find a file for: " . $line . "\n";
191 }
192 }
193}
194echo "Done.\n\n";
195?>