1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
/**
* Translator_p
* Copyright 2012 by Michael Peter Christen, mc@yacy.net, Frankfurt am Main, Germany
* First released 14.09.2011 at http://yacy.net
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program in the file lgpl21.txt
* If not, see <http://www.gnu.org/licenses/>.
*/
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import net.yacy.cora.protocol.RequestHeader;
import net.yacy.cora.util.ConcurrentLog;
import net.yacy.data.TransactionManager;
import net.yacy.search.Switchboard;
import net.yacy.search.SwitchboardConstants;
import net.yacy.server.serverObjects;
import net.yacy.server.serverSwitch;
import net.yacy.server.servletProperties;
import net.yacy.utils.translation.TranslationManager;
public class Translator_p {
public static servletProperties respond(final RequestHeader requestHeader, final serverObjects post, final serverSwitch env) {
try {
final servletProperties prop = new servletProperties();
final Switchboard sb = (Switchboard) env;
String langcfg = sb.getConfig("locale.language", "default");
prop.put("targetlang", langcfg);
if ("default".equals(langcfg) || "browser".equals(langcfg)) {
prop.put("errmsg", 1); // msg: activate diff lng
return prop;
} else {
prop.put("errmsg", 0);
}
final File localesFolder = sb.getAppPath("locale.source", "locales");
File lngfile = new File(localesFolder, langcfg + ".lng");
TranslationManager localTransMgr = new TranslationManager(/*new File ("locales","master.lng.xlf")*/);
File masterxlf = new File(localesFolder, "master.lng.xlf");
if (!masterxlf.exists()) localTransMgr.createMasterTranslationLists(localesFolder, masterxlf);
Map<String, Map<String, String>> origTrans = localTransMgr.joinMasterTranslationLists(masterxlf, lngfile);
final File locallngfile = localTransMgr.getScratchFile(lngfile);
Map<String, Map<String, String>> localTrans = localTransMgr.loadTranslationsLists(locallngfile); // TODO: this will read file twice
int i = 0;
if (origTrans.size() > 0) {
String filename = origTrans.keySet().iterator().next();
if (post != null && post.containsKey("sourcefile")) {
filename = post.get("sourcefile", filename);
}
// prepare filename list for drop-down list
Iterator<String> filenameit = origTrans.keySet().iterator();
final boolean filteruntranslated = post == null ? false : post.getBoolean("filteruntranslated");
while (filenameit.hasNext()) {
String tmp = filenameit.next();
if (filteruntranslated) { // filter filenames with untranslated entries
Map<String, String> tmptrans = origTrans.get(tmp);
boolean hasuntrans = false;
for (String x : tmptrans.values()) {
if (x == null || x.isEmpty()) {
hasuntrans = true;
break;
}
}
if (!hasuntrans) continue;
}
prop.put("filelist_" + i + "_filename", tmp);
prop.put("filelist_" + i + "_selected", tmp.equals(filename));
i++;
}
prop.put("filelist", i);
prop.add("sourcefile", filename);
Map<String, String> origTextList = origTrans.get(filename);
i = 0;
boolean editapproved = false; // to switch already approved translation in edit mode
int textlistid = -1;
if (post != null) {
prop.put("filter.checked", filteruntranslated); // remember filter setting
textlistid = post.getInt("approve", -1);
if (post.containsKey("editapproved")) {
textlistid = post.getInt("editapproved", -1);
editapproved = true;
}
}
boolean changed = false;
for (String sourcetext : origTextList.keySet()) {
String targettxt = origTextList.get(sourcetext);
boolean existinlocalTrans = localTrans.containsKey(filename) && localTrans.get(filename).containsKey(sourcetext);
if (editapproved) existinlocalTrans |= (i == textlistid); // if edit of approved requested handle as/like existing in local to allow edit
if (targettxt == null || targettxt.isEmpty() || existinlocalTrans) {
prop.put("textlist_" + i + "_filteruntranslated", true);
} else if (filteruntranslated) {
continue;
}
// handle (modified) input text
if (i == textlistid && post != null) {
/* Check this is a valid transaction */
TransactionManager.checkPostTransaction(requestHeader, post);
if (editapproved) { // switch already translated in edit mode by copying to local translation
// not saved here as not yet modified/approved
localTransMgr.addTranslation(localTrans, filename, sourcetext, targettxt);
} else {
String t = post.get("targettxt" + Integer.toString(textlistid));
// correct common partial html markup (part of text identification for words also used as html parameter)
if (!t.isEmpty()) {
if (sourcetext.startsWith(">") && !t.startsWith(">")) t=">"+t;
if (sourcetext.endsWith("<") && !t.endsWith("<")) t=t+"<";
}
targettxt = t;
// add changes to original (for display) and local (for save)
origTextList.put(sourcetext, targettxt);
changed = localTransMgr.addTranslation(localTrans, filename, sourcetext, targettxt);
}
}
prop.putHTML("textlist_" + i + "_sourcetxt", sourcetext);
prop.putHTML("textlist_" + i + "_targettxt", targettxt);
prop.put("textlist_" + i + "_tokenid", Integer.toString(i));
prop.put("textlist_" + i + "_filteruntranslated_tokenid", Integer.toString(i));
i++;
}
if (post != null && post.containsKey("savetranslationlist")) {
changed = true;
}
if (changed) {
/* Check this is a valid transaction */
TransactionManager.checkPostTransaction(requestHeader, post);
localTransMgr.saveAsLngFile(langcfg, locallngfile, localTrans);
// adhoc translate this file
// 1. get/calc the path
final String htRootPath = env.getConfig(SwitchboardConstants.HTROOT_PATH, SwitchboardConstants.HTROOT_PATH_DEFAULT);
final File sourceDir = new File(env.getAppPath(), htRootPath);
final File destDir = new File(env.getDataPath("locale.translated_html", "DATA/LOCALE/htroot"), locallngfile.getName().substring(0, locallngfile.getName().length() - 4));// cut .lng
// get absolute file by adding relative filename from translationlist
final File sourceFile = new File(sourceDir, filename);
final File destFile = new File(destDir, filename);
localTransMgr.translateFile(sourceFile, destFile, origTextList); // do the translation
}
}
/* Acquire a transaction token for the next POST form submission */
prop.put(TransactionManager.TRANSACTION_TOKEN_PARAM, TransactionManager.getTransactionToken(requestHeader));
prop.put("textlist", i);
return prop;
} catch (IOException ex) {
ConcurrentLog.logException(ex);
}
return null;
}
}
|