-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathPropGen.java
241 lines (204 loc) · 5.6 KB
/
PropGen.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package com.mxgraph.properties;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.Bidi;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
* Takes an English properties file and uses a tsv output of translations
* of each word/phrase to generate the properties files for all supported
* languages.
*
* The reason for using tsv (tab separated values) is so that commas
* may be used in the translations.
*
*/
public class PropGen
{
/**
* Whether or not to use the mxGraph % sign encoding for unicode
*/
public static boolean encodeValues = false;
/**
* Specifies the file extension.
*/
public static String fileExtension = "txt";
/**
* @param args
*/
public static void main(String[] args)
{
File tsvFile = selectFile("Select TSV file", "tsv");
if (tsvFile != null)
{
try
{
execute(tsvFile);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/**
* Creates the translations for the given files.
*/
public static void execute(File tsvFile) throws IOException
{
if (tsvFile != null)
{
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(tsvFile), "UTF-8"));
// First line contains full language names
in.readLine();
// Second line contains file extensions
String line = in.readLine();
if (line != null)
{
String[] codes = line.split("\t");
if (codes.length > 0)
{
if (codes[0].equals(""))
{
codes[0] = "i18n";
}
StringBuilder[] outputFiles = new StringBuilder[codes.length];
while ((line = in.readLine()) != null)
{
String[] entries = line.split("\t");
if (entries.length > 1)
{
String key = entries[0];
if (key != null && key.length() > 0)
{
for (int i = 0; i < outputFiles.length; i++)
{
if (!codes[i].equals(""))
{
if (outputFiles[i] == null)
{
outputFiles[i] = new StringBuilder();
}
if (codes[i] == "i18n")
{
outputFiles[i].append(key + "=" + key + "\n");
}
else
{
String value = (entries.length > i) ? entries[i] : "";
// Empty entries will be translated to English
if (value.equals(""))
{
value = entries[1];
}
if (PropGen.encodeValues)
{
value = encodeString(value);
}
String lang = codes[i].toLowerCase();
boolean rtl = false;
if (lang.equals("ar") || lang.equals("fa") || lang.equals("he"))
{
Bidi bidi = new Bidi(value, 0);
if (!bidi.isLeftToRight())
{
System.out.println(value);
rtl = true;
}
}
if (rtl)
{
outputFiles[i].append(key + "=" + "\u202B" + value + "\u202C" + "\n");
}
else
{
// Checks for HTML entities in output
if (value.contains("<") || value.contains(">"))
{
System.out.println("**** WARNING: HTML Entities in " + lang + "/" + key + "=" + value);
}
outputFiles[i].append(key + "=" + value + "\n");
}
}
}
}
}
}
}
for (int i = 0; i < codes.length; i++)
{
if (!codes[i].equals(""))
{
String ext = (codes[i].equals("en")) ? "" : "_" + codes[i].toLowerCase();
File file = new File(tsvFile.getParent() + "/dia" + ext + "." + fileExtension);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
writer.write("# *DO NOT DIRECTLY EDIT THIS FILE, IT IS AUTOMATICALLY GENERATED AND IT IS BASED ON:*\n");
writer.write("# https://docs.google.com/spreadsheet/ccc?key=0AmQEO36liL4FdDJLWVNMaVV2UmRKSnpXU09MYkdGbEE\n");
writer.write(outputFiles[i].toString());
writer.close();
System.out.println(file.getAbsolutePath() + " created");
}
}
}
}
in.close();
}
else
{
System.out.println("No file specified");
}
}
/**
* Encodes the given string.
*/
public static String encodeString(String value)
{
StringBuilder result = new StringBuilder();
value = value.trim();
for (int j = 0; j < value.length(); j++)
{
char character = value.charAt(j);
String hexString = Integer.toHexString(character);
// +255 value must have 4 digits, 3 doesn't work
if (hexString.length() == 3)
{
hexString = "0" + hexString;
}
// Have to convert '%' too
if (character == 37 || (character > 127 && character < 256))
{
result.append("%" + hexString);
}
else if (character > 255)
{
result.append("%u" + hexString);
}
else
{
result.append(value.substring(j, j + 1));
}
}
return result.toString();
}
/**
* Shows a file dialog.
*/
public static File selectFile(String title, String extension)
{
JFileChooser chooser = new JFileChooser();
chooser.addChoosableFileFilter(new FileNameExtensionFilter(extension.toUpperCase() + " File", extension));
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setDialogTitle(title);
if (chooser.showOpenDialog(chooser) == JFileChooser.APPROVE_OPTION)
{
return chooser.getSelectedFile();
}
return null;
}
}