Revision 6232 trunk/libraries/libInternationalization/src-utils/org/gvsig/i18n/utils/UpdateTrans.java

View differences:

UpdateTrans.java
58 58
import java.util.HashSet;
59 59
import java.util.Iterator;
60 60
import java.util.Properties;
61
import java.util.TreeMap;
61 62

  
62 63
/**
63 64
 * @author cesar
......
103 104
	}
104 105
	
105 106
	private void updateDB(){
106
		Project currentProject;
107 107
		String lang, auxLang;
108
		Properties dict,auxDict;
109
		Enumeration keys;
110 108
		String key, value, dbValue;
111
		HashMap newKeys = new HashMap();
109
		
110
		HashSet removedKeys = new HashSet();
111
		HashMap newKeys = detectNewKeys();
112

  
113
		/**
114
		 * Process the new or changed keys
115
		 */
112 116
		for (int i=0; i<config.languages.length; i++) {
113 117
			lang = config.languages[i];
114
			newKeys.put(lang, new Properties());
118
			File langDir = new File(config.outputDir+File.separator+lang);
119
			langDir.mkdirs();
120
			
121
			// process the keys
122
			TreeMap newKeysDict = (TreeMap) newKeys.get(lang);
123
			Iterator newKeysIterator = newKeysDict.keySet().iterator();
124
			while (newKeysIterator.hasNext()) {
125
				int numValues=0;
126
				value = null;
127
				String project=null;
128
				key = (String) newKeysIterator.next();
129
				ArrayList newKeyList = (ArrayList) newKeysDict.get(key);
130
				String[] newKey;
131
				for (int j=0; j<newKeyList.size(); j++) {
132
					newKey = (String[]) newKeyList.get(j);
133
					if (!newKey[0].equals("")) {
134
						if (numValues==0) { //if there are several non-empty values, take the first one
135
							value = newKey[0];
136
							project = newKey[1];
137
							numValues++;
138
						}
139
						if (numValues==2) {
140
							System.err.println("Atenci?n -- La clave '"+key+"' tiene diferentes valores para el idioma "  + lang + ".");
141
							System.err.println("\tSe usar? la primera de las mostradas a continuaci?n:");
142
							System.err.println(project + " -- " + key + "=" + value);
143
							System.err.println(newKey[1] + " -- " + key + "=" + newKey[0]);
144
						}
145
						else if (numValues>2)
146
							System.err.println(newKey[1] + " -- " + key + "=" + newKey[0]);
147
					}
148
				}
149
				if (value!=null && !value.equals("")) { // the translation has a value
150
					dbValue = database.getTranslation(lang, key);
151
					if (dbValue==null || !dbValue.equals("")) {
152
						/* if dbValue == "" means that the key has been changed and should be sent for translation.
153
						 * It should not be added to the database with the value from the property file, as it is not valid anymore.
154
						 */
155
											
156
						database.setTranslation(lang, key, value);
157
						// it has been added to database, it isn't new anymore
158
						// we add the key to the list of keys to remove. We don't remove it now because then there is troubles with the iterator
159
						removedKeys.add(key);
160
					}
161
				}
162
			}
115 163
		}
116 164
		
117
		/**
118
		 * Detect the new or changed keys
119
		 * We just make a list, we will check the list later (to detect conflicting changes)
120
		 */
