@ -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)
xliffTran s = loadTranslationsListsFromXliff ( masterOutputFile ) ;
mainTransList s = loadTranslationsListsFromXliff ( masterOutputFile ) ;
else
xliffTran s = new TreeMap < String , Map < String , String > > ( ) ;
mainTransList s = 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 , xliffTran s) ;
saveAsXliff ( null , masterOutputFile , mainTransList s) ;
}
/ * *
* 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 > > xliffTran s = loadTranslationsListsFromXliff ( xlifmaster ) ;
mainTransList s = 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 = xliffTran s. get ( transfilename ) ;
Map < String , String > masterList = mainTransList s. 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" ) ) ;