Révision 1223

tmp/org.txm.translate.rcp/.settings/org.eclipse.jdt.groovy.core.prefs (revision 1223)
1
eclipse.preferences.version=1
2
groovy.compiler.level=25
0 3

  
tmp/org.txm.translate.rcp/.classpath (revision 1223)
7 7
		</accessrules>
8 8
	</classpathentry>
9 9
	<classpathentry kind="src" path="src"/>
10
	<classpathentry exported="true" kind="con" path="GROOVY_SUPPORT"/>
11
	<classpathentry exported="true" kind="con" path="GROOVY_DSL_SUPPORT"/>
10 12
	<classpathentry kind="output" path="bin"/>
11 13
</classpath>
tmp/org.txm.translate.rcp/.project (revision 1223)
22 22
		</buildCommand>
23 23
	</buildSpec>
24 24
	<natures>
25
		<nature>org.eclipse.jdt.groovy.core.groovyNature</nature>
25 26
		<nature>org.eclipse.pde.PluginNature</nature>
26 27
		<nature>org.eclipse.jdt.core.javanature</nature>
27 28
	</natures>
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/FindUnusedTranslations.java (revision 1223)
1
package org.txm.rcp.translate;
2

  
3
public class FindUnusedTranslations {
4

  
5
}
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/FindMissingTranslations.java (revision 1223)
1
package org.txm.rcp.translate;
2

  
3
public class FindMissingTranslations {
4

  
5
}
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/devtools/ExportToOneFile.java (revision 1223)
1
package org.txm.rcp.translate.devtools;
2

  
3
public class ExportToOneFile {
4

  
5
}
0 6

  
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/devtools/FindUnusedTranslations.java (revision 1223)
1
package org.txm.rcp.translate.devtools;
2

  
3
public class FindUnusedTranslations {
4

  
5
}
0 6

  
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/devtools/FindMissingTranslations.java (revision 1223)
1
package org.txm.rcp.translate.devtools;
2

  
3
public class FindMissingTranslations {
4

  
5
}
0 6

  
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/devtools/MoveDedicatedStringsToPlugins.java (revision 1223)
1
package org.txm.rcp.translate.devtools;
2

  
3
public class MoveDedicatedStringsToPlugins {
4

  
5
}
0 6

  
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/devtools/NormalizeKeys.java (revision 1223)
1
package org.txm.rcp.translate.devtools;
2

  
3
import org.eclipse.osgi.util.NLS;
4

  
5
/**
6
 * Normalizes the keys using the message words
7
 * 
8
 * @author mdecorde
9
 *
10
 */
11
public class NormalizeKeys {
12
	static {
13
		
14
	}
15
	
16
	public static void main(String[] args) {
17
		System.out.println(NLS.bind("lol {0}", 1));
18
	}
19
}
0 20

  
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/devtools/MergeDuplicatedStringsInAPlugin.java (revision 1223)
1
package org.txm.rcp.translate.devtools;
2

  
3
public class MergeDuplicatedStringsInAPlugin {
4

  
5
}
0 6

  
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/devtools/FindUnusedKeys.java (revision 1223)
1
package org.txm.rcp.translate.devtools;
2

  
3
public class FindUnusedKeys {
4

  
5
}
0 6

  
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/devtools/ImportFromOneFile.java (revision 1223)
1
package org.txm.rcp.translate.devtools;
2

  
3
/**
4
 * 
5
 * @author mdecorde
6
 *
7
 *	Reads one properties file per lang and uses the key to dispatch the strings in the right files
8
 *
9
 */
10
public class ImportFromOneFile {
11

  
12
}
0 13

  
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/devtools/FindCommonMessages.java (revision 1223)
1
package org.txm.rcp.translate.devtools;
2

  
3
public class FindCommonMessages {
4

  
5
}
0 6

  
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/i18n/ExternalizationFilesUpdater.groovy (revision 1223)
1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
// 
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
// 
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
// 
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21

  
22
//
23
// This file is part of the TXM platform.
24
//
25
// The TXM platform is free software: you can redistribute it and/or modif y
26
// it under the terms of the GNU General Public License as published by
27
// the Free Software Foundation, either version 3 of the License, or
28
// (at your option) any later version.
29
//
30
// The TXM platform is distributed in the hope that it will be useful,
31
// but WITHOUT ANY WARRANTY; without even the implied warranty of
32
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33
// GNU General Public License for more details.
34
//
35
// You should have received a copy of the GNU General Public License
36
// along with the TXM platform.  If not, see <http://www.gnu.org/licenses/>.
37
// 
38
// 
39
// 
40
// $LastChangedDate: 2013-07-25 16:24:13 +0200 (jeu. 25 juil. 2013) $
41
// $LastChangedRevision: 2490 $
42
// $LastChangedBy: mdecorde $ 
43
//
44
package org.txm.rcp.translate.i18n;
45

  
46
import java.io.BufferedReader;
47
import java.io.File;
48
import java.io.FileInputStream;
49
import java.io.InputStreamReader;
50
import java.util.LinkedList;
51

  
52
// TODO: Auto-generated Javadoc
53
/**
54
 * Tool to update the key of externalization files xxxx_fr.properties
55
 * adds _NA to missing keys
56
 * 
57
 * @author mdecorde
58
 * 
59
 */
60
public class ExternalizationFilesUpdater {
61

  
62
	/** The dirfiles. */
63
	LinkedList<File> dirfiles = new LinkedList<File>();
64
	
65
	/** The propertyfiles. */
66
	HashMap<String, File> propertyfiles = new HashMap<String, File>();
67
	
68
	/** The fileentries. */
69
	HashMap<File, List<String>> fileentries = new HashMap<String, List<String>>();
70
	
71
	/** The fileentriesvalues. */
72
	HashMap<File, HashMap<String, String>> fileentriesvalues = new HashMap<String, HashMap<String, String>>();
73

  
74
	/**
75
	 * Gets the entries.
76
	 *
77
	 * @param file the file
78
	 * @return the entries
79
	 */
80
	public List<String> getEntries(File file)
81
	{
82
		List<String> entries = [];
83
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file) , "iso-8859-1"));
84
		String line = reader.readLine();
