-
-
Notifications
You must be signed in to change notification settings - Fork 7k
/
Copy pathI18n.java
96 lines (82 loc) · 2.76 KB
/
I18n.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
/*
* by Shigeru KANEMOTO at SWITCHSCIENCE.
*
* Extract strings to be translated by:
* % xgettext -L Java --from-code=utf-8 -k_ -d Resources_ja *.java
* Extract and merge by:
* % xgettext -j -L Java --from-code=utf-8 -k_ -d Resources_ja *.java
*
* Edit "Resources_ja.po".
* Convert to the properties file format by:
* % msgcat -p Resources_ja.po > Resources_ja.properties
*/
package processing.app;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
public class I18n {
// start using current locale but still allow using the dropdown list later
private static ResourceBundle i18n;
// prompt text stuff
static String PROMPT_YES;
static String PROMPT_NO;
static String PROMPT_CANCEL;
static String PROMPT_OK;
static String PROMPT_BROWSE;
static protected void init(String language) throws MissingResourceException {
String[] languageParts = language.split("_");
Locale locale = Locale.getDefault();
// both language and country
if (languageParts.length == 2) {
locale = new Locale(languageParts[0], languageParts[1]);
// just language
} else if (languageParts.length == 1 && !"".equals(languageParts[0])) {
locale = new Locale(languageParts[0]);
}
// there might be a null pointer exception ... most likely will never happen but the jvm gets mad
Locale.setDefault(locale);
i18n = ResourceBundle.getBundle("processing.app.i18n.Resources", Locale.getDefault());
PROMPT_YES = _("Yes");
PROMPT_NO = _("No");
PROMPT_CANCEL = _("Cancel");
PROMPT_OK = _("OK");
PROMPT_BROWSE = _("Browse");
}
public static String _(String s) {
String res;
try {
if (i18n == null)
res = s;
else
res = i18n.getString(s);
} catch (MissingResourceException e) {
res = s;
}
// The single % is the arguments selector in .PO files.
// We must put double %% inside the translations to avoid
// getting .PO processing in the way.
res = res.replace("%%", "%");
return res;
}
public static String format(String fmt, Object... args) {
// Single quote is used to escape curly bracket arguments.
// - Prevents strings fixed at translation time to be fixed again
fmt = fmt.replace("''", "'");
// - Replace ' with the escaped version ''
fmt = fmt.replace("'", "''");
return MessageFormat.format(fmt, args);
}
/**
* Does nothing.
* <p/>
* This method is an hack to extract words with gettext tool.
*/
protected static void unusedStrings() {
// These phrases are defined in the "platform.txt".
_("Arduino AVR Boards");
_("Arduino ARM (32-bits) Boards");
// This word is defined in the "boards.txt".
_("Processor");
}
}