From f8d6543a239b3a8bda860154c8fe6915cfe9a6fa Mon Sep 17 00:00:00 2001 From: reger Date: Sun, 28 Aug 2016 23:08:03 +0200 Subject: [PATCH] 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) --- .classpath | 1 - htroot/Translator_p.java | 20 +-- ...onMasters.java => TranslationManager.java} | 115 +++++++++++++++--- 3 files changed, 108 insertions(+), 28 deletions(-) rename source/net/yacy/utils/translation/{CreateTranslationMasters.java => TranslationManager.java} (69%) diff --git a/.classpath b/.classpath index 6fc97e48f..ebace4e59 100644 --- a/.classpath +++ b/.classpath @@ -100,6 +100,5 @@ - diff --git a/htroot/Translator_p.java b/htroot/Translator_p.java index c29800274..e8d6998cd 100644 --- a/htroot/Translator_p.java +++ b/htroot/Translator_p.java @@ -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> origTrans = ctm.joinMasterTranslationLists(masterxlf, lngfile); - final File locallngfile = ctm.getScratchFile(lngfile); - Map> localTrans = ctm.loadTranslationsLists(locallngfile); // TODO: this will read file twice + if (!masterxlf.exists()) localTransMgr.createMasterTranslationLists(masterxlf); + Map> origTrans = localTransMgr.joinMasterTranslationLists(masterxlf, lngfile); + final File locallngfile = localTransMgr.getScratchFile(lngfile); + Map> 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); diff --git a/source/net/yacy/utils/translation/CreateTranslationMasters.java b/source/net/yacy/utils/translation/TranslationManager.java similarity index 69% rename from source/net/yacy/utils/translation/CreateTranslationMasters.java rename to source/net/yacy/utils/translation/TranslationManager.java index 9e08f1b2c..8cd1fd6f8 100644 --- a/source/net/yacy/utils/translation/CreateTranslationMasters.java +++ b/source/net/yacy/utils/translation/TranslationManager.java @@ -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> 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 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 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> 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>(); + mainTransLists = new TreeMap>(); List 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> joinMasterTranslationLists(File xlifmaster, File lngfile) throws IOException { final String filename = lngfile.getName(); - Map> xliffTrans = loadTranslationsListsFromXliff(xlifmaster); + mainTransLists = loadTranslationsListsFromXliff(xlifmaster); // load translation list ConcurrentLog.info("TRANSLATOR", "join into master translation file " + filename); Map> origTrans = loadTranslationsLists(lngfile); @@ -162,19 +210,51 @@ public class CreateTranslationMasters extends TranslatorXliff { for (String transfilename : origTrans.keySet()) { // get translation filename // compare translation list Map origList = origTrans.get(transfilename); - Map masterList = xliffTrans.get(transfilename); + Map 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 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 lngFiles = Translator.langFiles(new File("locales"));