1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.i18n;
21
22 import java.io.*;
23 import java.util.*;
24
25 public class I18nHelper
26 {
27
28 /***
29 * Returns the position of the first unescaped occurence of needle in
30 * haystack.
31 */
32 public static int find(String haystack, String needle)
33 {
34 int i = 0;
35 while (true) {
36 i = haystack.indexOf(needle, i);
37 if (i <= 0) {
38 return i;
39 }
40 else if (!haystack.substring(i - 1, i).equals("//")) {
41 return i;
42 }
43 i++;
44 }
45 }
46
47 public static int findI18nDivider(String haystack)
48 {
49 return find(haystack, "=");
50 }
51
52 public static String getI18nCharset(String filename)
53 {
54 if (filename.indexOf("_ja") != -1) {
55 return "UTF-8";
56 }
57 else if (filename.indexOf("_zh") != -1) {
58 return "UTF-8";
59 }
60 else {
61 return "ISO-8859-1";
62 }
63 }
64
65 public static Hashtable readI18nFile(String filename,
66 boolean ignoreComments)
67 throws IOException
68 {
69 BufferedReader in = new BufferedReader
70 (new InputStreamReader(new FileInputStream(new File(filename)),
71 getI18nCharset(filename)));
72
73 System.err.println(getI18nCharset(filename));
74 Hashtable table = new Hashtable();
75 String s;
76 while ((s = in.readLine()) != null) {
77 if (s.startsWith("#")) {
78 if (ignoreComments) {
79 continue;
80 }
81 else {
82 s = s.substring(1);
83 }
84 }
85
86 int i = findI18nDivider(s);
87 if (i > 0 && i < s.length() - 1) {
88 String key = rawToI18nKey(s.substring(0, i));
89 String value = s.substring(i + 1);
90
91 table.put(key, value);
92 }
93 }
94 in.close();
95
96 return table;
97 }
98
99 public static HashSet readKeys()
100 {
101 System.out.print("Waiting for input... ");
102
103 HashSet keys = new HashSet();
104 try {
105 BufferedReader in
106 = new BufferedReader(new InputStreamReader(System.in));
107 String s;
108 while ((s = in.readLine()) != null) {
109 if (s.trim().length() > 0) {
110 keys.add(replaceAll(s, "//\"", "\""));
111 }
112 }
113 }
114 catch (IOException e) {
115 System.err.println("Error: " + e.getMessage());
116 System.exit(1);
117 }
118
119 System.out.println("processing " + keys.size() + " keys");
120
121 return keys;
122 }
123
124 public static String replaceAll(String s, String oldChars, String newChars)
125 {
126 StringBuffer sb = new StringBuffer();
127 int i = 0;
128 while (true) {
129 int j = s.indexOf(oldChars, i);
130 if (j != -1) {
131 sb.append(s.substring(i, j));
132 sb.append(newChars);
133 i = j + oldChars.length();
134 }
135 else {
136 sb.append(s.substring(i));
137 break;
138 }
139 }
140 return sb.toString();
141 }
142
143 public static String rawToI18nKey(String key)
144 {
145 return replaceAll(key, "//=", "=");
146 }
147
148 public static String i18nKeyToRaw(String key)
149 {
150 return replaceAll(key, "=", "//=");
151 }
152
153 public static String javaToRaw(String s)
154 {
155 s = replaceAll(s, "\n", "//n");
156 s = replaceAll(s, "\t", "//t");
157
158 return s;
159 }
160
161 public static String rawToJava(String s)
162 {
163 s = replaceAll(s, "//n", "\n");
164 s = replaceAll(s, "//t", "\t");
165
166 return s;
167 }
168
169 public static void writeI18nFile(String filename, Hashtable table)
170 throws IOException
171 {
172 BufferedWriter out = new BufferedWriter
173 (new OutputStreamWriter(new FileOutputStream(new File(filename)),
174 getI18nCharset(filename)));
175
176
177 for (Iterator i = table.keySet().iterator(); i.hasNext();) {
178 String key = (String)i.next();
179 String value = (String)table.get(key);
180 if (value != null && value.length() > 0) {
181 key = i18nKeyToRaw(key);
182
183 out.write(javaToRaw(key));
184 out.write("=");
185 out.write(javaToRaw(value));
186 out.newLine();
187 }
188 }
189
190 out.close();
191 }
192
193 }