121
		for (int i=0; i<config.projects.size(); i++) {
122
			currentProject = (Project) config.projects.get(i);
123
			for (int j=0; j<config.languages.length; j++) {
124
				lang = config.languages[j];
125
				dict = (Properties) currentProject.dictionaries.get(lang);
126
				if (dict==null)
127
					System.out.println("aqui");
128
				keys = dict.keys();
129
				while (keys.hasMoreElements()) {
130
					key = (String) keys.nextElement();
131
					value = dict.getProperty(key);
132
					dbValue = database.getTranslation(lang, key);
133
					if (dbValue==null || !dbValue.equals(value)) {
134
						String []newKey = new String[2];
135
						newKey[0]=value;
136
						newKey[1]= currentProject.dir;
137
						auxDict = (Properties) newKeys.get(lang);
138
						ArrayList keyList = (ArrayList) auxDict.get(key);
139
						if (keyList==null) {
140
							keyList = new ArrayList();
165
		// remove 
166
		Iterator removeIterator = removedKeys.iterator();
167
		while (removeIterator.hasNext()) {
168
			key = (String) removeIterator.next();
169
			newKeys.remove(key);
170
		}
171
		removedKeys = new HashSet();
172
		
173
		// we already added all the new keys with value to the database
174
		// now we try to find a translation for the keys without translation
175
		for (int i=0; i<config.languages.length; i++) {
176
			lang = config.languages[i];
177
			File langDir = new File(config.outputDir+File.separator+lang);
178
			langDir.mkdirs();
179
			
180
			// process the keys
181
			TreeMap newKeysDict = (TreeMap) newKeys.get(lang);
182
			Iterator newKeysIterator = newKeysDict.keySet().iterator();
183
			while (newKeysIterator.hasNext()) {
184
				key = (String) newKeysIterator.next();
185
				value = "";
186
				for (int currentAuxLang=0; currentAuxLang<config.languages.length && (value==null || value.equals("")); currentAuxLang++) {
187
					auxLang = config.languages[currentAuxLang];
188
					value = database.getTranslation(auxLang, key);
189
					if (value!=null && !value.equals("")) {
190
						ArrayList keyList = database.getAssociatedKeys(auxLang, value);
191
						if (keyList!=null) {
192
							for (int j=0; j<keyList.size() && (value==null || !value.equals("")); j++) {
193
								value = database.getTranslation(lang, (String)keyList.get(j));
194
							}
141 195
						}
142
						keyList.add(newKey);
143
						auxDict.put(key, keyList);
144 196
					}
145 197
				}
198
				if (value!=null && !value.equals("")) { // the translation has a value
199
					dbValue = database.getTranslation(lang, key);
200
					if (dbValue==null || !dbValue.equals("")) {
201
						/* if dbValue == "" means that the key has been changed and should be sent for translation.
202
						 * It should not be added to the database with the value from the property file, as it is not valid anymore.
203
						 */
204
											
205
						database.setTranslation(lang, key, value);
206
						// it has been added to database, it isn't new anymore
207
						// we add the key to the list of keys to remove. We don't remove it now because then there is troubles with the iterator
208
						removedKeys.add(key);
209
					}
210
				}
146 211
			}
147 212
		}
148 213
		
214
		
215
		// remove 
216
		removeIterator = removedKeys.iterator();
217
		while (removeIterator.hasNext()) {
218
			key = (String) removeIterator.next();
219
			newKeys.remove(key);
220
		}	
221
	
222
		outputNewKeys(newKeys);
223
		
224
		database.save();
225
	}
226
	
227
	private void outputNewKeys(HashMap newKeys) {
228
		String lang, auxLang;
149 229
		/**
150 230
		 * Process the new or changed keys
151 231
		 */
......
154 234
			File langDir = new File(config.outputDir+File.separator+lang);
155 235
			langDir.mkdirs();
156 236
			HashMap outputFiles = new HashMap();
237
			HashMap outputPropertiesStream = new HashMap();
238
			HashMap outputProperties = new HashMap();
157 239
			
158 240
			// open the output files, one for each defined language
159 241
			for (int j=0; j<config.outputLanguages.length; j++) {
160 242
				auxLang = config.outputLanguages[j];
161
				FileOutputStream fos;
243
				FileOutputStream fos, fosProp;
244
				Properties prop;
162 245
				try {
163 246
					fos = new FileOutputStream(langDir.getPath()+File.separator+config.defaultBaseName+"_"+auxLang+".properties-"+config.outputEncoding);
247
					fosProp = new FileOutputStream(langDir.getPath()+File.separator+config.defaultBaseName+"_"+auxLang+".properties");
248
					prop = new Properties();
249
					outputPropertiesStream.put(auxLang, fosProp);
250
					outputProperties.put(auxLang, prop);
164 251
					try {
165 252
						outputFiles.put(auxLang, new BufferedWriter(new OutputStreamWriter(fos, config.outputEncoding)));
166 253
					} catch (UnsupportedEncodingException e) {
......
177 264
			
178 265
			// also open the file for language we're processing currently
179 266
			if (!outputFiles.containsKey(lang)) {
180
				FileOutputStream fos;
267
				FileOutputStream fos, fosProp;
268
				Properties prop;
181 269
				try {
182 270
					fos = new FileOutputStream(langDir.getPath()+File.separator+config.defaultBaseName+"_"+lang+".properties-"+config.outputEncoding);
271
					fosProp = new FileOutputStream(langDir.getPath()+File.separator+config.defaultBaseName+"_"+lang+".properties");
272
					prop = new Properties();
273
					outputPropertiesStream.put(lang, fosProp);
274
					outputProperties.put(lang, prop);
183 275
					try {
184 276
						outputFiles.put(lang, new BufferedWriter(new OutputStreamWriter(fos, config.outputEncoding)));
185 277
					} catch (UnsupportedEncodingException e) {
......
193 285
					System.exit(-1);
194 286
				}
195 287
			}
288
			
289
			TreeMap dict = (TreeMap) newKeys.get(lang);
290
			Iterator keyIterator = dict.keySet().iterator();
291
			String key, value;
292
			while (keyIterator.hasNext()) {
293
				key = (String) keyIterator.next();
196 294

  
197
			// process the keys
198
			Properties newKeysDict = (Properties) newKeys.get(lang);
199
			Iterator newKeysIterator = newKeysDict.keySet().iterator();
200
			while (newKeysIterator.hasNext()) {
201
				int numValues=0;
202
				value = null;
203
				String project=null;
204
				key = (String) newKeysIterator.next();
205
				ArrayList newKeyList = (ArrayList) newKeysDict.get(key);
206
				String[] newKey;
207
				for (int j=0; j<newKeyList.size(); j++) {
208
					newKey = (String[]) newKeyList.get(j);
209
					if (!newKey[0].equals("")) {
210
						if (numValues==0) { //if there are several non-empty values, take the first one
211
							value = newKey[0];
212
							project = newKey[1];
213
						numValues++;
295
				Iterator files = outputFiles.keySet().iterator();
296
				BufferedWriter writer;
297
				while (files.hasNext()) {
298
					// we output the pair key-value for the defined output languages (they're used for reference by translators)							
299
					auxLang = (String) files.next();
300
					writer = (BufferedWriter) outputFiles.get(auxLang);
301
					value = database.getTranslation(auxLang, key);
302
					try {
303
						if (value!=null)
304
							writer.write(key+"="+value+"\n");
305
						else
306
							writer.write(key+"=\n");
307
					} catch (IOException e) {
308
						// TODO Auto-generated catch block
309
						e.printStackTrace();
214 310
					}
215
					if (numValues==2) {
216
						System.err.println("Atenci?n -- La clave '"+key+"' tiene diferentes valores para el idioma "  + lang + ".");
217
						System.err.println("\tSe usar? la primera de las mostradas a continuaci?n:");
218
						System.err.println(project + " -- " + key + "=" + value);
219
						System.err.println(newKey[1] + " -- " + key + "=" + newKey[0]);
220
					}
221
					else if (numValues>2)
222
						System.err.println(newKey[1] + " -- " + key + "=" + newKey[0]);
223
					}
224 311
				}
225
				if (value!=null) { // the translation has a value
226
					database.setTranslation(lang, key, value);
312
				Iterator props = outputProperties.keySet().iterator();
313
				Properties prop;
314
				while (props.hasNext()) {
315
					// we output the pair key-value for the defined output languages (they're used for reference by translators)							
316
					auxLang = (String) props.next();
317
					prop = (Properties) outputProperties.get(auxLang);
318
					value = database.getTranslation(auxLang, key);
319
					if (value!=null)
320
						prop.put(key, value);
321
					else
322
						prop.put(key, "");
227 323
				}
228
				else {
229
					String auxKey;
230
					value = "";
231
					// try to search a matching translation from other keys in the database
232
					for (int j=0; j<config.languages.length && value!=null && !value.equals(""); j++) {
233
						// is there a value for this key in any language?
234
						auxLang = (String) config.languages[j];
235
						auxDict = (Properties) newKeys.get(lang);
236
						value = (String) auxDict.get(key);
237
						if (value!=null && !value.equals("")) {
238
							// if there was a value in some language, search for other keys with the same value
239
							auxKey = database.getAssociatedKey(auxLang, value);
240
							if (auxKey!=null) {
241
								// ok, we found the key, now let's try to search for the translation in the target language
242
								value = database.getTranslation(lang, auxKey);
243
							}
244
							else
245
								value = null;
246
						}
247
					}
248
					// we found a matching translation, great
249
					if (value!=null && !value.equals(""))
250
						database.setTranslation(lang, key, value);
251
					else {
252
						// we didn't find a valid translation, so the key needs to be sent for translation
253
						Iterator files = outputFiles.keySet().iterator();
254
						BufferedWriter writer;
255
						while (files.hasNext()) {
256
							// we output the pair key-value for the defined output languages (they're used for reference by translators)							
257
							auxLang = (String) files.next();
258
							writer = (BufferedWriter) outputFiles.get(auxLang);
259
							value = database.getTranslation(auxLang, key);
260
							try {
261
								if (value!=null)
262
									writer.write(key+"="+value+"\n");
263
								else
264
									writer.write(key+"=\n");
265
							} catch (IOException e) {
266
								// TODO Auto-generated catch block
267
								e.printStackTrace();
268
							}
269
						}
270
					}
324
			}
325
			
326
			Iterator props = outputProperties.keySet().iterator();
327
			Properties prop;
328
			FileOutputStream fos;
329
			while (props.hasNext()) {
330
				auxLang = (String) props.next();
331
				fos = (FileOutputStream) outputPropertiesStream.get(auxLang);
332
				prop = (Properties) outputProperties.get(auxLang);
333
				try {
334
					prop.store(fos, "Translations for language ["+auxLang+"]");
335
				} catch (IOException e) {
336
					// TODO Auto-generated catch block
337
					e.printStackTrace();
271 338
				}
272 339
			}
340
			
273 341
			// close the files now
274 342
			Iterator files = outputFiles.keySet().iterator();
275 343
			while (files.hasNext()) {							
......
282 350
				}
283 351
			}
284 352
		}
353
			
354
	}
285 355
		
286
		database.save();
356
	private HashMap detectNewKeys() {
357
		Project currentProject;
358
		String lang;
359
		Properties dict;
360
		TreeMap auxDict;
361
		Enumeration keys;
362
		String key, value, dbValue;
363
		HashMap newKeys = new HashMap();
364
		for (int i=0; i<config.languages.length; i++) {
365
			lang = config.languages[i];
366
			newKeys.put(lang, new TreeMap());
367
		}
368
	
369
		/**
370
		 * Detect the new or changed keys
371
		 * We just make a list, we will check the list later (to detect conflicting changes)
372
		 */
373
		for (int i=0; i<config.projects.size(); i++) {
374
			currentProject = (Project) config.projects.get(i);
375
			for (int j=0; j<config.languages.length; j++) {
376
				lang = config.languages[j];
377
				dict = (Properties) currentProject.dictionaries.get(lang);
378
				keys = dict.keys();
379
				while (keys.hasMoreElements()) {
380
					key = (String) keys.nextElement();
381
					if (key.equals("abrir_plantilla") && lang.equals("ca")) {
382
						System.out.println("aqui");
383
					}
384
					value = dict.getProperty(key);
385
					dbValue = database.getTranslation(lang, key);
386
					if (dbValue==null || dbValue.equals("") || (!value.equals("") && !dbValue.equals(value))) {
387
						String []newKey = new String[2];
388
						newKey[0]=value;
389
						newKey[1]= currentProject.dir;
390
						auxDict = (TreeMap) newKeys.get(lang);
391
						ArrayList keyList = (ArrayList) auxDict.get(key);
392
						if (keyList==null) {
393
							keyList = new ArrayList();
394
						}
395
						keyList.add(newKey);
396
						auxDict.put(key, keyList);
397
					}
398
				}
399
			}
400
		}
401
		return newKeys;
287 402
	}
288 403
	
289 404
	private static void usage() {

Also available in: Unified diff