85
		while (line != null) {
86
			String[] split = line.split("=", 2);
87
			if (split.length > 0) {
88
				String key = split[0];
89
				entries.add(key);
90
			}
91
			line = reader.readLine();
92
		}
93
		return entries;
94
	}
95

  
96
	/**
97
	 * Gets the values.
98
	 *
99
	 * @param file the file
100
	 * @return the values
101
	 */
102
	public HashMap<String, String> getValues(File file) {
103
		HashMap<String, String> values = new HashMap<String, String>();
104
		BufferedReader reader = new BufferedReader(new InputStreamReader(
105
				new FileInputStream(file), "iso-8859-1"));
106
		String line = reader.readLine();
107
		while (line != null) {
108
			String[] split = line.split("=");
109
			if (split.length > 0) {
110
				String key = split[0];
111
				String concat = "";
112
				for (int i = 1; i < split.length; i++)
113
					concat += split[i];
114
				values.put(key, concat);
115
			}
116
			line = reader.readLine();
117
		}
118
		return values;
119
	}
120

  
121
	/**
122
	 * Process found directories.
123
	 */
124
	public void processFoundDirectories()
125
	{
126
		for (String dirfile : dirfiles) {
127
			println "DIRECTORY : "+dirfile
128
			List<File> files = propertyfiles.get(dirfile)
129
			for (File f : files) {
130
				this.fileentries.put(f, getEntries(f));
131
				this.fileentriesvalues.put(f, getValues(f));
132
			}
133
			File reference = null;
134
			if (files.get(0).getName().startsWith("m")) {
135
				reference = new File(dirfile,"messages.properties");
136
			}
137
			else if (files.get(0).getName().startsWith("b"))
138
				reference = new File(dirfile,"bundle.properties");
139
			
140
			if (reference != null && reference.exists()) {
141
				for (File f : files) {
142
					if (f != reference) {
143
						List<String> refentries = this.fileentries.get(reference).clone();
144
						List<String> tmp1 = this.fileentries.get(f).clone();
145
						tmp1.removeAll(refentries);
146
						
147
						refentries = this.fileentries.get(reference).clone();
148
						List<String> tmp = this.fileentries.get(f);
149
						refentries.removeAll(tmp);
150
						for (String missing : refentries) {
151
							this.fileentriesvalues.get(f).put(missing,"N/A_"+this.fileentriesvalues.get(reference).get(missing)); // put entry's value
152
						}
153
						
154
						this.fileentries.put(f, this.fileentries.get(reference)); // update file entries
155
						
156
						if (tmp1.size() > 0 || refentries.size() > 0)
157
							println " "+f.getName()
158
						if (tmp1.size() > 0)
159
							println "  Removed keys : "+tmp1;
160
						if (refentries.size() > 0)
161
							println "  Added keys : "+refentries;
162
					}
163
				}
164
			}
165
		}
166
	}
167

  
168
	/**
169
	 * Update files.
170
	 */
171
	public void updateFiles()
172
	{
173
		for(String dirfile : dirfiles)
174
		{
175
			List<File> files = propertyfiles.get(dirfile);
176
			for(File f : files)
177
			{
178
				List<String> entries =  this.fileentries.get(f);
179
				HashMap<String, String> values = this.fileentriesvalues.get(f);
180
				
181
				Writer writer = new OutputStreamWriter(new FileOutputStream(f) , "iso-8859-1");
182
				for(String entry : entries)
183
				{
184
					writer.write(entry+"="+values.get(entry)+"\n");
185
				}
186
				writer.close()
187
			}
188
		}
189
	}
190

  
191
	/**
192
	 * Creates the missing files.
193
	 *
194
	 * @param suffix the suffix
195
	 */
196
	public void createMissingFiles(String suffix)
197
	{
198
		println "Looking for missing messages files "+ suffix;
199
		for(String dirfile : dirfiles)
200
		{
201
			//println "DIRECTORY : "+dirfile
202
			File reference = null;
203
			String lookingname = "";
204
			List<File> files = propertyfiles.get(dirfile)
205
			if(files.get(0).getName().startsWith("m"))
206
			{
207
				reference = new File(dirfile,"messages.properties");
208
				lookingname = "messages_"+suffix+".properties"
209
			}
210
			else if (files.get(0).getName().startsWith("b"))
211
			{
212
				reference = new File(dirfile,"bundle.properties");
213
				lookingname = "bundle_"+suffix+".properties"
214
			}
215
			
216
			boolean create = true;
217
			if(reference != null && reference.exists())
218
			{
219
				for(File f : files)
220
				{
221
					if(f.getName() == lookingname)
222
						create = false;
223
				}
224
			}
225
			if(create)
226
			{
227
				new File(dirfile,lookingname).createNewFile();
228
				println "Create file " +new File(dirfile,lookingname)
229
			}
230
		}
231
	}
232

  
233
	/* (non-Javadoc)
234
	 * @see java.lang.Object#toString()
235
	 */
236
	public String toString()
237
	{
238
		String rez = "";
239
		for(String dirfile : dirfiles)
240
		{
241
			rez += dirfile+"\n";
242
			for(File f : propertyfiles.get(dirfile))
243
				rez += "  "+f.getName()+"\n"
244
		}
245
		return rez;
246
	}
247

  
248
	/**
249
	 * Scan directory.
250
	 *
251
	 * @param directory the directory
252
	 */
253
	public void scanDirectory(File directory)
