From 8dc6a85055c80820f601cef82e54923c5e61aad6 Mon Sep 17 00:00:00 2001 From: Ivo Hinkelmann Date: Mon, 14 Jun 2010 18:57:10 +0200 Subject: ause121: #i112091# inputfile detection --- l10ntools/java/jpropex/java/JPropEx.java | 94 ++++++++++++++++++++++++++++---- 1 file changed, 82 insertions(+), 12 deletions(-) (limited to 'l10ntools') diff --git a/l10ntools/java/jpropex/java/JPropEx.java b/l10ntools/java/jpropex/java/JPropEx.java index d0f72d627aaf..f59113ffb1f8 100644 --- a/l10ntools/java/jpropex/java/JPropEx.java +++ b/l10ntools/java/jpropex/java/JPropEx.java @@ -43,7 +43,10 @@ public class JPropEx private boolean isQuiet = false; private final String resourceType = "javaproperties"; private final String sourceLanguage = "en-US"; - //private SdfData data; + + static final int JAVA_TYPE = 0; + static final int JAVA_ENUS_TYPE = 1; + static final int EXTENSION_TYPE = 2; public JPropEx() { @@ -178,6 +181,7 @@ public class JPropEx private void mergeFile( String filename , SdfData data , boolean isSingleFile ) { + int type = detectFormat( filename ); java.util.Properties sourceProp = loadProp( filename ); Vector langs = getLanguages( data ); HashMap props = new HashMap(); @@ -209,7 +213,7 @@ public class JPropEx mergedEntity = data.get( curEntity ); if( mergedEntity == null ) { - // if case there is not translation the fallback to the en-US source string + // in case there is no translation then fallback to the en-US source string ( (java.util.Properties) props.get( curLang )).setProperty( curEntity.getLid() , sourceString ); } else @@ -225,10 +229,10 @@ public class JPropEx for( Iterator i = props.keySet().iterator() ; i.hasNext() ; ) { lang = (String) i.next(); - writeSinglePropertiesFile( filename , (java.util.Properties) props.get( lang ) , lang , isSingleFile ); + writeSinglePropertiesFile( filename , (java.util.Properties) props.get( lang ) , lang , isSingleFile , type ); } } - private void writeSinglePropertiesFile( String filename , java.util.Properties prop , String lang , boolean isSingleFile ) + private void writeSinglePropertiesFile( String filename , java.util.Properties prop , String lang , boolean isSingleFile , int type ) { // Prepare path to file int filenameIdx = filename.lastIndexOf( "/" ) > 0 ? filename.lastIndexOf( "/" )+1 : 0 ; @@ -240,24 +244,21 @@ public class JPropEx if( pathPrefixArg != null && pathPrefixArg.length()>0 && pathPostfixArg != null && pathPostfixArg.length()>0 ) { path = new StringBuffer().append( pathPrefixArg ).append( "/" ).append( lcLang ).append( "/" ).append( pathPostfixArg ).append( "/" ).toString(); - name = new StringBuffer().append( filename.substring( filenameIdx , filename.lastIndexOf( ".properties" ) ) ) - .append( "_" ).append( lcLang.replaceAll("-","_") ).append( ".properties" ).toString(); + name += formatFilename( filename , filenameIdx , lang , type ); } //use of -i else if( !isSingleFile && outputFileArg != null && outputFileArg.length()>0 ) { - name = outputFileArg; - name += new StringBuffer().append( filename.substring( filenameIdx , filename.lastIndexOf( ".properties" ) ) ) - .append( "_" ).append( lcLang.replaceAll("-","_") ).append( ".properties" ).toString(); //name = outputFileArg; + path = outputFileArg; + name += formatFilename( filename , filenameIdx , lang , type ); } //use of -i @ else if( isSingleFile && outputFileArg != null && outputFileArg.length()>0 ) { - name = outputFileArg; - name += new StringBuffer().append( inputFileArg.substring( filenameIdx , filename.lastIndexOf( ".properties" ) ) ) - .append( "_" ).append( lcLang.replaceAll("-","_") ).append( ".properties" ).toString(); //name = outputFileArg; + path = outputFileArg; + name += formatFilename( filename , filenameIdx , lang , type ); } else { @@ -298,6 +299,75 @@ public class JPropEx } } + // we have different types of properties in the source code + // each needs a different file nameing scheme + private int detectFormat( String filename ) + { + if( filename.endsWith( "_en_US.properties" ) ) + return EXTENSION_TYPE; + else if( filename.endsWith("_en_us.properties" ) ) + return JAVA_ENUS_TYPE; + else if( filename.endsWith( ".properties" ) ) + return JAVA_TYPE; + + // Can not detect, exit + System.err.println("ERROR: Invalid file name. Only allowed (case sensitive!) *_en_US.properties , *_en_us.properties or *.properties\n"); + System.exit(-1); + return JAVA_TYPE; // dummy + } + + private String formatFilename( String filename , int filenameIdx , String lang , int type ) + { + + if( !lang.equals( "en-US" ) ) + { + // Parse iso code + int pos = lang.indexOf("-"); + String langpart1 = new String(); + String langpart2 = new String(); + if( pos == -1 ) + { + langpart1 = lang; + } + else if( pos > 0 ) + { + langpart1 = lang.substring( 0 , pos ); + langpart2 = lang.substring( pos+1 , lang.length() ); + } + // change filename according to the type + switch( type ) + { + // -> de_DE + case EXTENSION_TYPE: + lang = langpart1.toLowerCase(); + if( langpart2.length() > 0 ) // -> en_US + lang += "_" + langpart2.toUpperCase(); + else // -> de_DE + lang += "_" + langpart1.toUpperCase(); + return new StringBuffer().append( filename.substring( filenameIdx , filename.lastIndexOf( "_en_US.properties" ) ) ) + .append( "_" ).append( lang.replaceAll("-","_") ).append( ".properties" ).toString(); + // -> de + case JAVA_ENUS_TYPE: + lang = langpart1.toLowerCase(); + if( langpart2.length() > 0 ) + lang += "_" + langpart2.toLowerCase(); + return new StringBuffer().append( filename.substring( filenameIdx , filename.lastIndexOf( "_en_us.properties" ) ) ) + .append( "_" ).append( lang.replaceAll("-","_") ).append( ".properties" ).toString(); + // -> de + case JAVA_TYPE: + lang = langpart1.toLowerCase(); + if( langpart2.length() > 0 ) + lang += "_" + langpart2.toLowerCase(); + return new StringBuffer().append( filename.substring( filenameIdx , filename.lastIndexOf( ".properties" ) ) ) + .append( "_" ).append( lang.replaceAll("-","_") ).append( ".properties" ).toString(); + default: + System.err.println("ERROR: Something is really broken here, l10ntools/java/jprop/java/JPropEx.java :: formatFilename()"); + System.exit( -1 ); + break; + } + } + return filename; // don't change en-US source file name + } private SdfData getSdfData() { SdfData data = new SdfData( inputSdfFileArg ); -- cgit v1.2.3