1
package freeguide.build.i18n;
4
import java.io.FileFilter;
5
import java.io.FileInputStream;
6
import java.io.IOException;
7
import java.io.InputStreamReader;
9
import java.util.ArrayList;
10
import java.util.Iterator;
11
import java.util.List;
13
import java.util.Properties;
15
import java.util.TreeSet;
16
import java.util.regex.Pattern;
24
public class CheckLocalization
26
protected static Pattern PATTERN_MAIN =
27
Pattern.compile( ".+[A-Za-z]{3,}\\.properties" );
28
protected static Map<String, String> translatedGlobal;
33
* @param args DOCUMENT_ME!
35
* @throws Exception DOCUMENT_ME!
37
public static void main( String[] args ) throws Exception
39
final File mainProps =
40
new File( "src/resources/i18n/MessagesBundle.properties" );
41
final File[] propFiles =
42
new File( "src/resources/i18n/" ).listFiles(
45
public boolean accept( File pathname )
47
return PATTERN_MAIN.matcher( pathname.getName( ) )
52
translatedGlobal = readPropertiesFile( mainProps );
55
"================= Files with untranslated strings =================" );
57
for( final File f : propFiles )
59
processFileFindUntranslated( f );
63
"================= Translation with non-exists strings =================" );
65
for( final File f : propFiles )
67
processFileFindUnused( f );
70
for( final String lang : "be,de,it,fr".split( "," ) )
73
"================= Language info: " + lang
74
+ " =================" );
76
for( final File f : propFiles )
78
processLang( lang, f );
83
protected static void processLang( final String lang, final File propFile )
86
final Map<String, String> baseStrings = readPropertiesFile( propFile );
87
final File transFile =
91
"\\.properties", '_' + lang + ".properties" ) );
93
if( !transFile.exists( ) )
96
" There is no file : " + transFile.getPath( ) );
101
final Map<String, String> translatedStrings =
102
readPropertiesFile( transFile );
104
final Set<String> untranslated =
105
new TreeSet<String>( baseStrings.keySet( ) );
106
removeFromSet( untranslated, translatedStrings.keySet( ) );
108
if( untranslated.size( ) > 0 )
111
" Untranslated strings in " + propFile.getPath( )
112
+ " for language '" + lang + "':" );
114
for( String str : untranslated )
116
System.out.println( " \"" + str + '"' );
120
final Set<String> unused =
121
new TreeSet<String>( translatedStrings.keySet( ) );
122
removeFromSet( unused, baseStrings.keySet( ) );
124
if( unused.size( ) > 0 )
127
" Unused translations in " + propFile.getPath( )
128
+ " for language '" + lang + "':" );
130
for( String str : unused )
132
System.out.println( " \"" + str + '"' );
137
protected static File[] getJavaFilesForPropertiesFile(
138
final File propFile )
140
final List<File> allFiles = new ArrayList<File>( );
142
String classesDir = propFile.getName( );
143
classesDir = classesDir.substring(
144
0, classesDir.length( ) - ".properties".length( ) );
145
classesDir = classesDir.replace( '_', '/' );
147
if( classesDir.equals( "MessagesBundle" ) )
149
classesDir = "src/freeguide/common/";
151
new File( classesDir ), allFiles,
154
public boolean accept( File pathname )
156
return pathname.getName( ).endsWith( ".java" );
159
classesDir = "src/freeguide/plugins/program/";
161
new File( classesDir ), allFiles,
164
public boolean accept( File pathname )
166
return pathname.getName( ).endsWith( ".java" );
172
classesDir = "src/freeguide/plugins/" + classesDir;
174
new File( classesDir ), allFiles,
177
public boolean accept( File pathname )
179
return pathname.getName( ).endsWith( ".java" );
184
return allFiles.toArray( new File[allFiles.size( )] );
187
protected static String readFile( final File f ) throws IOException
189
final StringBuffer buf = new StringBuffer( );
190
final InputStreamReader in =
191
new InputStreamReader( new FileInputStream( f ), "UTF-8" );
195
char[] buffer = new char[65536];
199
int len = in.read( buffer );
206
buf.append( buffer, 0, len );
214
return buf.toString( );
217
protected static Map<String, String> readPropertiesFile( final File f )
220
final Properties result = new Properties( );
221
result.load( new FileInputStream( f ) );
226
protected static Set<String> getStringsForTranslationFromFile(
227
final File f ) throws IOException
229
MODE mode = MODE.DATA;
230
final Set<String> result = new TreeSet<String>( );
232
StringBuffer str = new StringBuffer( );
233
StringBuffer command = new StringBuffer( );
236
final String data = readFile( f );
238
for( int i = 0; i < data.length( ); i++ )
240
char c = data.charAt( i );
246
if( ( prevC == '/' ) && ( c == '*' ) )
250
else if( ( prevC == '/' ) && ( c == '/' ) )
252
mode = MODE.LINE_COMMENT;
254
else if( ( c == '"' ) && ( prevC != '\'' ) )
263
if( ( prevC == '*' ) && ( c == '/' ) )
266
command.setLength( 0 );
282
if( ( c == '"' ) && ( prevC != '\\' ) )
286
if( isNeedToLocalize( command.toString( ) ) )
288
result.add( str.toString( ) );
301
if( mode == MODE.DATA )
303
if( ( c == ';' ) || ( c == '{' ) || ( c == '}' ) )
305
command.setLength( 0 );
319
protected static boolean isNeedToLocalize( final String command )
321
if( command.contains( "static" ) && command.contains( "final" ) )
326
if( command.contains( "Exception(" ) )
332
command.replaceAll( "\\s+", "" )
333
.contains( "Application.getInstance().getLogger()." ) )
341
protected static void findFile(
342
final File dir, final List<File> files, final FileFilter filter )
344
final File[] fs = dir.listFiles( );
351
for( final File f : fs )
355
if( filter.accept( f ) )
362
if( !f.getName( ).equals( ".svn" ) )
364
findFile( f, files, filter );
370
protected static void processFileFindUntranslated( final File propFile )
373
System.out.println( "Translation '" + propFile.getName( ) + "' :" );
375
final Map<String, String> translated = readPropertiesFile( propFile );
377
for( final File f : getJavaFilesForPropertiesFile( propFile ) )
379
final Set<String> forTranslation =
380
getStringsForTranslationFromFile( f );
381
removeFromSet( forTranslation, translated.keySet( ) );
382
removeFromSet( forTranslation, translatedGlobal.keySet( ) );
384
if( forTranslation.size( ) > 0 )
387
" Untranslated strings in " + f.getPath( ) + " :" );
389
for( String str : forTranslation )
391
System.out.println( " \"" + str + '"' );
397
protected static void processFileFindUnused( final File propFile )
400
final Map<String, String> translated = readPropertiesFile( propFile );
401
final Set<String> keys = translated.keySet( );
403
for( final File f : getJavaFilesForPropertiesFile( propFile ) )
405
final Set<String> forTranslation =
406
getStringsForTranslationFromFile( f );
408
removeFromSet( keys, forTranslation );
411
if( keys.size( ) > 0 )
414
"Unused strings in '" + propFile.getName( ) + "' :" );
416
for( String str : keys )
418
System.out.println( " \"" + str + '"' );
423
protected static void removeFromSet(
424
final Set<String> sourceSet, final Set<String> removeSet )
426
for( final Iterator<String> it = sourceSet.iterator( );
429
if( removeSet.contains( it.next( ) ) )
435
protected static enum MODE