Adjust TranslatorXliff to load default 1st and merge downloaded or modified local translation.

process 1. load default from locales/*.* 
        2. load and merge(overwrite) from DATA/LOCALE/*.* (can be partial translation as it is merged)
- include all entries from DATA/LOCAL to be edited in Translator servlet
  and save just modifications (instead of full list) to DATA/LOCALE

This shall make it easy to share modifications.
pull/58/head
reger 9 years ago
parent 02a0b28ce0
commit 5b22c63030

@ -52,6 +52,8 @@ public class Translator_p {
File masterxlf = new File("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
int i = 0;
if (origTrans.size() > 0) {
String filename = origTrans.keySet().iterator().next();
@ -87,7 +89,8 @@ public class Translator_p {
boolean changed = false;
for (String sourcetext : origTextList.keySet()) {
String targettxt = origTextList.get(sourcetext);
if (targettxt == null || targettxt.isEmpty()) {
boolean existinlocalTrans = localTrans.containsKey(filename) && localTrans.get(filename).containsKey(sourcetext);
if (targettxt == null || targettxt.isEmpty() || existinlocalTrans) {
prop.put("textlist_" + i + "_filteruntranslated", true);
} else if (filteruntranslated) {
continue;
@ -100,8 +103,9 @@ public class Translator_p {
if (sourcetext.endsWith("<") && !t.endsWith("<")) t=t+"<";
}
targettxt = t;
// add changes to original (for display) and local (for save)
origTextList.put(sourcetext, targettxt);
changed = true;
changed = ctm.addTranslation (localTrans, filename, sourcetext, targettxt);
}
prop.putHTML("textlist_" + i + "_sourcetxt", sourcetext);
prop.putHTML("textlist_" + i + "_targettxt", targettxt);
@ -113,7 +117,7 @@ public class Translator_p {
changed = true;
}
if (changed) {
ctm.saveAsLngFile(langcfg, ctm.getScratchFile(lngfile), origTrans);
ctm.saveAsLngFile(langcfg, locallngfile, localTrans);
}
}
prop.put("textlist", i);

@ -58,7 +58,7 @@ public class CreateTranslationMasters extends TranslatorXliff {
* @param targetLngTxt the translated text
* @return true = if map was modified, otherwise false
*/
protected boolean addTranslation(Map<String, Map<String, String>> translation, final String relFileName, final String sourceLngTxt, final String targetLngTxt) {
public boolean addTranslation(Map<String, Map<String, String>> translation, final String relFileName, final String sourceLngTxt, final String targetLngTxt) {
boolean modified = false;
Map<String, String> transFile;
@ -156,7 +156,7 @@ public class CreateTranslationMasters extends TranslatorXliff {
final String filename = lngfile.getName();
Map<String, Map<String, String>> xliffTrans = loadTranslationsListsFromXliff(xlifmaster);
// load translation list
System.out.println("join into master translation file " + filename);
ConcurrentLog.info("TRANSLATOR", "join into master translation file " + filename);
Map<String, Map<String, String>> origTrans = loadTranslationsLists(lngfile);
for (String transfilename : origTrans.keySet()) { // get translation filename

@ -55,8 +55,11 @@ import org.oasis.xliff.core_12.Xliff;
* Wordlist based translator
*
* Translator which can read and write translation lists from a
* <a href="http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html">XLIFF
* 1.2</a> file with phrases or single words to translate a string or a file
* <a href="http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html">XLIFF 1.2</a>
* file with phrases or single words to translate a string or a file.
*
* On loading of translation files loaded data is merged with local (modified or downloaded)
* translation data in DATA/LOCALE/
*/
public class TranslatorXliff extends Translator {
@ -140,21 +143,56 @@ public class TranslatorXliff extends Translator {
/**
* Maps (overrides) Translator.loadTranslationsLists to read from xliff file
* if file extension is .xlf or .xliff (otherwise load xx.lng file)
* if file extension is .xlf or .xliff (otherwise load xx.lng file).
* Additionally if localy modified translation exists in DATA/LOCALE content
* is merged into given translation.
*
* @param xliffFile
* @return translatio map
* @return translation map
*/
@Override
public Map<String, Map<String, String>> loadTranslationsLists(final File xliffFile) {
File locallng = getScratchFile(xliffFile);
if (xliffFile.getName().toLowerCase().endsWith(".xlf") || xliffFile.getName().toLowerCase().endsWith(".xliff")) {
return locallng.exists() ? loadTranslationsListsFromXliff(locallng) : loadTranslationsListsFromXliff(xliffFile);
if (locallng.exists()) {
Map<String, Map<String, String>> mergedList = loadTranslationsListsFromXliff(xliffFile);
Map<String, Map<String, String>> tmplist = loadTranslationsListsFromXliff(locallng);
return mergeTranslationLists(mergedList, tmplist);
} else {
return loadTranslationsListsFromXliff(xliffFile);
}
} else if (locallng.exists()) {
Map<String, Map<String, String>> mergedList = super.loadTranslationsLists(xliffFile);
Map<String, Map<String, String>> tmplist = super.loadTranslationsLists(locallng);
return mergeTranslationLists(mergedList, tmplist);
} else {
return locallng.exists() ? super.loadTranslationsLists(locallng) : super.loadTranslationsLists(xliffFile);
return super.loadTranslationsLists(xliffFile);
}
}
/**
* Merges translations, values from localTrans overwrite entries in masterTrans.
*
* @param masterTrans master translation
* @param localTrans translation to be merged to master
* @return resulting map with all entries from master and localTrans
*/
protected Map<String, Map<String, String>> mergeTranslationLists(Map<String, Map<String, String>> masterTrans, Map<String, Map<String, String>> localTrans) {
if (localTrans != null && !localTrans.isEmpty()) {
for (String transfilename : localTrans.keySet()) { // get translation filename
Map<String, String> origList = localTrans.get(transfilename);
if (masterTrans.containsKey(transfilename)) {
Map<String, String> xliffList = masterTrans.get(transfilename);
xliffList.putAll(origList);
} else {
masterTrans.put(transfilename, origList);
}
}
}
return masterTrans;
}
/**
* Saves the internal translation map as XLIFF 1.2 file
*
@ -221,23 +259,24 @@ public class TranslatorXliff extends Translator {
* @throws IOException
*/
private void writeFileSection(final String filename, final Map<String, String> textlist, OutputStreamWriter output) throws IOException {
output.write("#File: " + filename + "\n"
+ "#---------------------------\n"); // required in 1.2
if (!filename.isEmpty()) {
output.write("#File: " + filename + "\n"
+ "#---------------------------\n");
for (String source : textlist.keySet()) {
String target = textlist.get(source);
// we use hashCode of source string to get same id in different xliff files for same translation text
if (target != null && !target.isEmpty()) { // omitt target text if not available
if (source.equals(target)) {
output.write("#" + source + "==" + target + "\n"); // no translation needed (mark #)
for (String source : textlist.keySet()) {
String target = textlist.get(source);
if (target != null && !target.isEmpty()) { // omitt target text if not available
if (source.equals(target)) {
output.write("#" + source + "==" + target + "\n"); // no translation needed (mark #)
} else {
output.write(source + "==" + target + "\n");
}
} else {
output.write(source + "==" + target + "\n");
output.write("#" + source + "==" + source + "\n"); // no translation available (mark #)
}
} else {
output.write("#"+source + "==" + source + "\n"); // no translation available (mark #)
}
output.write("#-----------------------------\n\n");
}
output.write("#-----------------------------\n\n");
}
/**
@ -267,7 +306,7 @@ public class TranslatorXliff extends Translator {
// special handling of "ConfigLanguage_p.html" to list on top of all other
// because of some important identifier
Map<String, String> txtmap = lng.get("ConfigLanguage_p.html");
writeFileSection("ConfigLanguage_p.html", txtmap, output);
if (txtmap != null) writeFileSection("ConfigLanguage_p.html", txtmap, output);
for (String afilemap : lng.keySet()) {
txtmap = lng.get(afilemap);

Loading…
Cancel
Save