Rename class CreateTranslationMaster to TranslationManager and add

additional routines and the capability to handle translation maps internally 
(to reduce complexity of handling translation maps for calling servelets)
pull/71/head
reger 8 years ago
parent 19b4509d54
commit f8d6543a23

@ -100,6 +100,5 @@
<classpathentry kind="lib" path="lib/imageio-bmp-3.2.1.jar"/>
<classpathentry kind="lib" path="lib/jsonic-1.2.0.jar"/>
<classpathentry kind="lib" path="lib/langdetect.jar"/>
<classpathentry kind="lib" path="lib/xliff-core-1.2-1.1.jar"/>
<classpathentry kind="output" path="gen"/>
</classpath>

@ -29,7 +29,7 @@ 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.CreateTranslationMasters;
import net.yacy.utils.translation.TranslationManager;
public class Translator_p {
@ -48,13 +48,13 @@ public class Translator_p {
}
File lngfile = new File(sb.getAppPath("locale.source", "locales"), langcfg + ".lng");
CreateTranslationMasters ctm = new CreateTranslationMasters(/*new File ("locales","master.lng.xlf")*/);
TranslationManager localTransMgr = new TranslationManager(/*new File ("locales","master.lng.xlf")*/);
File masterxlf = new File(sb.getAppPath("locale.source", "locales"), "master.lng.xlf");
if (!masterxlf.exists()) ctm.createMasterTranslationLists(masterxlf);
Map<String, Map<String, String>> origTrans = ctm.joinMasterTranslationLists(masterxlf, lngfile);
final File locallngfile = ctm.getScratchFile(lngfile);
Map<String, Map<String, String>> localTrans = ctm.loadTranslationsLists(locallngfile); // TODO: this will read file twice
if (!masterxlf.exists()) localTransMgr.createMasterTranslationLists(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();
@ -114,7 +114,7 @@ public class Translator_p {
if (i == textlistid && post != null) {
if (editapproved) { // switch already translated in edit mode by copying to local translation
// not saved here as not yet modified/approved
ctm.addTranslation(localTrans, filename, sourcetext, targettxt);
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)
@ -125,7 +125,7 @@ public class Translator_p {
targettxt = t;
// add changes to original (for display) and local (for save)
origTextList.put(sourcetext, targettxt);
changed = ctm.addTranslation(localTrans, filename, sourcetext, targettxt);
changed = localTransMgr.addTranslation(localTrans, filename, sourcetext, targettxt);
}
}
prop.putHTML("textlist_" + i + "_sourcetxt", sourcetext);
@ -138,7 +138,7 @@ public class Translator_p {
changed = true;
}
if (changed) {
ctm.saveAsLngFile(langcfg, locallngfile, localTrans);
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);
@ -147,7 +147,7 @@ public class Translator_p {
// get absolute file by adding relative filename from translationlist
final File sourceFile = new File(sourceDir, filename);
final File destFile = new File(destDir, filename);
ctm.translateFile(sourceFile, destFile, origTextList); // do the translation
localTransMgr.translateFile(sourceFile, destFile, origTextList); // do the translation
}
}
prop.put("textlist", i);

@ -1,4 +1,4 @@
// CreateTranslationMasters.java
// TranslationManager.java
// -------------------------------------
// part of YACY
// (C) by Michael Peter Christen; mc@yacy.net
@ -47,7 +47,30 @@ import net.yacy.data.Translator;
* Also can join existing translation with master (currently ristrictive,
* means only translation text exist in master are included in resultin Map
*/
public class CreateTranslationMasters extends TranslatorXliff {
public class TranslationManager extends TranslatorXliff {
protected Map<String, Map<String, String>> mainTransLists; // current translation entries for one language
public TranslationManager() {
super();
}
public TranslationManager(final File langfile) {
mainTransLists = loadTranslationsLists(langfile);
}
/**
* Add a translation text to the current map map
*
* @param relFileName relative filename the translation belongs to
* @param sourceLngTxt the english source text
* @param targetLngTxt the translated text
* @return true = if map was modified, otherwise false
*/
public boolean addTranslation(final String relFileName, final String sourceLngTxt, final String targetLngTxt) {
assert mainTransLists != null;
return addTranslation (mainTransLists, relFileName, sourceLngTxt, targetLngTxt);
}
/**
* Helper to add a translation text to the map
@ -79,6 +102,29 @@ public class CreateTranslationMasters extends TranslatorXliff {
return modified;
}
/**
* Get the translation list for a ui/html file
* @param filename relative path to htroot
* @return translation map or null
*/
public Map<String, String> getTranslationForFile(String filename) {
return mainTransLists.get(filename);
}
/**
* Get a translation target text
* @param filename of the translation
* @param source english source text
* @return translated text or null
*/
public String getTranslation (String filename, String source) {
Map<String, String> tmp = mainTransLists.get(filename);
if (tmp != null)
return tmp.get(source);
else
return null;
}
/**
* Create a master translation list by reading all translation files
* If a masterOutputFile exists, content is preserved (loaded first)
@ -87,11 +133,10 @@ public class CreateTranslationMasters extends TranslatorXliff {
* @throws IOException
*/
public void createMasterTranslationLists(File masterOutputFile) throws IOException {
Map<String, Map<String, String>> xliffTrans;
if (masterOutputFile.exists()) // if file exists, conserve existing master content (may be updated by external tool)
xliffTrans = loadTranslationsListsFromXliff(masterOutputFile);
mainTransLists = loadTranslationsListsFromXliff(masterOutputFile);
else
xliffTrans = new TreeMap<String, Map<String, String>>();
mainTransLists = new TreeMap<String, Map<String, String>>();
List<String> lngFiles = Translator.langFiles(new File("locales"));
for (String filename : lngFiles) {
@ -130,7 +175,7 @@ public class CreateTranslationMasters extends TranslatorXliff {
// it is possible that intentionally empty translation is given
// in this case xliff target is missing (=null)
if (origVal != null && !origVal.isEmpty()) { // if translation exists
addTranslation(xliffTrans, transfilename, sourcetxt, null); // add to master, set target text null
addTranslation(transfilename, sourcetxt, null); // add to master, set target text null
}
}
}
@ -140,11 +185,14 @@ public class CreateTranslationMasters extends TranslatorXliff {
}
}
// save as xliff file w/o language code
saveAsXliff(null, masterOutputFile, xliffTrans);
saveAsXliff(null, masterOutputFile, mainTransLists);
}
/**
* Joins translation master (xliff) and existing translation (lng)
* Joins translation master (xliff) and existing translation (lng).
* Only texts existing in master are included from the lngfile,
* the resulting map includes all keys from master with the matching translation
* from lngfile.
*
* @param xlifmaster master (with en text to be translated)
* @param lngfile existing translation
@ -154,7 +202,7 @@ public class CreateTranslationMasters extends TranslatorXliff {
public Map<String, Map<String, String>> joinMasterTranslationLists(File xlifmaster, File lngfile) throws IOException {
final String filename = lngfile.getName();
Map<String, Map<String, String>> xliffTrans = loadTranslationsListsFromXliff(xlifmaster);
mainTransLists = loadTranslationsListsFromXliff(xlifmaster);
// load translation list
ConcurrentLog.info("TRANSLATOR", "join into master translation file " + filename);
Map<String, Map<String, String>> origTrans = loadTranslationsLists(lngfile);
@ -162,19 +210,51 @@ public class CreateTranslationMasters extends TranslatorXliff {
for (String transfilename : origTrans.keySet()) { // get translation filename
// compare translation list
Map<String, String> origList = origTrans.get(transfilename);
Map<String, String> masterList = xliffTrans.get(transfilename);
Map<String, String> masterList = mainTransLists.get(transfilename);
for (String sourcetxt : origList.keySet()) {
if ((masterList != null) && (masterList.isEmpty() || masterList.containsKey(sourcetxt))) { // only if included in master (as all languages are in there but checked for occuance
String origVal = origList.get(sourcetxt);
// it is possible that intentionally empty translation is given
// in this case xliff target is missing (=null)
if (origVal != null && !origVal.isEmpty()) {
addTranslation(xliffTrans, transfilename, sourcetxt, origVal);
addTranslation(transfilename, sourcetxt, origVal);
}
}
}
}
return xliffTrans;
return mainTransLists;
}
/**
* Stores the loaded translations to a .lng file
* @param lng
* @param f
* @return
*/
public boolean saveXliff(final String lng, File f) {
return this.saveAsXliff(lng, f, mainTransLists);
}
/**
* Stores the loaded translations to a .xlf file
* @param lng
* @param f
* @return
*/
public boolean saveLng(final String lng, File f) {
return this.saveAsLngFile(lng, f, mainTransLists);
}
/**
* Total number of loaded translation entries
* @return
*/
public int size() {
int i = 0;
for (Map<String, String> trans : mainTransLists.values()) {
i += trans.size();
}
return i;
}
/**
@ -183,12 +263,13 @@ public class CreateTranslationMasters extends TranslatorXliff {
* @param args
*/
public static void main(String args[]) {
File outputdirectory = new File ("test/DATA");
CreateTranslationMasters ctm = new CreateTranslationMasters();
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 {
if (!outputdirectory.exists()) outputdirectory.mkdir();
File xlfmaster = new File(outputdirectory, "master.lng.xlf");
ctm.createMasterTranslationLists(xlfmaster); // write the language neutral translation master as xliff
List<String> lngFiles = Translator.langFiles(new File("locales"));
Loading…
Cancel
Save