forked from processing/processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLanguage.java
More file actions
376 lines (310 loc) · 10.3 KB
/
Language.java
File metadata and controls
376 lines (310 loc) · 10.3 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2014 The Processing Foundation
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2, as published by the Free Software Foundation.
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, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package processing.app;
import java.io.*;
import java.util.*;
import processing.core.PApplet;
/**
* Internationalization (i18n)
*/
public class Language {
// Store the language information in a file separate from the preferences,
// because preferences need the language on load time.
static protected final String PREF_FILE = "language.txt";
static protected final File prefFile = Base.getSettingsFile(PREF_FILE);
/** Single instance of this Language class */
static private volatile Language instance;
/** The system language */
private String language;
/** Available languages */
private HashMap<String, String> languages;
private LanguageBundle bundle;
private Language() {
String systemLanguage = Locale.getDefault().getLanguage();
language = loadLanguage();
boolean writePrefs = false;
if (language == null) {
language = systemLanguage;
writePrefs = true;
}
// Set available languages
languages = new HashMap<String, String>();
for (String code : listSupported()) {
Locale locale = Locale.forLanguageTag(code);
languages.put(code, locale.getDisplayLanguage(locale));
}
// Set default language
if (!languages.containsKey(language)) {
language = "en";
writePrefs = true;
}
if (writePrefs) {
saveLanguage(language);
}
// Get bundle with translations (processing.app.language.PDE)
//bundle = ResourceBundle.getBundle(Language.FILE, new Locale(this.language), new UTF8Control());
try {
bundle = new LanguageBundle(language);
} catch (IOException e) {
e.printStackTrace();
}
}
static private String[] listSupported() {
// List of languages in alphabetical order. (Add yours here.)
// Also remember to add it to build/shared/lib/languages/languages.txt.
final String[] SUPPORTED = {
"de", // German, Deutsch
"en", // English
"el", // Greek
"es", // Spanish
"fr", // French, Français
"ja", // Japanese
"ko", // Korean
"nl", // Dutch, Nederlands
"pt", // Portuguese
"tr", // Turkish
"uk", // Ukrainian
"zh" // Chinese
};
return SUPPORTED;
/*
// come back to this when bundles are placed outside the JAR
InputStream input = getClass().getResourceAsStream(LISTING);
String[] lines = PApplet.loadStrings(input);
ArrayList<String> list = new ArrayList<String>();
for (String line : lines) {
int index = line.indexOf('#');
if (index != -1) {
line = line.substring(0, index);
}
line = line.trim();
list.add(line);
}
return list.toArray(new String[0]);
*/
}
/** Read the saved language */
static private String loadLanguage() {
try {
if (prefFile.exists()) {
String language = PApplet.loadStrings(prefFile)[0];
language = language.trim().toLowerCase();
if (language.trim().length() != 0) {
return language;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Save the language directly to a settings file. This is 'save' and not
* 'set' because a language change requires a restart of Processing.
*/
static public void saveLanguage(String language) {
try {
Util.saveFile(language, prefFile);
} catch (Exception e) {
e.printStackTrace();
}
Platform.saveLanguage(language);
}
/** Singleton constructor */
static public Language init() {
if (instance == null) {
synchronized (Language.class) {
if (instance == null) {
instance = new Language();
}
}
}
return instance;
}
static private String get(String key) {
LanguageBundle bundle = init().bundle;
try {
String value = bundle.getString(key);
if (value != null) {
return value;
}
} catch (MissingResourceException e) { }
return null;
}
/** Get translation from bundles. */
static public String text(String key) {
String value = get(key);
if (value == null) {
// MissingResourceException and null values
return key;
}
return value;
}
static public String interpolate(String key, Object... arguments) {
String value = get(key);
if (value == null) {
return key;
}
// System.out.println(" interp for " + key + " is " + String.format(value, arguments));
return String.format(value, arguments);
}
static public String pluralize(String key, int count) {
// First check if the bundle contains an entry for this specific count
String customKey = key + "." + count;
String value = get(customKey);
if (value != null) {
return String.format(value, count);
}
// Use the general 'n' version for n items
return interpolate(key + ".n", count);
}
/**
* @param which either yes, no, cancel, ok, or browse
*/
static public String getPrompt(String which) {
return Language.text("prompt." + which);
}
/** Get all available languages */
static public Map<String, String> getLanguages() {
return init().languages;
}
/**
* Get the current language.
* @return two digit ISO code (lowercase)
*/
static public String getLanguage() {
return init().language;
}
/**
* Is this a CJK language where Input Method support is suggested/required?
* @return true if the user is running in Japanese, Korean, or Chinese
*/
static public boolean useInputMethod() {
final String language = getLanguage();
return (language.equals("ja") ||
language.equals("ko") ||
language.equals("zh"));
}
// /** Set new language (called by Preferences) */
// static public void setLanguage(String language) {
// this.language = language;
//
// try {
// File file = Base.getContentFile("lib/language.txt");
// Base.saveFile(language, file);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
/**
* Custom 'Control' class for consistent encoding.
* http://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle
*/
/*
static class UTF8Control extends ResourceBundle.Control {
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException,IOException {
// The below is a copy of the default implementation.
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, "properties");
ResourceBundle bundle = null;
InputStream stream = null;
if (reload) {
URL url = loader.getResource(resourceName);
if (url != null) {
URLConnection connection = url.openConnection();
if (connection != null) {
connection.setUseCaches(false);
stream = connection.getInputStream();
}
}
} else {
stream = loader.getResourceAsStream(resourceName);
}
if (stream != null) {
try {
// Only line changed from the original source:
bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
} finally {
stream.close();
}
}
return bundle;
}
}
*/
static class LanguageBundle {
Map<String, String> table;
LanguageBundle(String language) throws IOException {
table = new HashMap<String, String>();
// Check to see if the user is working on localization,
// and has their own .properties files in their sketchbook.
String baseFilename = "languages/PDE.properties";
String langFilename = "languages/PDE_" + language + ".properties";
File baseFile = Base.getLibFile(baseFilename);
File userBaseFile = new File(Base.getSketchbookFolder(), baseFilename);
if (userBaseFile.exists()) {
baseFile = userBaseFile;
}
File langFile = Base.getLibFile(langFilename);
File userLangFile = new File(Base.getSketchbookFolder(), langFilename);
if (userLangFile.exists()) {
langFile = userLangFile;
}
read(baseFile);
read(langFile);
}
void read(File additions) {
String[] lines = PApplet.loadStrings(additions);
if (lines == null) {
throw new NullPointerException("File not found:\n" + additions.getAbsolutePath());
}
//for (String line : lines) {
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
if ((line.length() == 0) ||
(line.charAt(0) == '#')) continue;
// this won't properly handle = signs inside in the text
int equals = line.indexOf('=');
if (equals != -1) {
String key = line.substring(0, equals).trim();
String value = line.substring(equals + 1).trim();
/*
// Support for backslashes to continue lines... Nah.
while (line.endsWith("\\")) {
// remove the backslash from the previous
value = value.substring(0, value.length() - 1);
// get the next line
line = lines[++i].trim();
// append the new line to the value (with a space)
// This is imperfect since the prev may end <br>
value += " " + line;
}
*/
// fix \n and \'
value = value.replaceAll("\\\\n", "\n");
value = value.replaceAll("\\\\'", "'");
table.put(key, value);
}
}
}
String getString(String key) {
return table.get(key);
}
boolean containsKey(String key) {
return table.containsKey(key);
}
}
}