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
|
/* GNU gettext for Java
* Copyright (C) 2001-2003, 2007, 2015 Free Software Foundation, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gnu.gettext;
import java.lang.reflect.*;
import java.util.*;
import java.io.*;
/**
* This programs dumps a resource as a PO file. The resource must be
* accessible through the CLASSPATH.
*
* @author Bruno Haible
*/
public class DumpResource {
private Writer out;
private void dumpString (String str) throws IOException {
int n = str.length();
out.write('"');
for (int i = 0; i < n; i++) {
char c = str.charAt(i);
if (c == 0x0008) {
out.write('\\'); out.write('b');
} else if (c == 0x000c) {
out.write('\\'); out.write('f');
} else if (c == 0x000a) {
out.write('\\'); out.write('n');
} else if (c == 0x000d) {
out.write('\\'); out.write('r');
} else if (c == 0x0009) {
out.write('\\'); out.write('t');
} else if (c == '\\' || c == '"') {
out.write('\\'); out.write(c);
} else
out.write(c);
}
out.write('"');
}
private void dumpMessage (String msgid, String msgid_plural, Object msgstr) throws IOException {
int separatorPos = msgid.indexOf('\u0004');
if (separatorPos >= 0) {
String msgctxt = msgid.substring(0,separatorPos);
msgid = msgid.substring(separatorPos+1);
out.write("msgctxt "); dumpString(msgctxt);
}
out.write("msgid "); dumpString(msgid); out.write('\n');
if (msgid_plural != null) {
out.write("msgid_plural "); dumpString(msgid_plural); out.write('\n');
for (int i = 0; i < ((String[])msgstr).length; i++) {
out.write("msgstr[" + i + "] ");
dumpString(((String[])msgstr)[i]);
out.write('\n');
}
} else {
out.write("msgstr "); dumpString((String)msgstr); out.write('\n');
}
out.write('\n');
}
private ResourceBundle catalog;
private Method lookupMethod;
// Lookup the value corresponding to a key found in catalog.getKeys().
// Here we assume that the catalog returns a non-inherited value for
// these keys. FIXME: Not true. Better see whether handleGetObject is
// public - it is in ListResourceBundle and PropertyResourceBundle.
private Object lookup (String key) {
Object value = null;
if (lookupMethod != null) {
try {
value = lookupMethod.invoke(catalog, new Object[] { key });
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.getTargetException().printStackTrace();
}
} else {
try {
value = catalog.getObject(key);
} catch (MissingResourceException e) {
}
}
return value;
}
private void dump () throws IOException {
lookupMethod = null;
try {
lookupMethod = catalog.getClass().getMethod("lookup", new Class[] { java.lang.String.class });
} catch (NoSuchMethodException e) {
} catch (SecurityException e) {
}
Method pluralMethod = null;
try {
pluralMethod = catalog.getClass().getMethod("get_msgid_plural_table", new Class[0]);
} catch (NoSuchMethodException e) {
} catch (SecurityException e) {
}
Field pluralField = null;
try {
pluralField = catalog.getClass().getField("plural");
} catch (NoSuchFieldException e) {
} catch (SecurityException e) {
}
// Search for the header entry.
{
Object header_entry = null;
Enumeration keys = catalog.getKeys();
while (keys.hasMoreElements())
if ("".equals(keys.nextElement())) {
header_entry = lookup("");
break;
}
// If there is no header entry, fake one.
// FIXME: This is not needed; right after po_lex_charset_init set
// the PO charset to UTF-8.
if (header_entry == null)
header_entry = "Content-Type: text/plain; charset=UTF-8\n";
dumpMessage("",null,header_entry);
}
// Now the other messages.
{
Enumeration keys = catalog.getKeys();
Object plural = null;
if (pluralMethod != null) {
// msgfmt versions > 0.13.1 create a static get_msgid_plural_table()
// method.
try {
plural = pluralMethod.invoke(catalog, new Object[0]);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.getTargetException().printStackTrace();
}
} else if (pluralField != null) {
// msgfmt versions <= 0.13.1 create a static plural field.
try {
plural = pluralField.get(catalog);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
if (plural instanceof String[]) {
// A GNU gettext created class with plural handling, Java2 format.
int i = 0;
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
Object value = lookup(key);
String key_plural = (value instanceof String[] ? ((String[])plural)[i++] : null);
if (!"".equals(key))
dumpMessage(key,key_plural,value);
}
if (i != ((String[])plural).length)
throw new RuntimeException("wrong plural field length");
} else if (plural instanceof Hashtable) {
// A GNU gettext created class with plural handling, Java format.
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
if (!"".equals(key)) {
Object value = lookup(key);
String key_plural = (value instanceof String[] ? (String)((Hashtable)plural).get(key) : null);
dumpMessage(key,key_plural,value);
}
}
} else if (plural == null) {
// No plural handling.
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
if (!"".equals(key))
dumpMessage(key,null,lookup(key));
}
} else
throw new RuntimeException("wrong plural field value");
}
}
public DumpResource (String resource_name, String locale_name) {
// Split locale_name into language_country_variant.
String language;
String country;
String variant;
language = locale_name;
{
int i = language.indexOf('_');
if (i >= 0) {
country = language.substring(i+1);
language = language.substring(0,i);
} else
country = "";
}
{
int j = country.indexOf('_');
if (j >= 0) {
variant = country.substring(j+1);
country = country.substring(0,j);
} else
variant = "";
}
Locale locale = new Locale(language,country,variant);
// Get the resource.
ResourceBundle catalog = ResourceBundle.getBundle(resource_name,locale);
// We are only interested in the messsages belonging to the locale
// itself, not in the inherited messages. But catalog.getLocale() exists
// only in Java2 and sometimes differs from the given locale.
try {
Writer w1 = new OutputStreamWriter(System.out,"UTF8");
Writer w2 = new BufferedWriter(w1);
this.out = w2;
this.catalog = catalog;
dump();
w2.close();
w1.close();
System.out.flush();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
public static void main (String[] args) {
new DumpResource(args[0], args.length > 1 ? args[1] : "");
System.exit(0);
}
}
|