FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing4/app/src/processing/app/Language.java at main · processing/processing4 · GitHub
processing
processing4
Repository navigation
Code
Issues
259
(259)
Pull requests
31
(31)
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
processing4
/
app
/
src
/
processing
/
app
/
Language.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
454 lines (376 loc) · 13.3 KB
Breadcrumbs
processing4
/
app
/
src
/
processing
/
app
/
Language.java
Copy path
File metadata and controls
454 lines (376 loc) · 13.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/* -*- 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
processing
.
core
.
PApplet
;
import
java
.
io
.
File
;
import
java
.
io
.
IOException
;
import
java
.
io
.
InputStream
;
import
java
.
util
.
HashMap
;
import
java
.
util
.
Locale
;
import
java
.
util
.
Map
;
import
java
.
util
.
MissingResourceException
;
/**
* Internationalization (I18N) and Localization (L10N)
*/
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. List of all support locales:
* https://www.oracle.com/java/technologies/javase/jdk17-suported-locales.html
*/
private
String
language
;
// 2-digit (en) or with country specifier (zh-TW)
/** Available languages */
private
final
Map
<
String
,
String
>
languages
;
private
LanguageBundle
bundle
;
private
Language
() {
language
=
loadLanguage
();
String
origLanguage
=
language
;
// boolean writePrefs = false;
// Set available languages
languages
=
new
HashMap
<>();
for
(
String
code
:
listSupported
()) {
Locale
locale
=
Locale
.
forLanguageTag
(
code
);
// languages.put(code, locale.getDisplayLanguage(locale));
// System.out.println(locale.getDisplayName());
// display name will include the extra suffix information
languages
.
put
(
code
,
locale
.
getDisplayName
(
locale
));
}
// null out the language if unavailable (may already be null)
if
(!
languages
.
containsKey
(
language
)) {
language
=
null
;
}
// if language is null, try to set based on the system language
if
(
language
==
null
) {
Locale
defaultLocale
=
Locale
.
getDefault
();
// String systemLanguage = Locale.getDefault().getLanguage();
String
shortCode
=
defaultLocale
.
getLanguage
();
String
fullCode
=
shortCode
+
"-"
+
defaultLocale
.
getCountry
();
if
(
languages
.
containsKey
(
fullCode
)) {
language
=
fullCode
;
}
else
if
(
languages
.
containsKey
(
shortCode
)) {
language
=
shortCode
;
}
// writePrefs = true;
}
// If still null, fall back to English, sorry.
// if (!languages.containsKey(language)) {
if
(
language
==
null
) {
language
=
"en"
;
// writePrefs = true;
}
// If there was a change, write the file that sets the language
if
(!
language
.
equals
(
origLanguage
)) {
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
() {
var
loader
=
Language
.
class
.
getClassLoader
();
try
(
var
localeFile
=
loader
.
getResourceAsStream
(
"languages/locales.txt"
)) {
return
PApplet
.
loadStrings
(
localeFile
);
}
catch
(
IOException
e
) {
e
.
printStackTrace
();
}
return
new
String
[]{
"en"
};
}
/** Read the saved language */
static
private
String
loadLanguage
() {
try
{
if
(
prefFile
.
exists
()) {
String
[]
lines
=
PApplet
.
loadStrings
(
prefFile
);
if
(
lines
!=
null
&&
lines
.
length
>
0
) {
String
language
=
lines
[
0
].
trim
();
if
(
language
.
length
() !=
0
) {
return
language
;
}
}
System
.
err
.
println
(
"Using default language because of a problem while reading "
+
prefFile
);
}
}
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
);
boolean
ok
=
prefFile
.
setWritable
(
true
,
false
);
if
(!
ok
) {
System
.
err
.
println
(
"Warning: could not set "
+
prefFile
+
" to writable"
);
}
}
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
ignored
) { }
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
;
}
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
;
}
/*
static public String nameToCode(String languageName) {
Map<String, String> languages = Language.getLanguages();
for (Map.Entry<String, String> lang : languages.entrySet()) {
if (lang.getValue().equals(languageName)) {
return lang.getKey().trim().toLowerCase();
}
}
return null; // not found
}
*/
/**
* Get the current language.
* @return two-digit ISO code (lowercase) or extended code (en-US)
*/
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"
));
}
@
SuppressWarnings
(
"unused"
)
static
public
void
addModeStrings
(
Mode
mode
) {
String
baseFilename
=
"languages/mode.properties"
;
File
modeBaseFile
=
new
File
(
mode
.
getFolder
(),
baseFilename
);
if
(
modeBaseFile
.
exists
()) {
init
().
bundle
.
read
(
modeBaseFile
,
true
);
}
String
langFilename
=
"languages/mode_"
+
instance
.
language
+
".properties"
;
File
modeLangFile
=
new
File
(
mode
.
getFolder
(),
langFilename
);
if
(
modeLangFile
.
exists
()) {
init
().
bundle
.
read
(
modeLangFile
,
true
);
}
}
// /** 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
<>();
// Disabling load from sketchbook in 4.2.1, because the path
// is not yet loaded from Preferences when languages are loaded.
// Fixing that (i.e. reloading languages) makes the code a lot
// more complicated for dubious benefit over simply editing the
// language files in the download (i.e. still would not help
// with adding new language codes.)
var
loader
=
Language
.
class
.
getClassLoader
();
String
baseFilename
=
"languages/PDE.properties"
;
String
langFilename
=
"languages/PDE_"
+
language
+
".properties"
;
var
baseFile
=
loader
.
getResourceAsStream
(
baseFilename
);
/*
// Also check to see if the user is working on localization,
// and has their own .properties files in their sketchbook.
// https://github.com/processing/processing4/wiki/Translations
File userBaseFile = new File(Base.getSketchbookFolder(), baseFilename);
if (userBaseFile.exists()) {
baseFile = userBaseFile;
}
*/
var
langFile
=
loader
.
getResourceAsStream
(
langFilename
);
/*
File userLangFile = new File(Base.getSketchbookFolder(), langFilename);
if (userLangFile.exists()) {
langFile = userLangFile;
}
*/
read
(
baseFile
);
read
(
langFile
);
}
void
read
(
File
additions
,
boolean
enforcePrefix
) {
try
{
InputStream
in
=
PApplet
.
createInput
(
additions
);
if
(
in
!=
null
) {
read
(
in
,
enforcePrefix
);
in
.
close
();
}
else
{
System
.
err
.
println
(
"Unable to read "
+
additions
);
}
}
catch
(
IOException
e
) {
e
.
printStackTrace
();
}
}
void
read
(
InputStream
additions
) {
read
(
additions
,
false
);
}
void
read
(
InputStream
additions
,
boolean
enforcePrefix
) {
String
prefix
=
null
;
String
[]
lines
=
PApplet
.
loadStrings
(
additions
);
if
(
lines
!=
null
) {
for
(
String
line
:
lines
) {
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
();
boolean
ignore
=
false
;
if
(
enforcePrefix
) {
if
(
prefix
==
null
) {
prefix
=
key
.
substring
(
0
,
key
.
indexOf
(
'.'
) +
1
);
if
(
prefix
.
length
() ==
0
) {
System
.
err
.
println
(
"Language strings in Modes must include a prefix for all entries."
);
System
.
err
.
println
(
additions
+
" will be ignored."
);
return
;
// exit read()
}
}
else
if
(!
key
.
startsWith
(
prefix
)) {
System
.
err
.
println
(
"Ignoring "
+
key
+
" because all entries in "
+
additions
+
" must begin with "
+
prefix
);
ignore
=
true
;
}
}
if
(!
ignore
) {
String
value
=
line
.
substring
(
equals
+
1
).
trim
();
// Replace \n and \' with their actual values
value
=
value
.
replaceAll
(
"
\\
\\
n"
,
"
\n
"
);
value
=
value
.
replaceAll
(
"
\\
\\
'"
,
"'"
);
table
.
put
(
key
,
value
);
}
}
}
}
else
{
System
.
err
.
println
(
"Unable to read "
+
additions
);
}
}
String
getString
(
String
key
) {
return
table
.
get
(
key
);
}
/*
// removing in 4.0 beta 5; not known to be in use [fry 220130]
boolean containsKey(String key) {
return table.containsKey(key);
}
*/
}
}
Back
|
FazBrowse Home
|
New Git URL