254
	{
255
		if(!directory.exists())
256
		{
257
			println "directory '$directory' does not exists"
258
			return;
259
		}
260
		
261
		println "scan directory : "+directory.getAbsolutePath();
262
		LinkedList<File> files = new LinkedList<File>();
263
		files.add(directory);
264
		
265
		while(!files.isEmpty())
266
		{
267
			File current = files.removeFirst();
268
			if(current.isDirectory())
269
			{
270
				List<String> currentpfiles = [];
271
				for(File sfile : current.listFiles())
272
				{
273
					if(sfile.isDirectory())
274
						files.add(sfile);
275
					else if(sfile.getName().endsWith(".properties") && ( sfile.getName().startsWith("messages") || sfile.getName().startsWith("bundle")) )
276
						currentpfiles.add(sfile)
277
				}
278
				if(currentpfiles.size() > 0)
279
				{
280
					dirfiles.add(current.getAbsolutePath());
281
					propertyfiles.put(current.getAbsolutePath(), currentpfiles);
282
				}
283
			}
284
		}
285
	}
286

  
287
	/**
288
	 * The main method.
289
	 *
290
	 * @param args the arguments
291
	 */
292
	public static void main(String[] args)
293
	{
294
		String userdir = System.getProperty("user.home");
295

  
296
		println "\nRCP\n";
297
		
298
		ExternalizationFilesUpdater scanner = new ExternalizationFilesUpdater();
299
//		scanner.scanDirectory(new File(userdir, "workspace37/org.txm.rcp")); // find directories with a messages.properties file
300
//		scanner.createMissingFiles("fr"); // create messages_fr.properties files when a messages.properties is found
301
//		scanner.processFoundDirectories(); // find missing and obsolets keys
302
//		scanner.updateFiles(); // update messages files content
303
		
304
		println "\nTOOLBOX\n";
305
		
306
		scanner = new ExternalizationFilesUpdater();
307
		scanner.scanDirectory(new File(userdir, "workspace43/org.txm.rcp/src/main/java"));
308
		scanner.createMissingFiles("fr");
309
		scanner.createMissingFiles("ru");
310
		scanner.processFoundDirectories();
311
		scanner.updateFiles();
312
	}
313
}
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/i18n/I18nDiff.groovy (revision 1223)
1
package org.txm.rcp.translate.i18n
2

  
3
import java.io.File;
4
import java.util.HashMap;
5
/**
6
 * Display a diff of 2 message files
7
 * 
8
 * @author mdecorde
9
 *
10
 */
11
class I18nDiff {
12
	Properties values1, values2;
13
	File file1;
14
	File file2;
15

  
16
	def notInValues1, notInValues2
17
	I18nDiff(File file1, File file2) {
18
		this.file1 = file1
19
		this.file2 = file2
20
		values1 = getValues(file1)
21
		values2 = getValues(file2)
22

  
23
		def inter = values1.keySet().intersect(values2.keySet());
24
		notInValues1 = values2.keySet().minus(values1.keySet());
25
		notInValues2 = values1.keySet().minus(values2.keySet());
26

  
27
		println "not in $file1"
28
		for(String key : notInValues1)
29
			if (key.trim().length() > 0)
30
				println key+" = "+values2.get(key)
31

  
32
		println "not in $file2"
33
		for(String key : notInValues2)
34
			if (key.trim().length() > 0)
35
				println key+" = "+values1.get(key)
36

  
37
		//		println "values diff"
38
		//
39
		//		for(String key : inter ) {
40
		//			if(values1.get(key) != values2.get(key))
41
		//				println key+" = "+values1.get(key)+" != "+values2.get(key)
42
		//		}
43
	}
44

  
45
	public void synchronizeFromF1toF2() {
46
		
47
		for(String key : notInValues2) {
48
			if (key.trim().length() > 0)
49
				values2.setProperty(key, "NA_"+values1.get(key));
50
		}
51
		
52
		for(String key : notInValues1)
53
			if (key.trim().length() > 0)
54
				values2.remove(key)
55
				
56
		file2.withWriter("iso-8859-1") { writer ->
57
			values2.store(writer, "")
58
		}
59
	}
60

  
61
	/**
62
	 * Gets the values.
63
	 *
64
	 * @param file the file
65
	 * @return the values
66
	 */
67
	public Properties getValues(File file) {
68
		Properties props = new Properties();
69
		file.withReader("iso-8859-1") { input->
70
			props.load(input);
71
			input.close()
72
		}
73
		return props;
74
	}
75

  
76
	public static void main(String[] args) {
77
		String userhome = System.getProperty("user.home");
78
		File f1 = new File(userhome, "workspace43/org.txm.rcp/OSGI-INF/l10n/bundle.properties")
79
		File f2 = new File(userhome, "workspace43/org.txm.rcp/OSGI-INF/l10n/bundle_fr.properties")
80
		
81
		I18nDiff diff = new I18nDiff(f1, f2);
82
		diff.synchronizeFromF1toF2()
83
	}
84
}
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/i18n/MergeExternalizedStringFiles.groovy (revision 1223)
1
package org.txm.rcp.translate.i18n
2

  
3
import java.io.File;
4
import org.eclipse.osgi.util.NLS;
5
import org.txm.utils.DeleteDir;
6

  
7
/**
8
 * Scan for *.properties files and merge them into one
9
 * @author mdecorde
10
 *
11
 */
