001: package br.com.gfpshare.testes;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.FileOutputStream;
006: import java.io.FilenameFilter;
007: import java.io.OutputStreamWriter;
008: import java.util.Enumeration;
009: import java.util.Properties;
010:
011: /*
012: * Created on 26/09/2005
013: *
014: * Swing Components - visit http://sf.net/projects/gfd
015: *
016: * Copyright (C) 2005 Igor Regis da Silva Simões
017: *
018: * This program is free software; you can redistribute it and/or
019: * modify it under the terms of the GNU General Public License
020: * as published by the Free Software Foundation; either version 2
021: * of the License, or (at your option) any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
031: *
032: */
033:
034: /**
035: *
036: * @author Igor Regis da Silva Simoes
037: * @since 26/09/2005
038: */
039: public class IntiDiff {
040:
041: public static void main(String[] args) throws Throwable {
042: // File dirInt = new File("/media/PEN_IGOR/GFProject/Gerenciador de Financas Pessoais - GFP/internationalization");
043: File dirInt = new File(
044: "H:\\GFProject\\Gerenciador de Financas Pessoais - GFP\\internationalization");
045: if (!dirInt.exists()) {
046: System.out.println("Dir Not Found! " + dirInt);
047: return;
048: }
049: String[] files = dirInt.list(new FilenameFilter() {
050: public boolean accept(File dir, String name) {
051: // return name.endsWith("zh.properties");
052: return name.endsWith(".properties")
053: && name.indexOf('_') == -1;
054: }
055: });
056:
057: if (true)
058: new IntiDiff().buildDiferences("_fa", files, dirInt
059: .getAbsolutePath());
060: else
061: new IntiDiff().applyDiferences("_pl", files, dirInt
062: .getAbsolutePath());
063:
064: }
065:
066: /**
067: *
068: * @param idiom Idioma do qual queremos construir as diferenças
069: * @param files
070: * @param dir
071: * @throws Throwable
072: */
073: private void buildDiferences(String idiom, String[] files,
074: String dir) throws Throwable {
075: Properties resultado = new Properties();
076:
077: for (int i = 0; i < files.length; i++) {
078: Properties originalEnglish = new Properties();
079: originalEnglish.load(originalEnglish.getClass()
080: .getResourceAsStream(
081: "/internationalization/" + files[i]));
082: // Para remover inconsistencias
083: // if (true)
084: // {
085: // Enumeration enu = originalEnglish.keys();
086: // while (enu.hasMoreElements())
087: // {
088: // String key = enu.nextElement().toString();
089: // if (originalEnglish.get(key) == null || originalEnglish.get(key).toString().equals(""))
090: // {
091: // System.out.println(key + " = " +originalEnglish.get(key));
092: // originalEnglish.remove(key);
093: // }
094: // }
095: // originalEnglish.store(new FileOutputStream("C:\\eclipse\\workspace\\Gerenciador de Financas Pessoais - GFP\\internationalization\\" + files[i]), null);
096: // continue;
097: // }
098: // System.out.println("Passou aqui");
099: if (!new File(dir
100: + File.separator
101: + files[i].replaceAll(".properties", idiom
102: + ".properties")).exists()) {
103: System.out.println();
104: System.out.println("!!!!!!!!!!!!!!! "
105: + files[i].replaceAll(".properties", idiom
106: + ".properties") + " não existe!!!");
107: System.out.println();
108:
109: Enumeration enu = originalEnglish.keys();
110: while (enu.hasMoreElements()) {
111: String key = enu.nextElement().toString();
112: if (key.indexOf("Mnemonic") == -1)
113: resultado.put(files[i] + "*" + key,
114: originalEnglish.get(key));
115: }
116: } else {
117: Properties originalIt = new Properties();
118: try {
119: originalIt
120: .load(originalIt
121: .getClass()
122: .getResourceAsStream(
123: "/internationalization/"
124: + files[i]
125: .replaceAll(
126: ".properties",
127: idiom
128: + ".properties")));
129: } catch (Exception e) {
130: e.printStackTrace();
131: System.out.println("/internationalization/"
132: + files[i].replaceAll(".properties", idiom
133: + ".properties"));
134: System.exit(-1);
135: }
136: System.out.println("Comparando: ");
137: System.out.println(files[i]);
138: System.out.println(files[i].replaceAll(".properties",
139: idiom + ".properties"));
140: System.out.println("");
141:
142: Enumeration enu = originalEnglish.keys();
143: while (enu.hasMoreElements()) {
144: String key = enu.nextElement().toString();
145:
146: if (originalIt.get(key) == null
147: || originalIt.get(key).equals("")) {
148: if (key.indexOf("Mnemonic") == -1) {
149: System.out.println("----> " + key);
150: resultado.put(files[i] + "*" + key,
151: originalEnglish.get(key));
152: }
153: }
154: }
155: }
156: }
157: // resultado.store(new FileOutputStream("/home/igor/Desktop"+File.separator+"ToTranslate" + idiom + ".properties"), null);
158: resultado.store(new FileOutputStream("C:\\ToTranslate" + idiom
159: + ".properties"), null);
160: }
161:
162: /**
163: *
164: * @param idiom
165: * @param files
166: * @param dir
167: * @throws Throwable
168: */
169: private void applyDiferences(String idiom, String[] files,
170: String dir) throws Throwable {
171: Properties resultado = new Properties();
172: // FileInputStream fis = new FileInputStream("/home/igor/Desktop"+File.separator+"ToTranslate" + idiom + ".properties");
173: FileInputStream fis = new FileInputStream("C:\\ToTranslate"
174: + idiom + ".properties");
175: resultado.load(fis);
176:
177: for (int i = 0; i < files.length; i++) {
178: Properties originalEnglish = new Properties();
179: originalEnglish.load(originalEnglish.getClass()
180: .getResourceAsStream(
181: "/internationalization/" + files[i]));
182: if (!new File(dir
183: + File.separator
184: + files[i].replaceAll(".properties", idiom
185: + ".properties")).exists()) {
186: System.out.println();
187: System.out.println("Criando arquivo "
188: + files[i].replaceAll(".properties", idiom
189: + ".properties"));
190: System.out.println();
191:
192: Properties originalIt = new Properties();
193: Enumeration enu = originalEnglish.keys();
194: while (enu.hasMoreElements()) {
195: String key = enu.nextElement().toString();
196:
197: if (originalIt.get(key) == null
198: || originalIt.get(key).equals("")) {
199: if (resultado.get(files[i] + "*" + key) != null) {
200: if (originalIt.get(key) != null
201: && originalIt.get(key).equals(
202: resultado.get(files[i]
203: + "*" + key)))
204: continue;
205: System.out.println(files[i]
206: + "*"
207: + key
208: + "--> Tradução "
209: + resultado.get(files[i] + "*"
210: + key));
211: originalIt.put(key, resultado.get(files[i]
212: + "*" + key));
213: } else
214: System.out.println(key
215: + " ainda não traduzida!");
216: }
217: }
218: new File(dir
219: + File.separator
220: + files[i].replaceAll(".properties", idiom
221: + ".properties")).createNewFile();
222: FileOutputStream fos = new FileOutputStream(dir
223: + File.separator
224: + files[i].replaceAll(".properties", idiom
225: + ".properties"));
226: OutputStreamWriter osw = new OutputStreamWriter(fos);
227: System.out.println(osw.getEncoding());
228: originalIt.store(fos, null);
229: fos.flush();
230: fos.close();
231: } else {
232: Properties originalIt = new Properties();
233: originalIt
234: .load(originalIt
235: .getClass()
236: .getResourceAsStream(
237: "/internationalization/"
238: + files[i]
239: .replaceAll(
240: ".properties",
241: idiom
242: + ".properties")));
243:
244: System.out.println("Inserindo Diferenças: ");
245: System.out.println(files[i]);
246: System.out.println(files[i].replaceAll(".properties",
247: idiom + ".properties"));
248: System.out.println("");
249:
250: Enumeration enu = originalEnglish.keys();
251: while (enu.hasMoreElements()) {
252: String key = enu.nextElement().toString();
253:
254: if (originalIt.get(key) == null) {
255: if (resultado.get(files[i] + "*" + key) != null) {
256: System.out.println(files[i]
257: + "*"
258: + key
259: + "--> Tradução "
260: + resultado.get(files[i] + "*"
261: + key));
262: originalIt.put(key, resultado.get(files[i]
263: + "*" + key));
264: } else
265: System.out.println(key
266: + " ainda não traduzida!");
267: }
268: }
269: originalIt.store(new FileOutputStream(dir
270: + File.separator
271: + files[i].replaceAll(".properties", idiom
272: + ".properties")), null);
273: }
274: }
275: }
276: }
|