1
- package cat .nyaa .utils ;
1
+ package cat .nyaa .nyaacore ;
2
2
3
- import cat .nyaa .nyaautils .NyaaUtils ;
4
3
import org .bukkit .ChatColor ;
5
4
import org .bukkit .configuration .ConfigurationSection ;
6
5
import org .bukkit .configuration .file .YamlConfiguration ;
16
15
import java .util .Map ;
17
16
18
17
public abstract class Internationalization {
19
- private final String DEFAULT_LANGUAGE = "en_US" ;
20
- private final Map <String , String > map = new HashMap <>();
21
- // internal language keys will only be loaded from NyaaUtils
22
- // but share across all dependent plugins
23
- // TODO do not depend on NyaaUtils
24
- // TODO HEH load before NU ?!
18
+ /**
19
+ * Use English as default & fallback language
20
+ */
21
+ private static final String DEFAULT_LANGUAGE = "en_US" ;
22
+ /**
23
+ * Internal language map is loaded and should only be loaded by {@link NyaaCoreLoader#onLoad()}
24
+ * Once it's loaded, it cannot be reloaded or modified.
25
+ * The internal map will be shared across all plugins using {@link Internationalization}
26
+ */
25
27
private final static Map <String , String > internalMap = new HashMap <>();
28
+ /**
29
+ * Per-plugin language map used by {@link Internationalization}
30
+ * This map has a higher priority than {@link this#internalMap}
31
+ * So it's possible to overwrite some internal language keys here.
32
+ */
33
+ private final Map <String , String > map = new HashMap <>();
26
34
35
+ /**
36
+ * Dependent plugins should provide the "main" class instance
37
+ * So that language files could be loaded automatically
38
+ *
39
+ * @return the plugin class instance
40
+ */
27
41
protected abstract JavaPlugin getPlugin ();
28
42
43
+ /**
44
+ * @return the language to be loaded into {@link this#map}
45
+ */
29
46
protected abstract String getLanguage ();
30
47
48
+
49
+ /**
50
+ * Reset then load per-plugin language map
51
+ * Based on {@link this#getPlugin()} and {@link this#getLanguage()}
52
+ */
31
53
public void load () {
54
+ map .clear ();
32
55
String language = getLanguage ();
33
56
JavaPlugin plugin = getPlugin ();
34
57
if (language == null ) language = DEFAULT_LANGUAGE ;
35
- // internal map
36
- if (plugin instanceof NyaaUtils ) {
37
- loadInternalMap ((NyaaUtils ) plugin , language );
38
- }
39
58
// language map
40
59
loadLanguageMap (plugin , language );
41
60
// save (probably) modified language file back to disk
42
- saveLanguageMap (plugin , language );
61
+ saveLanguageMap ();
43
62
44
63
plugin .getLogger ().info (get ("internal.info.using_language" , language ));
45
64
}
46
65
47
- private void loadInternalMap (NyaaUtils plugin , String language ) {
48
- internalMap .clear ();
49
- boolean forceJar = System .getProperty ("nyaautils.i18n.loadFromDisk" , "true" ).equals ("false" );
50
- if (!forceJar ) { // load from disk
51
- File localLangFile = new File (plugin .getDataFolder (), language + ".yml" );
52
- if (localLangFile .exists ()) {
53
- ConfigurationSection section = YamlConfiguration .loadConfiguration (localLangFile );
54
- loadLanguageSection (internalMap , section .getConfigurationSection ("internal" ), "internal." , false );
66
+ /**
67
+ * Load per-plugin language map
68
+ * Based on {@link this#getPlugin()} and {@link this#getLanguage()}
69
+ */
70
+ public void saveLanguageMap () {
71
+ JavaPlugin plugin = getPlugin ();
72
+ String language = getLanguage ();
73
+ File localLangFile = new File (plugin .getDataFolder (), language + ".yml" );
74
+ try {
75
+ YamlConfiguration yaml = new YamlConfiguration ();
76
+ for (String key : map .keySet ()) {
77
+ yaml .set (key , map .get (key ));
55
78
}
56
- }
57
- InputStream stream = plugin .getResource ("lang/" + language + ".yml" );
58
- if (stream != null ) {
59
- ConfigurationSection section = YamlConfiguration .loadConfiguration (new InputStreamReader (stream ));
60
- loadLanguageSection (internalMap , section .getConfigurationSection ("internal" ), "internal." , false );
61
- }
62
- stream = plugin .getResource ("lang/" + DEFAULT_LANGUAGE + ".yml" );
63
- if (stream != null ) {
64
- ConfigurationSection section = YamlConfiguration .loadConfiguration (new InputStreamReader (stream ));
65
- loadLanguageSection (internalMap , section .getConfigurationSection ("internal" ), "internal." , false );
79
+ yaml .save (localLangFile );
80
+ } catch (IOException ex ) {
81
+ plugin .getLogger ().warning ("Cannot save language file: " + language + ".yml" );
66
82
}
67
83
}
68
84
@@ -87,24 +103,6 @@ private void loadLanguageMap(JavaPlugin plugin, String language) {
87
103
loadLanguageSection (map , YamlConfiguration .loadConfiguration (new InputStreamReader (stream )), "" , true );
88
104
}
89
105
90
- private void saveLanguageMap (JavaPlugin plugin , String language ) {
91
- File localLangFile = new File (plugin .getDataFolder (), language + ".yml" );
92
- try {
93
- YamlConfiguration yaml = new YamlConfiguration ();
94
- for (String key : map .keySet ()) {
95
- yaml .set (key , map .get (key ));
96
- }
97
- if (plugin instanceof NyaaUtils ) { // save internal section if is NyaaUtils
98
- for (String key : internalMap .keySet ()) {
99
- yaml .set (key , internalMap .get (key ));
100
- }
101
- }
102
- yaml .save (localLangFile );
103
- } catch (IOException ex ) {
104
- plugin .getLogger ().warning ("Cannot save language file: " + language + ".yml" );
105
- }
106
- }
107
-
108
106
/**
109
107
* add all language items from section into language map recursively
110
108
* existing items won't be overwritten
@@ -113,7 +111,7 @@ private void saveLanguageMap(JavaPlugin plugin, String language) {
113
111
* @param prefix used in recursion to determine the proper prefix
114
112
* @param ignoreInternal ignore keys prefixed with `internal'
115
113
*/
116
- private void loadLanguageSection (Map <String , String > map , ConfigurationSection section , String prefix , boolean ignoreInternal ) {
114
+ private static void loadLanguageSection (Map <String , String > map , ConfigurationSection section , String prefix , boolean ignoreInternal , JavaPlugin plugin ) {
117
115
if (map == null || section == null || prefix == null ) return ;
118
116
for (String key : section .getKeys (false )) {
119
117
String path = prefix + key ;
@@ -122,13 +120,49 @@ private void loadLanguageSection(Map<String, String> map, ConfigurationSection s
122
120
map .put (path , ChatColor .translateAlternateColorCodes ('&' , section .getString (key )));
123
121
}
124
122
} else if (section .isConfigurationSection (key )) {
125
- loadLanguageSection (map , section .getConfigurationSection (key ), path + "." , ignoreInternal );
123
+ loadLanguageSection (map , section .getConfigurationSection (key ), path + "." , ignoreInternal , plugin );
126
124
} else {
127
- getPlugin () .getLogger ().warning ("Skipped language section: " + path );
125
+ plugin .getLogger ().warning ("Skipped language section: " + path );
128
126
}
129
127
}
130
128
}
131
129
130
+ private void loadLanguageSection (Map <String , String > map , ConfigurationSection section , String prefix , boolean ignoreInternal ) {
131
+ loadLanguageSection (map , section , prefix , ignoreInternal , getPlugin ());
132
+ }
133
+
134
+
135
+ /**
136
+ * Load the internal map
137
+ * should only be called from {@link NyaaCoreLoader#onLoad()}
138
+ *
139
+ * @param plugin the NyaaCore plugin
140
+ */
141
+ static void loadInternalMap (NyaaCoreLoader plugin ) {
142
+ loadInternalMap (plugin , DEFAULT_LANGUAGE );
143
+ }
144
+
145
+ private static void loadInternalMap (NyaaCoreLoader plugin , String language ) {
146
+ internalMap .clear ();
147
+ boolean forceJar = System .getProperty ("nyaautils.i18n.loadFromDisk" , "true" ).equals ("false" );
148
+ if (!forceJar ) { // load from disk
149
+ File localLangFile = new File (plugin .getDataFolder (), language + ".yml" );
150
+ if (localLangFile .exists ()) {
151
+ ConfigurationSection section = YamlConfiguration .loadConfiguration (localLangFile );
152
+ loadLanguageSection (internalMap , section .getConfigurationSection ("internal" ), "internal." , false , plugin );
153
+ }
154
+ }
155
+ InputStream stream = plugin .getResource ("lang/" + language + ".yml" );
156
+ if (stream != null ) {
157
+ ConfigurationSection section = YamlConfiguration .loadConfiguration (new InputStreamReader (stream ));
158
+ loadLanguageSection (internalMap , section .getConfigurationSection ("internal" ), "internal." , false , plugin );
159
+ }
160
+ stream = plugin .getResource ("lang/" + DEFAULT_LANGUAGE + ".yml" );
161
+ if (stream != null ) {
162
+ ConfigurationSection section = YamlConfiguration .loadConfiguration (new InputStreamReader (stream ));
163
+ loadLanguageSection (internalMap , section .getConfigurationSection ("internal" ), "internal." , false , plugin );
164
+ }
165
+ }
132
166
133
167
public String get (@ LangKey String key , Object ... para ) {
134
168
String val = map .get (key );
@@ -141,9 +175,9 @@ public String get(@LangKey String key, Object... para) {
141
175
}
142
176
return keyBuilder .toString ();
143
177
} else {
144
- try {
178
+ try {
145
179
return String .format (val , para );
146
- } catch (IllegalFormatConversionException e ){
180
+ } catch (IllegalFormatConversionException e ) {
147
181
e .printStackTrace ();
148
182
getPlugin ().getLogger ().warning ("Corrupted language key: " + key );
149
183
getPlugin ().getLogger ().warning ("val: " + val );
@@ -161,9 +195,4 @@ public String get(@LangKey String key, Object... para) {
161
195
public boolean hasKey (String key ) {
162
196
return map .containsKey (key ) || internalMap .containsKey (key );
163
197
}
164
-
165
- public void reset () {
166
- if (getPlugin () instanceof NyaaUtils ) internalMap .clear ();
167
- map .clear ();
168
- }
169
198
}
0 commit comments