12
class MergeExternalizedStringFiles {
13
	
14
	ArrayList<File> alldirs;
15
	LinkedHashMap<File, ArrayList> data = [:];
16
	
17
	public MergeExternalizedStringFiles(File projectDir)
18
	{
19
		alldirs = scanDirectory(projectDir);
20
		alldirs.sort{it.getAbsolutePath();}
21
		//System.out.println("All dirs size: "+alldirs.size);
22
		findMessageClasses();
23
		mergeAll()
24
	}
25
	
26
	public findMessageClasses()
27
	{
28
		File messageClass;
29
		ArrayList<File> externalizeMessages;
30
		for(int i = 0 ; i < alldirs.size() ; i++) {
31
			File dir = alldirs.get(i)
32
			if (!dir.isDirectory())
33
				continue;
34
			//System.out.println("Dir: "+dir);
35
			externalizeMessages = new ArrayList<File>();
36
			for (File f : dir.listFiles()) {
37
				if (f.getName() == "Messages.java")
38
					messageClass = f
39
				if (f.getName() =~ "messages(_..)?\\.properties")
40
					externalizeMessages << f
41
			}
42
			
43
			if (messageClass != null) {
44
				String bundle = findBundle(messageClass);
45
				data.put(dir, [messageClass, bundle, externalizeMessages])
46
			} else {
47
				alldirs.remove(i);
48
				i--;
49
			}
50
		}
51
		println "all dirs size: "+alldirs.size()
52
		//		for(def f : alldirs)
53
		//			println f
54
	}
55
	
56
	public String findBundle(File message)
57
	{
58
		String ret;
59
		message.eachLine("iso-8859-1") {
60
			if (it.contains("private static final String BUNDLE_NAME = ")) {
61
				int start = it.indexOf("\"")
62
				int end = it.lastIndexOf("\"")
63
				ret = it.substring(start+1, end)
64
			}
65
		}
66
		return ret;
67
	}
68
	
69
	/**
70
	 * Scan directory.
71
	 *
72
	 * @param directory the directory
73
	 */
74
	public ArrayList<File> scanDirectory(File directory)
75
	{
76
		ArrayList<File> dirfiles = [];
77
		
78
		if (!directory.exists()) {
79
			println "directory '$directory' does not exists"
80
			return;
81
		}
82
		
83
		println "scan directory : "+directory.getAbsolutePath();
84
		LinkedList<File> files = new LinkedList<File>();
85
		files.add(directory);
86
		
87
		while (!files.isEmpty()) {
88
			File current = files.removeFirst();
89
			if (current.isDirectory() && !current.getName().startsWith(".")) {
90
				//println "add dir: "+current
91
				List<String> currentpfiles = [];
92
				for (File sfile : current.listFiles()) {
93
					if (sfile.isDirectory())
94
						files.add(sfile);
95
				}
96
				
97
				dirfiles.add(current);
98
			}
99
		}
100
		return dirfiles;
101
	}
102
	
103
	public void mergeAll()
104
	{
105
		def files = new ArrayList<File>(data.keySet());
106
		println files
107
		if (files.size() < 1)
108
			return;
109
		
110
		File refDir = files.get(0);
111
		File refClass = data.get(refDir)[0];
112
		String refBundle = data.get(refDir)[1];
113
		def externalized = data.get(refDir)[2];
114
		
115
		System.out.println("ref dir: "+refDir);
116
		System.out.println("ref class: "+refClass);
117
		System.out.println("ref bundle: "+refBundle);
118
		System.out.println("ref files: "+externalized);
119
		
120
		// get externalized ref strings
121
		//      Lang            Key     externalized string 
122
		HashMap<String, HashMap<String, String>> externalizedLines = [:];
123
		for (File source : externalized) {
124
			def lines = new HashMap<String, String>();
125
			externalizedLines.put(source.getName(), lines);
126
			source.eachLine("iso-8859-1"){
127
				if (it =~ ".+=.+") {
128
					int idx = it.indexOf("=");
129
					lines.put(it.substring(0, idx), it.substring(idx+1))
130
				} else if(it =~ ".+=\$") {
131
					int idx = it.indexOf("=")
132
					String key = it.substring(0, idx)
133
					String value = ""
134
					lines.put(key, value)
135
				}
136
			}
137
		}
138
		
139
		// get references keys
140
		HashSet<String> refKeys = new HashSet<String>();
141
		refClass.eachLine("iso-8859-1") {
142
			if (it =~ ".+public static String .+")
143
				refKeys << it.trim()
144
		}
145
		
146
		println "BEFORE"
147
		println "refKeys size: "+refKeys.size();
148
		println "fr externalizedLines size: "+externalizedLines.get("messages_fr.properties").size();
149
		println "en externalizedLines size: "+externalizedLines.get("messages.properties").size();
150
		
151
		// for all sub dir
152
		for (int i = 1 ; i < files.size() ; i++) {
153
			File srcDir = files.get(i);
154
			File srcClass = data.get(srcDir)[0];
155
			String srcBundle = data.get(srcDir)[1];
156
			
157
			def srcExternalized = data.get(srcDir)[2];
158
			// merge externalized strings
159
			for (File source : srcExternalized) {
160
				def lines = externalizedLines.get(source.getName());
161
				source.eachLine("iso-8859-1") {
162
					if (it =~ ".+=.+") {
163
						int idx = it.indexOf("=")
164
						String key = it.substring(0, idx)
165
						String value = it.substring(idx+1)
166
						if (lines.containsKey(key) && lines.get(key) != value)
167
							println "merged message of "+source.getName()+": "+it+ " >> "+lines.get(key)
168
						lines.put(key, value)
169
					} else if(it =~ ".+=\$") {
170
						int idx = it.indexOf("=")
171
						String key = it.substring(0, idx)
172
						String value = ""
173
						lines.put(key, value)
174
					}
175
				}
176
				source.delete(); // bye bye *.properties files
177
			}
178
						
179
			// merge static String
180
			if(!srcClass.exists())
181
			{
182
				println "file removed: "+srcClass
183
				println  srcDir
184
				println  srcClass
185
				println  srcBundle
186
				println  srcExternalized
187
				continue;
188
			}
189
			srcClass.eachLine("iso-8859-1"){
190
				if(it =~ ".+public static String .+")
191
				{
192
//					if(refKeys.contains(it.trim()))
193
//						println "Externalized key merged: "+it
194
					refKeys << it.trim()
195
				}
196
			}
197
			srcClass.delete(); // bye bye Messages.java
198
		}
199
		
200
		println "AFTER"
201
		println "refKeys size: "+refKeys.size();
202
		println "fr externalizedLines size: "+externalizedLines.get("messages_fr.properties").size();
203
		println "en externalizedLines size: "+externalizedLines.get("messages.properties").size();
204
		
205
		//check unused externalized strings
206
		def keysDeclared = refKeys.collect{it.substring(21, it.length() -1) };
207
		System.out.println("number of keys declared in Message.java: "+keysDeclared.size());
208
		def missingDeclarations = new HashMap<String, HashSet<String>>();
209
		def missingExternalizations = new HashMap<String, HashSet<String>>();
210
		for(def lang : externalizedLines.keySet())
211
		{
212
			def keys = externalizedLines.get(lang).keySet(); // keys  in propeties files
213
			missingExternalizations.put(lang, new HashSet<String>());
214
			missingDeclarations.put(lang, new HashSet<String>());
215
			for(String key : keys)
216
				if(!keysDeclared.contains(key))
217
					missingDeclarations.get(lang) << key
218
					
219
			for(String key : keysDeclared)
220
				if(!keys.contains(key))
221
					missingExternalizations.get(lang) << key
222
		}
223
		
224
		println "fix missing key declaration and externalization"
225
		for(String lang: missingExternalizations.keySet())
226
		{
227
			println "Lang: "+lang
228
			println " missingDeclarations: "+missingDeclarations.get(lang)
229
			for(String key : missingDeclarations.get(lang))
230
				refKeys << "public static String "+key+";"
231
			println " missingExternalizations: "+missingExternalizations.get(lang)
232
			for(String key : missingExternalizations.get(lang))
233
				externalizedLines.get(lang).put(key, "N/A")
234
		}
235
		
236
		println "AFTER FIX"
237
		println "refKeys size: "+refKeys.size();
238
		println "fr externalizedLines size: "+externalizedLines.get("messages_fr.properties").size();
239
		println "en externalizedLines size: "+externalizedLines.get("messages.properties").size();
240
		
241
	
242
		// write externalized lines
243
		for(File source : externalized)
244
		{
245
			source.withWriter("iso-8859-1") { out ->
246
				def hash = externalizedLines.get(source.getName())
247
				def keys = hash.keySet();
248
				keys = keys.sort()
249
				keys.each() { 
250
					out.writeLine(it+"="+hash.get(it))
251
				}
252
			}
253
		}
254

  
255
		// write classRef File
256
		refClass.withWriter("iso-8859-1") { out ->			
257
			// write start
258
			out.writeLine('''
259
package org.txm.scripts.;
260

  
261
import org.eclipse.osgi.util.NLS;
262

  
263
public class Messages extends NLS {
264

  
265
	// The Constant BUNDLE_NAME. 
266
	private static final String BUNDLE_NAME = "org.txm.messages"; //$NON-NLS-1$
267
	
268
				''')
269
			
270
			//write keys
271
			for(String key : refKeys.sort())
272
				out.writeLine("\t"+key);
273
			
274
			// write end
275
			out.writeLine('''
276
	static {
277
		// initialize resource bundle
278
		NLS.initializeMessages(BUNDLE_NAME, Messages.class);
279
	}
280

  
281
	private Messages() {
282
	}
283
}
284
				''')
285
		}
286
	}
287
	
288
	public static void main(String[] args)
289
	{
290
		File projectdir = new File("/home/mdecorde/workspace37/org.txm.core/src/java")
291
		def rez = new MergeExternalizedStringFiles(projectdir);
292
	}
293
}
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/i18n/IsStringUsedInPlugin.groovy (revision 1223)
1
package org.txm.rcp.translate.i18n
2

  
3
/**
4
 * Display the String and key not used in the plugin messages
5
 * @author mdecorde
6
 *
7
 */
