TransNews_p: add ad-hoc translation of target file on positive vote (additon to local translation)

+ errmsg on language=default
pull/71/head
reger 9 years ago
parent 02129a04dd
commit 5e72d37f0a

@ -17,6 +17,7 @@
&nbsp;&nbsp;<small>You can check your outgoing messages <a href="News.html?page=3">here</a></small>
</p>
</form>
#(errmsg)#::<p class="error">Please activate a different language <a href='ConfigBasic.html'>here</a></p>#(/errmsg)#
#{results}#
<!-- link begin -->

@ -52,15 +52,28 @@ public class TransNews_p {
String currentlang = sb.getConfig("locale.language", "default");
prop.put("currentlang", currentlang);
TranslatorXliff transx = new TranslatorXliff();
File langFile = transx.getScratchFile(new File(currentlang + ".lng"));
TranslationManager trans = new TranslationManager(langFile);
prop.put("transsize", trans.size());
if ("default".equals(currentlang) || "browser".equals(currentlang)) {
prop.put("errmsg", 1); // msg: activate diff lng
prop.put("transsize", 0);
return prop;
} else {
prop.put("errmsg", 0);
}
TranslationManager transMgr = new TranslationManager();
File locallangFile = transMgr.getScratchFile(new File(currentlang + ".lng"));
Map<String, Map<String, String>> localTrans = transMgr.loadTranslationsLists(locallangFile);
// calculate size of local translations list
int size = 0;
for (Map<String, String> lst : localTrans.values()) {
size += lst.size();
}
prop.put("transsize", size);
// read voting
if ((post != null) && post.containsKey("publishtranslation")) {
Map<String, Map<String, String>> localTrans = trans.loadTranslationsLists(langFile);
Iterator<String> filenameit = localTrans.keySet().iterator();
while (filenameit.hasNext()) {
String file = filenameit.next();
@ -77,7 +90,7 @@ public class TransNews_p {
continue;
}
if (NewsPool.CATEGORY_TRANSLATION_ADD.equals(rtmp.category())) {
String tmplng = rtmp.attribute("language", null);
//String tmplng = rtmp.attribute("language", null);
String tmpfile = rtmp.attribute("file", null);
String tmpsource = rtmp.attribute("source", null);
String tmptarget = rtmp.attribute("target", null);
@ -143,22 +156,22 @@ public class TransNews_p {
}
if ((post != null) && ((refid = post.get("votePositive", null)) != null)) {
if (!sb.verifyAuthentication(header)) {
prop.authenticationRequired();
return prop;
}
// add to local translation extension
if (trans.addTranslation(post.get("filename"), post.get("source"), post.get("target"))) {
File f = new File(currentlang + ".lng");
f = trans.getScratchFile(f);
trans.saveLng(currentlang, f);
}
final String filename = post.get("filename");
File lngfile = new File(sb.getAppPath("locale.source", "locales"), currentlang + ".lng");
transMgr = new TranslationManager(lngfile); // load full language for check if entry is new (globally)
if (transMgr.addTranslation(filename, post.get("source"), post.get("target"))) {
// add to local translation extension
transMgr.addTranslation(localTrans, filename, post.get("source"), post.get("target"));
transMgr.saveAsLngFile(currentlang, locallangFile, localTrans); // save local-trans to local-file
transMgr.translateFile(filename); // ad-hoc translate file with new/added text
} // TODO: shall we post voting if translation is not new ?
// make new news message with voting
final HashMap<String, String> map = new HashMap<String, String>();
map.put("language", currentlang);
map.put("file", crypt.simpleDecode(post.get("filename", "")));
map.put("file", crypt.simpleDecode(filename));
map.put("source", crypt.simpleDecode(post.get("source", "")));
map.put("target", crypt.simpleDecode(post.get("target", "")));
map.put("vote", "positive");
@ -168,6 +181,7 @@ public class TransNews_p {
sb.peers.newsPool.moveOff(NewsPool.INCOMING_DB, refid);
} catch (IOException | SpaceExceededException ex) {
}
}
// create Translation voting list
@ -187,7 +201,7 @@ public class TransNews_p {
String target;
while (k.hasNext()) {
String existingtarget = null;
refid = k.next();
if (refid == null) {
continue;
@ -206,7 +220,12 @@ public class TransNews_p {
continue;
}
existingtarget = trans.getTranslation(filename, source);
String existingtarget = null; //transMgr.getTranslation(filename, source);
Map<String, String> tmpMap = localTrans.get(filename);
if (tmpMap != null) {
existingtarget = tmpMap.get(source);
}
boolean altexist = existingtarget != null && !target.isEmpty() && !existingtarget.isEmpty() && !existingtarget.equals(target);

@ -40,6 +40,8 @@ import java.util.Map;
import java.util.TreeMap;
import net.yacy.cora.util.ConcurrentLog;
import net.yacy.data.Translator;
import net.yacy.search.Switchboard;
import net.yacy.search.SwitchboardConstants;
/**
* Utility to create a translation master file from all existing translation
@ -50,6 +52,7 @@ import net.yacy.data.Translator;
public class TranslationManager extends TranslatorXliff {
protected Map<String, Map<String, String>> mainTransLists; // current translation entries for one language
protected String loadedLng; // language loaded in mainTransLists (2-letter code)
public TranslationManager() {
super();
@ -57,6 +60,10 @@ public class TranslationManager extends TranslatorXliff {
public TranslationManager(final File langfile) {
mainTransLists = loadTranslationsLists(langfile);
int pos = langfile.getName().indexOf('.');
if (pos >= 0) {
loadedLng = langfile.getName().substring(0, pos);
}
}
/**
@ -125,6 +132,35 @@ public class TranslationManager extends TranslatorXliff {
return null;
}
/**
* Translates one file. The relFilepath is the file name as given in the
* translation source lists. The source (english) file is expected under
* htroot path. The destination file is under DATA/LOCALE and calculated
* using the language of loaded data.
*
* @param relFilepath file name releative to htroot
* @return true on success
*/
public boolean translateFile(String relFilepath) {
assert loadedLng != null;
assert mainTransLists != null;
boolean result = false;
if (mainTransLists.containsKey(relFilepath)) {
Switchboard sb = Switchboard.getSwitchboard();
if (sb != null) {
final String htRootPath = sb.getConfig(SwitchboardConstants.HTROOT_PATH, SwitchboardConstants.HTROOT_PATH_DEFAULT);
final File sourceDir = new File(sb.getAppPath(), htRootPath);
final File destDir = new File(sb.getDataPath("locale.translated_html", "DATA/LOCALE/htroot"), loadedLng);
// get absolute file by adding relative filename
final File sourceFile = new File(sourceDir, relFilepath);
final File destFile = new File(destDir, relFilepath);
result = translateFile(sourceFile, destFile, mainTransLists.get(relFilepath)); // do the translation
}
}
return result;
}
/**
* Create a master translation list by reading all translation files
* If a masterOutputFile exists, content is preserved (loaded first)
@ -256,33 +292,4 @@ public class TranslationManager extends TranslatorXliff {
}
return i;
}
/**
* for testing to create on master and joined translation results for all lang's
*
* @param args
*/
public static void main(String args[]) {
File outputdirectory = new File("test/DATA");
if (!outputdirectory.exists()) {
outputdirectory.mkdir();
}
File xlfmaster = new File(outputdirectory, "master.lng.xlf");
TranslationManager ctm = new TranslationManager(xlfmaster);
try {
ctm.createMasterTranslationLists(xlfmaster); // write the language neutral translation master as xliff
List<String> lngFiles = Translator.langFiles(new File("locales"));
for (String filename : lngFiles) {
Map<String, Map<String, String>> lngmaster = ctm.joinMasterTranslationLists(xlfmaster, new File("locales", filename)); // create individual language translation files from master
File xlftmp = new File(outputdirectory, filename + ".xlf");
System.out.println("output new master translation file " + xlftmp.toString() + " and " + filename);
ctm.saveAsXliff(filename.substring(0, 2), xlftmp, lngmaster);
ctm.saveAsLngFile(filename.substring(0, 2), new File(outputdirectory, filename), lngmaster);
}
} catch (IOException ex) {
ConcurrentLog.logException(ex);
}
ConcurrentLog.shutdown();
}
}

Loading…
Cancel
Save