Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / scripts / i18n / SplitExternalizedStringFiles.groovy @ 202

History | View | Annotate | Download (4.8 kB)

1
package org.txm.scripts.i18n
2

    
3
import java.io.File;
4
import java.util.ArrayList;
5
import java.util.regex.Matcher
6
import java.util.regex.Pattern;
7

    
8
// PARAMETERS
9

    
10
File workspaceDir = new File("/home/mdecorde/workspace079")
11
String sourceProject = "org.txm.core";
12
String targetProject = "org.txm.core";
13

    
14
// VARIABLES
15
def langs = ["", "_fr", "_ru"]
16
String sourceRootPackage = sourceProject
17
if (sourceProject.endsWith(".core")) sourceRootPackage = sourceProject.substring(0, sourceProject.lastIndexOf("."))
18
File sourceProjectDir = new File(workspaceDir, sourceProject)
19
File sourceRootPackageDir = new File(sourceProjectDir, "src/java/"+sourceRootPackage.replace(".", "/"))
20
if (!sourceRootPackageDir.exists()) sourceRootPackageDir = new File(sourceProjectDir, "src/java/main/"+sourceRootPackage.replace(".", "/"))
21
File sourceOSGIDir = new File(sourceProjectDir, "OSGI/l10n");
22

    
23
if (!sourceRootPackageDir.exists()) {println "MISSING FILE: $sourceRootPackageDir";}
24
if (!sourceOSGIDir.exists()) {println "MISSING FILE: $sourceOSGIDir";}
25

    
26
String targetRootPackage = targetProject
27
if (targetRootPackage.endsWith(".core")) targetRootPackage = targetProject.substring(0, targetProject.lastIndexOf("."))
28
File targetProjectDir = new File(workspaceDir, targetProject)
29
File targetPluginFile = new File(targetProjectDir, "plugin.xml")
30
File targetRootPackageDir = new File(targetProjectDir, "src/java/"+targetRootPackage.replace(".", "/"))
31
if (!targetRootPackageDir.exists()) targetRootPackageDir = new File(targetProjectDir, "src/java/main/"+targetRootPackage.replace(".", "/"))
32
File targetOSGIDir = new File(targetProjectDir, "OSGI/l10n");
33

    
34
if (!targetRootPackageDir.exists()) {println "MISSING FILE: $targetRootPackageDir";}
35
if (!targetOSGIDir.exists()) {println "MISSING FILE: $targetOSGIDir";}
36
if (!workspaceDir.exists()) { println "missing file $workspaceDir";return}
37
if (!targetProjectDir.exists()) { println "missing file $targetProjectDir";return}
38

    
39
// 1- find all keys from Java&Groovy files
40
def listJavaFiles(File directory) {
41
        def javaFiles = [];
42

    
43
        println "scan directory : "+directory;
44
        LinkedList<File> filesToProcess = new LinkedList<File>();
45
        filesToProcess.add(directory);
46
        while (!filesToProcess.isEmpty()) {
47
                File current = filesToProcess.removeFirst();
48
                if (current.isDirectory() && !current.getName().startsWith(".")) {
49
                        List<String> currentpfiles = [];
50
                        for (File sfile : current.listFiles()) {
51
                                if (sfile.isDirectory())
52
                                        filesToProcess.add(sfile);
53
                                else if (sfile.getName().endsWith(".java"))
54
                                        javaFiles.add(sfile);
55
                        }
56
                }
57
        }
58
        return javaFiles;
59
}
60

    
61
def targetJavaFiles = listJavaFiles(targetProjectDir)
62

    
63
// 2- find keys in Java files
64
println "processing "+targetJavaFiles.size()+" files..."
65
def keys = new HashSet<String>();
66

    
67
def p = /Messages\.([A-Z][^+ ),;}]++)/
68
Matcher m;
69
for (def javaFile : targetJavaFiles) {
70
        javaFile.eachLine("UTF-8") { line->
71
                if ((m = line =~ p)) {
72
                        keys << m.group(1)
73
                }
74
        }
75
}
76
println "Java files keys: "+keys.sort()
77
//println new HashSet(keys.collect{k-> k[-1]})
78

    
79
// 3- find keys in the plugin.xml file
80
println "processing "+targetJavaFiles.size()+" files..."
81
def pluginKeys = new HashSet<String>();
82

    
83
def p2 = /"\%([^"]++)"/
84

    
85
targetPluginFile.eachLine("UTF-8") { line->
86
        if ((m = line =~ p2)) {
87
                pluginKeys << m.group(1)
88
        }
89
}
90

    
91
println "Plugin file keys: "+pluginKeys.sort()
92

    
93
// 4- process each lang properties file
94
println "Extracting translations from source properties files and write to target properties files..."
95
for (def lang : langs) {
96
        File sourceBundleFile = new File(sourceOSGIDir, "bundle"+lang+".properties")
97
        File sourcePropertiesFile = new File(sourceRootPackageDir, "messages"+lang+".properties")
98
        File targetBundleFile = new File(sourceOSGIDir, "bundle"+lang+".properties")
99
        File targetPropertiesFile = new File(sourceRootPackageDir, "messages"+lang+".properties")
100

    
101
        if (sourcePropertiesFile.exists()) {
102
                println " Extracting translations from $sourcePropertiesFile"
103

    
104
                sourcePropertiesFile.eachLine("UTF-8") { line->
105
                        def split = line.split("=", 2);
106
                        def k = split[0]
107
                        if (keys.contains(k)) {
108
                                targetPropertiesFile << line
109
                        }
110
                }
111
        }
112
        if (sourceBundleFile.exists()) {
113
                println " Extracting translations from $sourceBundleFile"
114
                sourceBundleFile.eachLine("UTF-8") { line->
115
                        def split = line.split("=", 2);
116
                        def k = split[0]
117
                        if (pluginKeys.contains(k)) {
118
                                targetBundleFile << line
119
                        }
120
                }
121
        }
122
}
123

    
124
// 5- create the Messages.java files
125
def content1 = """package $targetRootPackageDir;
126

127
import org.eclipse.osgi.util.NLS;
128
import org.txm.core.messages.TXMCoreMessages;
129

130
public class Messages extends NLS {
131
        private static final String BUNDLE_NAME = "org.txm.messages"; //\$NON-NLS-1\$
132
"""
133

    
134
for (String k : keys) {
135
        content1 +="\n        public static String $k;"
136
}
137

    
138
def content2 = """
139
        static {
140
                TXMCoreMessages.initializeMessages(BUNDLE_NAME, Messages.class);
141
        }
142

143
        private Messages() {
144
        }
145
}
146
"""