8
class IsStringUsedInPlugin {
9
	Properties props;
10

  
11
	public boolean scan(File propertyFile, File pluginFile) {
12
		if(!(propertyFile.exists() && propertyFile.canRead() && propertyFile.canWrite() && propertyFile.isFile()))
13
		{
14
			println "error file : "+propertyFile
15
			return false;
16
		}
17

  
18
		props = new Properties();
19
		propertyFile.withReader("iso-8859-1") { input->
20
			props.load(input);
21
			input.close()
22
		}
23

  
24
		String content = pluginFile.getText();
25
		def toRemove = [];
26
		for(def key : props.keySet()) {
27
			if (!content.contains("\"%"+key+"\"")) {
28
				println "not used $key "+props.get(key);
29
				toRemove << key
30
			}
31
		}
32

  
33
		for(String key : toRemove) {
34
			props.remove(key);
35
		}
36

  
37

  
38
		if (toRemove == 0) {
39
			println "nothing to do"
40
			return false;
41
		}
42
		return true
43
	}
44

  
45
	public boolean writeFixes(File propertyFile) {
46
		if (props == null) return false;
47
		propertyFile.withWriter("iso-8859-1") { output ->
48
			props.store(output, "")
49
		}
50
		return true;
51
	}
52

  
53
	public static void main(String[] args) {
54
		String userhome = System.getProperty("user.home")
55
		File propertyFile = new File(userhome, "workspace43/org.txm.rcp/OSGI-INF/l10n/bundle.properties");
56
		File pluginFile = new File(userhome, "workspace43/org.txm.rcp/plugin.xml");
57
		IsStringUsedInPlugin fixer = new IsStringUsedInPlugin();
58
		//uncomment to remove unused Strings
59
		if (fixer.scan(propertyFile, pluginFile)) {
60
			fixer.writeFixes(propertyFile)
61
		}
62
	}
63
}
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/i18n/ExternalizerUI.groovy (revision 1223)
1
package org.txm.rcp.translate.i18n;
2

  
3
import java.awt.*;
4
import javax.swing.*;
5
import javax.swing.table.*
6
import java.awt.GridLayout
7
import java.awt.event.*
8

  
9
import org.txm.utils.LS;
10
/**
11
 * This replace the eclipse externalisation interface
12
 * Scan the java files for not translated Strings and update the messages.properties file
13
 * 
14
 * uses ExternalizeStringFile
15
 * @author mdecorde
16
 *
17
 */
