Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_898 / libraries / libInternationalization / src-utils / org / gvsig / i18n / utils / AddNewTranslations.java @ 10513

History | View | Annotate | Download (4.9 KB)

1 6251 cesar
/**
2
 *
3
 */
4
package org.gvsig.i18n.utils;
5
6
import java.io.BufferedReader;
7
import java.io.File;
8
import java.io.FileInputStream;
9
import java.io.FileNotFoundException;
10
import java.io.IOException;
11
import java.io.InputStreamReader;
12
import java.io.UnsupportedEncodingException;
13
import java.util.Enumeration;
14
import java.util.Properties;
15
16
/**
17
 * @author cesar
18
 *
19
 */
20
public class AddNewTranslations {
21
        // The filename which stores the configuration (may be overriden by the command line parameter)
22
        private String configFileName = "config.xml";
23
24
        // Object to load and store the config options
25
        private ConfigOptions config;
26
27
        private TranslationDatabase database;
28
29
        /**
30
         * @param args
31
         */
32
        public static void main(String[] args) {
33
                AddNewTranslations process = new AddNewTranslations();
34
35
                // load command line parameters
36
                if (!process.readParameters(args)) {
37
                        usage();
38
                        System.exit(-1);
39
                }
40
41
                // transfer control to the program's main loop
42
                process.start();
43
        }
44
45
        private void start() {
46
                // load config options from the config file
47
                if (!loadConfig()) {
48
                        System.out.println("Error leyendo el fichero de configuraci?n.");
49
                        usage();
50
                        System.exit(-1);
51
                }
52
53
                loadDataBase();
54
55
                readNewTranslations();
56
57
                database.save();
58
        }
59
60
        /**
61
         *  Reads the command line parameters */
62
        private boolean readParameters(String[] args) {
63
                String configPair[];
64
65
                for (int i=0; i<args.length; i++) {
66
                        configPair = args[i].split("=",2);
67
                        if ( (configPair[0].equals("-c") || configPair[0].equals("--config"))
68
                                        && configPair.length==2) {
69
                                configFileName = configPair[1];
70
                        }
71
                        else {
72
                                return false;
73
                        }
74
                }
75
                return true;
76
        }
77
78
        private boolean loadConfig() {
79
                config = new ConfigOptions(configFileName);
80
                config.load();
81
                return true;
82
        }
83
84
        private static void usage() {
85
                System.out.println("Uso: AddNewTranslations [OPCION]");
86
                System.out.println("\t-c\t--config=configFile");
87
        }
88
89
        private void loadDataBase() {
90
                database = new TranslationDatabase(config);
91
                database.load();
92
        }
93
94
        private void readNewTranslations() {
95 6351 cesar
                String lang, key, value, oldValue;
96 6251 cesar
97
                for (int i=0; i<config.languages.length; i++) {
98
                        lang = config.languages[i];
99
                        try {
100
                                FileInputStream fisProp = new FileInputStream(config.inputDir+File.separator+config.defaultBaseName+"_"+lang+".properties");
101
                                Properties prop = new Properties();
102
                                try {
103
                                        prop.load(fisProp);
104
                                        Enumeration keysEnum  = prop.keys();
105
                                        while (keysEnum.hasMoreElements()){
106
                                                key = (String) keysEnum.nextElement();
107
                                                value = prop.getProperty(key);
108 6260 cesar
                                                if (value!=null && !value.equals("")) {
109 6351 cesar
                                                        if (!database.containsKey(lang, key)) {
110 6260 cesar
                                                                System.out.println("["+lang+"] Traducci?n a?adida -- "+key+"="+value);
111 6351 cesar
                                                                database.setTranslation(lang, key, value);
112 6260 cesar
                                                        }
113
                                                        else {
114 6351 cesar
                                                                oldValue = database.setTranslation(lang, key, value);
115
                                                                if (!oldValue.equals(value)) {
116
                                                                        System.out.println("["+lang+"] Traducci?n actualizada -- "+key+"="+value);
117
                                                                        System.out.println("Valor anterior: "+database.getTranslation(lang, key));
118
                                                                }
119 6260 cesar
                                                        }
120
                                                }
121 6251 cesar
                                        }
122
                                } catch (IOException e) {
123
                                        System.err.println("Error leyendo traducciones para idioma ["+lang+"]. "+e.getLocalizedMessage());
124
                                }
125
126
                        } catch (FileNotFoundException e) {
127
                                try {
128
                                        FileInputStream fis = new FileInputStream(config.inputDir+File.separator+config.defaultBaseName+"_"+lang+".properties-"+config.outputEncoding);
129
130
                                        BufferedReader currentFile=null;
131
                                        try {
132
                                                currentFile = new BufferedReader(new InputStreamReader(fis, config.outputEncoding));
133
                                        } catch (UnsupportedEncodingException e1) {
134
                                                // TODO Auto-generated catch block
135
                                                e1.printStackTrace();
136
                                        }
137
138
                                    String line = null;
139
                                    try {
140
                                                while((line = currentFile.readLine()) != null) {
141
                                                        String[] parts = line.split("=");
142
                                                        if (parts.length == 2) {
143
                                                                key = parts[0];
144
                                                                value = parts[1];
145 6260 cesar
                                                                if (value!=null && !value.equals("")) {
146 6351 cesar
                                                                        if (!database.containsKey(lang, key)) {
147 6260 cesar
                                                                                System.out.println("["+lang+"] Traducci?n a?adida -- "+key+"="+value);
148 6351 cesar
                                                                                database.setTranslation(lang, key, value);
149 6260 cesar
                                                                        }
150
                                                                        else {
151 6351 cesar
                                                                                oldValue = database.setTranslation(lang, key, value);
152
                                                                                if (!oldValue.equals(value)) {
153
                                                                                        System.out.println("["+lang+"] Traducci?n actualizada -- "+key+"="+value);
154
                                                                                        System.out.println("Valor anterior: "+database.getTranslation(lang, key));
155
                                                                                }
156 6260 cesar
                                                                        }
157
                                                                }
158 6251 cesar
                                                        }
159
                                                        else {
160
                                                                System.err.println("Error leyendo traducciones para idioma ["+lang+"].");
161
                                                                System.err.println("L?nea: "+line);
162
                                                        }
163
                                                }
164
                                            currentFile.close();
165
                                        } catch (IOException ex) {
166
                                                System.err.println("Error leyendo traducciones para idioma ["+lang+"]. "+ex.getLocalizedMessage());
167
                                        }
168
169
                                } catch (FileNotFoundException e1) {
170
                                        System.out.println("Aviso -- no se encontraron nuevas traducciones para el idioma ["+lang+"].");
171
                                }
172
                        }
173
174
                }
175
176
        }
177
178
}