Statistics
| Revision:

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

History | View | Annotate | Download (4 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 messageFile;
15
        String encoding = "UTF-8"
16
        
17
        LinkedHashSet messageKeys = new LinkedHashSet<String>()
18
        HashMap<File, BiHashMap<String, String>> langs = new HashMap<File, BiHashMap<String, String>>();
19
        BiHashMap<File, String> file2lang = new BiHashMap<File, String>()
20
                
21
        public PluginMessages(File messageFile) {
22
                this.messageFile = messageFile
23
                
24
                def propFiles = messageFile.getParentFile().listFiles()
25
                for (File propFile : propFiles) {
26
                        String name = propFile.getName();
27
                        if (!name.endsWith(".properties")) continue;
28
                        //messages.properties
29
                        //messages_xx.properties
30
                        String lang = name.substring(8, name.indexOf(".properties"))
31
                        langs.put(propFile, new BiHashMap<String, String>());
32
                        file2lang.put(propFile, lang)
33
                        
34
                        def hash = langs[propFile]
35
                        
36
                        Properties props1 = new Properties()
37
                        props1.load(propFile.newReader(encoding))
38
                        for (String k : props1.keySet()) {
39
                                hash.put(k, props1.get(k))
40
                        }
41
                }
42
        
43
                if (messageFile != null) {
44
                        BufferedReader reader2 = new BufferedReader(new InputStreamReader(new FileInputStream(messageFile), encoding));
45
                        String line2 = reader2.readLine();
46
                        while (line2 != null) {
47
                                line2 = line2.trim();
48
                                if (line2.startsWith("public static String") && line2.endsWith(";") && !line2.contains("=")) {
49
                                        line2 = line2.substring(21, line2.length() -1);
50
                                        messageKeys << line2
51
                                }
52
                                line2 = reader2.readLine();
53
                        }
54
                }
55
        }
56
        
57
        public LinkedHashSet getMessageKeys() {
58
                return messageKeys;
59
        }
60
        
61
        public File getMessageFile() {
62
                return messageFile;
63
        }
64
        
65
        public String getMessageFileName() {
66
                return messageFile.getName().substring(0, messageFile.getName().length()-5);
67
        }
68
        
69
//        public def getMissingsMessageKeys(String lang) {
70
//                def missing = []
71
//                for (String pKey : this.getKeys())
72
//                        if (!messageKeys.contains(pKey))
73
//                                missing << pKey
74
//                                
75
//                return missing
76
//        }
77
//        
78
//        public getMissingPropKeys(String lang) {
79
//                def missing = []
80
//                def pKeys = this.getKeys()
81
//                for (String mKey : messageKeys)
82
//                        if (!pKeys.contains(mKey))
83
//                                missing << mKey
84
//                                
85
//                return missing
86
//        }
87
        
88
        public void saveChanges() {
89

    
90
                //Write prop File
91
                for (File propFile : langs.keySet()) {
92
                        String name = propFile.getName();
93
                        if (!name.endsWith(".properties")) continue;
94
                        String lang = name.substring(8, name.indexOf(".properties"))
95
                        
96
                        File oldFile = new File(propFile.getParentFile(), propFile.getName()+".cpy");
97
                        propFile.renameTo(oldFile);
98
                        
99
                        Properties props = new Properties()
100
                }
101
                
102
                
103
                // write message File
104
                if (messageFile == null) return;
105
                
106
                File oldFile2 = new File(messageFile.getParentFile(), messageFile.getName()+".cpy");
107
                messageFile.renameTo(oldFile2); // back up
108
                messageFile.withWriter(encoding) { out ->
109
                        // write start
110
                        out.println('''package org.txm;
111

112
import org.eclipse.osgi.util.NLS;
113

114
public class Messages extends NLS {
115
        
116
        // The Constant BUNDLE_NAME. 
117
        private static final String BUNDLE_NAME = "org.txm.messages"; //$NON-NLS-1$
118
                                ''')
119
                        
120
                        //write keys
121
                        for(String key : this.getKeys().sort())
122
                                out.println("\tpublic static String $key;");
123
                        
124
                        // write end
125
                        out.println('''
126
        static {
127
                // initialize resource bundle
128
                NLS.initializeMessages(BUNDLE_NAME, Messages.class);
129
        }
130

131
        private Messages() {
132
        }
133
}
134
                                ''')
135
                }
136
        }
137
        
138
        public void summary() {
139
                println getMessageFileName()
140
                println "Messages keys"
141
                for (def key : messageKeys) {
142
                        println " $key"
143
                }
144
                
145
                println "Langs"
146
                for (def lang : langs.keySet()) {
147
                        println " '$lang'"
148
                        def hash = langs[lang]
149
                        println " keys="+ hash.getKeys()
150
                        for (def key : hash.getKeys()) {
151
                                println "  $key="+hash.get(key)
152
                        }
153
                }
154
        }
155
        
156
        public static void main(String[] args) {
157
                File messageFile = new File("/home/mdecorde/workspace047/org.txm.core/src/java/org/txm/core/messages/TXMCoreMessages.java")
158
                PluginMessages dict = new PluginMessages(messageFile);
159
                
160
                println dict.summary()
161
                //dict.saveChanges();
162
        }
163
}