18
class ExternalizeUI extends JPanel {
19
	ExternalizeStringFile externalizer;
20

  
21
	ExternalizeUI() {
22
		super(new FlowLayout());
23
		File userDir = new File(System.getProperty("user.home"))
24
		File srcDir = new File(userDir, "workspace47/org.txm.rcp/src/main/java")
25
		File propFile = new File(srcDir, "org/txm/rcp/messages.properties")
26
		File messageFile = new File(srcDir, "org/txm/rcp/Messages.java")
27

  
28
		println userDir
29
		println srcDir
30
		println propFile
31
		println messageFile
32

  
33
		String[] columnNames = ["Files in "+srcDir.getPath(), "Nb of changes"];
34

  
35

  
36
		def files = LS.list(srcDir, true, false);
37
		files = files.findAll { file -> file.getName().endsWith(".java") }
38
		files = files.sort()
39
		println files
40

  
41
		externalizer = new ExternalizeStringFile(propFile, messageFile);
42
		println "externalizer ready"
43
		def data = []
44
		def selectedFiles = []
45
		for (File file : files) {
46
			int count = externalizer.getNbOfChanges(file)
47
			if (count != 0)	{
48
				selectedFiles << file
49
				data << [file.getPath().substring(srcDir.getPath().length()), count]
50
			}
51
		}
52
		Object[][] arrayData = data;
53
		JTable table = new JTable(arrayData, columnNames);
54
		table.getColumnModel().getColumn(0).setPreferredWidth(600);
55
		table.getColumnModel().getColumn(1).setPreferredWidth(100);
56

  
57
		table.addMouseListener(new MouseAdapter() {
58
					@Override
59
					public void mouseClicked(MouseEvent e) {
60
						if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 1) {
61
							int selectedRowIndex = table.getSelectedRow();
62
							int c = externalizer.process(selectedFiles[selectedRowIndex])
63
							println ""+c
64
							int after = table.getValueAt(table.getSelectedRow(), 1);
65
							table.setValueAt(after - c, table.getSelectedRow(), 1);
66
							//show your dialog with the selected row's contents
67
						}
68
					}
69
				});
70

  
71
		JScrollPane scrollPane = new JScrollPane(table);
72
		table.setFillsViewportHeight(true);
73

  
74
		add(scrollPane)
75

  
76
		JButton cancelButton = new JButton("CANCEL");
77
		JButton okButton = new JButton("OK + SAVE");
78
		okButton.addActionListener(new ActionListener() {
79
			public void actionPerformed(ActionEvent e)
80
			{
81
				ExternalizeUI.this.doSave();
82
				System.exit(0);
83
			}
84
		});
85
		
86
		cancelButton.addActionListener(new ActionListener() {
87
			public void actionPerformed(ActionEvent e)
88
			{
89
				println "Changes not saved: "
90
				System.exit(0);
91
			}
92
		});
93
		cancelButton.setSize(100, 20);
94
		okButton.setSize(100, 20);
95
		add(cancelButton)
96
		add(okButton)
97

  
98
		println "end of constructor"
99
	}
100

  
101
	public void doSave() {
102
		if (externalizer != null && externalizer.saveChanges()) {
103
			JOptionPane.showMessageDialog(this, "Changes saved");
104
		}
105
	}
106

  
107
	/**
108
	 * Create the GUI and show it.  For thread safety,
109
	 * this method should be invoked from the
110
	 * event-dispatching thread.
111
	 */
112
	private static void createAndShowGUI() {
113
		//Create and set up the window.
114
		JFrame frame = new JFrame("ExternalizeUI");
115
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
116

  
117
		//Create and set up the content pane.
118
		ExternalizeUI newContentPane = new ExternalizeUI();
119
		newContentPane.setOpaque(true); //content panes must be opaque
120

  
121
		//		frame.addWindowListener(new java.awt.event.WindowAdapter() {
122
		//			public void windowClosing(WindowEvent winEvt) {
123
		//				newContentPane.doSave();
124
		//				System.exit(0);
125
		//			}
126
		//		});
127
		frame.setContentPane(newContentPane);
128

  
129
		println "display"
130
		//Display the window.
131
		frame.pack();
132
		frame.setVisible(true);
133
	}
134

  
135

  
136
	public static void main(String[] args) {
137
		//Schedule a job for the event-dispatching thread:
138
		//creating and showing this application's GUI.
139
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
140
					public void run() {
141
						createAndShowGUI();
142
					}
143
				});
144
	}
