Statistics
| Revision:

root / tmp / org.txm.translate.rcp / src / org / txm / rcp / translate / i18n / PluginMessages.groovy @ 1229

History | View | Annotate | Download (4.6 kB)

1
package org.txm.rcp.translate.i18n;
2

    
3
import org.txm.utils.BiHashMap
4

    
5
/**
6
 *  
7
 * Check any missing key in both of messages.properties and Message.java files
8
 *
9
 * @author mdecorde
10
 *
11
 */
12
class PluginMessages {
13
        
14
        File projectDirectory;
15
        File messageFile;
16
        String encoding = "UTF-8"
17
        
18
        LinkedHashSet messageKeys = new LinkedHashSet<String>()
19
        HashMap<File, BiHashMap<String, String>> langs = new HashMap<File, BiHashMap<String, String>>();
20
        BiHashMap<File, String> file2lang = new BiHashMap<File, String>()
21
                
22
        public PluginMessages(File projectDirectory, File messageFile) {
23
                this.messageFile = messageFile
24
                this.projectDirectory = projectDirectory;
25
                def propFiles = messageFile.getParentFile().listFiles()
26
                for (File propFile : propFiles) {
27
                        String name = propFile.getName();
28
                        if (!name.endsWith(".properties")) continue;
29
                        //messages.properties
30
                        //messages_xx.properties
31
                        String lang = name.substring(8, name.indexOf(".properties"))
32
                        langs.put(propFile, new BiHashMap<String, String>());
33
                        file2lang.put(propFile, lang)
34
                        
35
                        def hash = langs[propFile]
36
                        
37
                        Properties props1 = new Properties()
38
                        props1.load(propFile.newReader(encoding))
39
                        for (String k : props1.keySet()) {
40
                                hash.put(k, props1.get(k))
41
                        }
42
                }
43
        
44
                if (messageFile != null) {
45
                        BufferedReader reader2 = new BufferedReader(new InputStreamReader(new FileInputStream(messageFile), encoding));
46
                        String line2 = reader2.readLine();
47
                        while (line2 != null) {
48
                                line2 = line2.trim();
49
                                if (line2.startsWith("public static String") && line2.endsWith(";") && !line2.contains("=")) {
50
                                        line2 = line2.substring(21, line2.length() -1);
51
                                        messageKeys << line2
52
                                }
53
                                line2 = reader2.readLine();
54
                        }
55
                }
56
        }
57
        
58
        public LinkedHashSet getMessageKeys() {
59
                return messageKeys;
60
        }
61
        
62
        public File getMessageFile() {
63
                return messageFile;
64
        }
65
        
66
        public String getMessageClassName() {
67
                return messageFile.getName().substring(0, messageFile.getName().length()-5);
68
        }
69
        
70
        public String getMessageFullClassName() {
71
                return messageFile.getAbsolutePath().substring(messageFile.getAbsolutePath().lastIndexOf("org/txm/"), messageFile.getAbsolutePath().length()-5).replace("/", ".");
72
        }
73
        
74
        public String getMessageName() {
75
                return messageFile.getName().substring(0, messageFile.getName().length()-5-8);
76
        }
77
        
78
        public String getMessageFullName() {
79
                return messageFile.getAbsolutePath().substring(messageFile.getAbsolutePath().lastIndexOf("org/txm/")+8, messageFile.getAbsolutePath().length()-5-8).replace("/", ".");
80
        }
81
        
82
//        public def getMissingsMessageKeys(String lang) {
83
//                def missing = []
84
//                for (String pKey : this.getKeys())
85
//                        if (!messageKeys.contains(pKey))
86
//                                missing << pKey
87
//                                
88
//                return missing
89
//        }
90
//        
91
//        public getMissingPropKeys(String lang) {
92
//                def missing = []
93
//                def pKeys = this.getKeys()
94
//                for (String mKey : messageKeys)
95
//                        if (!pKeys.contains(mKey))
96
//                                missing << mKey
97
//                                
98
//                return missing
99
//        }
100
        
101
        public void saveChanges() {
102

    
103
                //Write prop File
104
                for (File propFile : langs.keySet()) {
105
                        String name = propFile.getName();
106
                        if (!name.endsWith(".properties")) continue;
107
                        String lang = name.substring(8, name.indexOf(".properties"))
108
                        
109
                        File oldFile = new File(propFile.getParentFile(), propFile.getName()+".cpy");
110
                        propFile.renameTo(oldFile);
111
                        
112
                        Properties props = new Properties()
113
                }
114
                
115
                
116
                // write message File
117
                if (messageFile == null) return;
118
                
119
                File oldFile2 = new File(messageFile.getParentFile(), messageFile.getName()+".cpy");
120
                messageFile.renameTo(oldFile2); // back up
121
                messageFile.withWriter(encoding) { out ->
122
                        // write start
123
                        out.println('''package org.txm;
124

125
import org.eclipse.osgi.util.NLS;
126

127
public class Messages extends NLS {
128
        
129
        // The Constant BUNDLE_NAME. 
130
        private static final String BUNDLE_NAME = "org.txm.messages"; //$NON-NLS-1$
131
                                ''')
132
                        
133
                        //write keys
134
                        for(String key : this.getKeys().sort())
135
                                out.println("\tpublic static String $key;");
136
                        
137
                        // write end
138
                        out.println('''
139
        static {
140
                // initialize resource bundle
141
                NLS.initializeMessages(BUNDLE_NAME, Messages.class);
142
        }
143

144
        private Messages() {
145
        }
146
}
147
                                ''')
148
                }
149
        }
150
        
151
        public void summary() {
152
                println getMessageFileName()
153
                println "Messages keys"
154
                for (def key : messageKeys) {
155
                        println " $key"
156
                }
157
                
158
                println "Langs"
159
                for (def lang : langs.keySet()) {
160
                        println " '$lang'"
161
                        def hash = langs[lang]
162
                        println " keys="+ hash.getKeys()
163
                        for (def key : hash.getKeys()) {
164
                                println "  $key="+hash.get(key)
165
                        }
166
                }
167
        }
168
        
169
        public static void main(String[] args) {
170
                File messageFile = new File("/home/mdecorde/workspace047/org.txm.core/src/java/org/txm/core/messages/TXMCoreMessages.java")
171
                PluginMessages dict = new PluginMessages(messageFile);
172
                
173
                println dict.summary()
174
                //dict.saveChanges();
175
        }
176
}