145
}
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/i18n/GetStringsFromPropertiesFile.groovy (revision 1223)
1
package org.txm.rcp.translate.i18n;
2

  
3
import java.io.BufferedReader;
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.InputStreamReader;
7
import java.util.LinkedList;
8

  
9
import org.txm.utils.io.IOUtils;
10

  
11
File newProjectMessagesPackage= new File(System.getProperty("user.home"), "workspace079/org.txm.concordance.core/src/org/txm/concordances/messages");
12
File oldProjectMessagesPackage= new File(System.getProperty("user.home"), "workspace079/org.txm.core/src/java/org/txm/core/messages")
13
File newNLSClassFile = new File(newProjectMessagesPackage, "ConcordanceMessages.java");
14
File oldNLSClassFile = new File(oldProjectMessagesPackage, "TBXMessages.java");
15
def langs = ["_fr", "", "_ru"]
16
for (def lang : langs) {
17
	println "LANG=$lang"
18
	Properties oldProperties = new Properties();
19
	File oldMessageFile = new File(oldProjectMessagesPackage, "messages${lang}.properties")
20
	oldProperties.load(IOUtils.getReader(oldMessageFile, "UTF-8"));
21
	Properties newProperties = new Properties();
22
	File newMessageFile = new File(newProjectMessagesPackage, "messages${lang}.properties")
23
	if (newMessageFile.exists())
24
		newProperties.load(IOUtils.getReader(newMessageFile, "UTF-8"));
25
		
26
	newNLSClassFile.getText("UTF-8").findAll("public static String [^;]+;") { key ->
27
		key = key.substring(21, key.length()-1)
28
		
29
		if (oldProperties.get(key) == null) {
30
			println "Key $key is missing from old messages file: "+oldMessageFile.getName()
31
			newProperties.put(key, "MISSING")
32
		} else {
33
			if (newProperties.get(key) != null) {
34
				println "update $key = "+oldProperties.get(key)
35
			} else {
36
				println "add $key = "+oldProperties.get(key)
37
			}
38
			newProperties.put(key, oldProperties.get(key))
39
		}
40
	}
41
	newProperties.store(IOUtils.getWriter(newMessageFile, "UTF-8"), "created/updated with GetStringsFromPropertiesFile.groovy")
42
	println "Done"		
43
}
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/i18n/FindUntranslatedMessages.groovy (revision 1223)
1
package org.txm.rcp.translate.i18n
2

  
3
import org.txm.rcp.translate.i18n.ExternalizationFilesUpdater
4

  
5
def userdir = System.getProperty("user.home")
6
def workspace = new File(userdir, "workspace47")
7
def langs = ["fr", "ru"]
8

  
9
		scanner = new ExternalizationFilesUpdater();
10
		scanner.scanDirectory(new File(workspace, "org.txm.rcp"));
11
		for (def lang : langs) {
12
			scanner.createMissingFiles(lang);
13
		}
14
		scanner.processFoundDirectories();
15
		scanner.updateFiles();
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/i18n/FixExternalizedDuplicatedStrings.groovy (revision 1223)
1
package org.txm.rcp.translate.i18n
2

  
3
import org.txm.utils.DeleteDir;
4

  
5
/**
6
 * Browse messages files and look for duplicated values
7
 * @author mdecorde
8
 *
9
 */
10
class FixExternalizedDuplicatedStrings {
11
	
12
	static main(args) {
13
//		File propertyFile = new File("/home/mdecorde/Bureau/Experiences/rcp/messages.properties");
14
//		File messageClass = new File("/home/mdecorde/Bureau/Experiences/rcp/Messages.java");
15
//		File srcdirectory = new File("/home/mdecorde/Bureau/Experiences/rcp");
16
		File propertyFile = new File("/home/mdecorde/workspace37/org.txm.rcp/src/main/java/org/txm/rcp/messages.properties");
17
		File messageClass = new File("/home/mdecorde/workspace37/org.txm.rcp/src/main/java/org/txm/rcp/Messages.java");
18
		File srcdirectory = new File("/home/mdecorde/workspace37/org.txm.rcp/src/main/java/org/txm/rcp");
19
		if (!(propertyFile.exists() && propertyFile.canRead() && propertyFile.canWrite() && propertyFile.isFile())) {
20
			println "error file : "+propertyFile
21
			return
22
		}
23
		
24
		// build hash of externalised Strings
25
		//						  key	  externalized string
26
		def entries = new HashMap<String, String>()
27
		propertyFile.eachLine("iso-8859-1"){
28
			if(it =~ ".+=.+")
29
			{
30
				int idx = it.indexOf("=")
31
				String key = it.substring(0, idx)
32
				String value = it.substring(idx+1)
33
				entries.put(key, value)
34
			}
35
			else if(it =~ ".+=\$")
36
			{
37
				int idx = it.indexOf("=")
38
				String key = it.substring(0, idx)
39
				String value = ""
40
				entries.put(key, value)
41
			}
42
		}
43
		
44
		// invert hash
45
		def hash = entries
46
		def reverse = [:]
47
		hash.each{
48
			if(reverse[it.value] == null)
49
				reverse[it.value] = []
50
			reverse[it.value] << it.key
51
		};
52
		
53
		for(def key : reverse.keySet())
54
			if(reverse[key].size() > 1)
55
				println "doublons of $key: "+reverse[key]
56
		
57
		// translate dictionnary
58
		def dictionnary = [:]
59
		for(def value : reverse.keySet())
60
		{
61
			def keys = reverse[value];
62
			def first = keys[0];
63
			for(int i = 1 ; i < keys.size() ; i++) // the first key will not be removed
64
			{
65
				def key = keys.get(i)
66
				if(key != first)
67
				{
68
					dictionnary.put(key , first)
69
					println key+ " >> "+first
70
				}
71
			}
72
		}
73
		
74
		// patch source files : iterates over all java files
75
		
76
		/*def files = DeleteDir.scanDirectory(srcdirectory, true);
77
		for(File f : files)
78
		{
79
			if(f.getName().endsWith(".java") && f.getName() != "Messages.java")
80
			{
81
				def lines = f.readLines("UTF-8")
82
				f.withWriter("UTF-8"){ writer ->
83
					
84
					for(String line : lines)
85
					{
86
						for(String key : dictionnary.keySet())
87
						{
88
							String old = line;
89
							//line = line.replaceAll(, "Messages."+dictionnary[key]);
90
							line = (line =~ /Messages\.$key([^0-9]|$)/).replaceAll("Messages."+dictionnary[key]+'$1')
91

  
92
							if(old != line)
93
								System.out.println(f.getAbsolutePath()+":"+old+": replaced "+"Messages\\."+key+"[^0-9] Messages."+dictionnary[key]);
94
						}
95
						writer.writeLine(line);
96
					}	
97
				}
98
			}
99
		}
100
		
101
		// remove key declaration from Messages.java
102
		String txt = messageClass.getText("UTF-8")
103
		String[] split = txt.split("\n");
104
		messageClass.withWriter "UTF-8", { writer -> 
105
			for(String line : split)
106
			{
107
				if(line.contains("public static String "))
108
				{
109
					for(String key : dictionnary.keySet())
110
					{
111
						if(line.contains("public static String "+key+";"))
112
						{
113
							line = line.trim()
114
							line = null
115
							println "remove key: "+key
116
							break;
117
						}
118
					}
119
					if(line != null)
120
						writer.writeLine(line);
121
				}
122
				else
123
					writer.writeLine(line);
124
			}
125
		}*/
126
		
127
		// rewrite messages.properties
128
		def keytoRemove = dictionnary.keySet();
129
		propertyFile.withWriter("UTF-8") { writer ->
130
			for(String key : entries.keySet().sort())
131
				if(!keytoRemove.contains(key))
132
					writer.writeLine(key+"="+entries[key])
133
		}
134
	}
135
}
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/i18n/Merge2PropertiesFile.groovy (revision 1223)
1
package org.txm.rcp.translate.i18n
2

  
3
File propFile1 = new File("/home/mdecorde/workspace43/org.eclipse.equinox.p2.ui.nl_fr/org/eclipse/equinox/internal/p2/ui/messages_fr.properties")
4
File propFile2 = new File("/home/mdecorde/workspace43/org.txm.rcp.p2.ui/src/org/eclipse/equinox/internal/p2/ui/messages_fr.properties")
5

  
6
Properties props1 = new Properties()
7
props1.load(propFile1.newReader("iso-8859-1"))
8
Properties props2 = new Properties()
9
props2.load(propFile2.newReader("iso-8859-1"))
10

  
11
println "props1.size = "+props1.size()
12
println "props2.size = "+props2.size()
13

  
14
for( def key : props1.keySet()) {
15
	println "update: "+key
16
	props2.put(key, props1.get(key))
17
}
18

  
19
println "props2.size AFTER = "+props2.size()
20
props2.store(propFile2.newWriter("iso-8859-1"), "$propFile1 + $propFile2")
tmp/org.txm.translate.rcp/src/org/txm/rcp/translate/i18n/ReverseI18nDict.groovy (revision 1223)
1
package org.txm.rcp.translate.i18n
2

  
3
import org.txm.utils.BiHashMap
4

  
5
/**
6
 * Bihashmap 
7
 * Check any missing key in both of messages.properties and Message.java files
8
 * @author mdecorde
9
 *
10
 */
11
class ReverseI18nDict extends BiHashMap<String, String> {
12
	File propFile;
13
	File messageFile;
14
	
15
	def messageKeys = new HashSet<String>()
16
	
17
	public ReverseI18nDict(File propFile) {
18
		this(propFile, null)
19
	}
20
	
21
	public ReverseI18nDict(File propFile, File messageFile) {
22
		this.propFile = propFile
23
		this.messageFile = messageFile
24
		BufferedReader reader = new BufferedReader(new InputStreamReader(
25
				new FileInputStream(propFile), "iso-8859-1"));
26
		String line = reader.readLine();
27
		while (line != null) {
28
			if (!line.startsWith("#")) {
29
				String[] split = line.split("=", 2);
30
				if (split.length == 2) {
31
					this.put(split[0], split[1]);
32
				}
33
			}
34
			line = reader.readLine();
35
		}
36
		
37
		if (messageFile != null) {
38
			BufferedReader reader2 = new BufferedReader(new InputStreamReader(new FileInputStream(messageFile), "iso-8859-1"));
39
			String line2 = reader2.readLine();
40
			while (line2 != null) {
41
				line2 = line2.trim();
42
				if (line2.startsWith("public static String")) {
43
					line2 = line2.substring(21, line2.length() -1);
44
					messageKeys << line2
45
				}
46
				line2 = reader2.readLine();
47
			}
48
		}
49
	}
50
	
51
	public def getMissingsMessageKeys() {
52
		def missing = []
53
		for (String pKey : this.getKeys())
54
			if (!messageKeys.contains(pKey))
55
				missing << pKey
56
				
57
		return missing
58
	}
59
	
60
	public getMissingPropKeys() {
61
		def missing = []
62
		def pKeys = this.getKeys()
63
		for (String mKey : messageKeys)
64
			if (!pKeys.contains(mKey))
65
				missing << mKey
66
				
67
		return missing
68
	}
69
	
70
	public void saveChanges() {
71

  
72
		//Write prop File
73
		File oldFile = new File(propFile.getParentFile(), propFile.getName()+".cpy");
74
		propFile.renameTo(oldFile);
75
		propFile.withWriter("iso-8859-1") { out ->
76
			for(String key : this.getKeys().sort()) {
77
				out.println(key+"="+this.get(key))
78
			}
79
		}
80
		
81
		// write message File
82
		if (messageFile == null) return;
83
		
84
		File oldFile2 = new File(messageFile.getParentFile(), messageFile.getName()+".cpy");
85
		messageFile.renameTo(oldFile2); // back up
86
		messageFile.withWriter("iso-8859-1") { out ->
87
			// write start
88
			out.println('''package org.txm;
89

  
90
import org.eclipse.osgi.util.NLS;
91

  
92
public class Messages extends NLS {
93
	
94
	// The Constant BUNDLE_NAME. 
95
	private static final String BUNDLE_NAME = "org.txm.messages"; //$NON-NLS-1$
96
				''')
97
			
98
			//write keys
99
			for(String key : this.getKeys().sort())
100
				out.println("\tpublic static String $key;");
101
			
102
			// write end
103
			out.println('''
104
	static {
105
		// initialize resource bundle
106
		NLS.initializeMessages(BUNDLE_NAME, Messages.class);
107
	}
108

  
109
	private Messages() {
110
	}
111
}
112
				''')
113
		}
114
	}
... Ce différentiel a été tronqué car il excède la taille maximale pouvant être affichée.

Formats disponibles : Unified diff