-
Notifications
You must be signed in to change notification settings - Fork 497
/
Localization.java
118 lines (98 loc) · 3.3 KB
/
Localization.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package buildcraft.core.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.Properties;
import buildcraft.core.proxy.CoreProxy;
/**
* Simple mod localization class.
*
* @author Jimeo Wan
* @license Public domain
*/
public class Localization {
private static class modInfo {
final String modName, defaultLanguage;
public modInfo(String modName, String defaultLanguage) {
this.modName = modName;
this.defaultLanguage = defaultLanguage;
}
}
private static String loadedLanguage = getCurrentLanguage();
private static Properties defaultMappings = new Properties();
private static Properties mappings = new Properties();
private static LinkedList<modInfo> mods = new LinkedList<modInfo>();
/**
* Adds localization from a given directory. The files must have the same
* name as the corresponding language file in minecraft and a ".properties"
* file extention e.g "en_US.properties"
*
* @param path The path to the localization files
* @param defaultLanguage The default localization to be used when there is
* no localization for the selected language or if a string is missing (e.g.
* "en_US")
*/
public static void addLocalization(String path, String defaultLanguage) {
mods.add(new modInfo(path, defaultLanguage));
load(path, defaultLanguage);
}
/**
* Get a string for the given key, in the currently active translation.
*
* @param key
* @return
*/
public static synchronized String get(String key) {
if (!getCurrentLanguage().equals(loadedLanguage)) {
defaultMappings.clear();
mappings.clear();
for (modInfo mInfo : mods) {
load(mInfo.modName, mInfo.defaultLanguage);
}
loadedLanguage = getCurrentLanguage();
}
return mappings.getProperty(key, defaultMappings.getProperty(key, key));
}
private static void load(String path, String default_language) {
InputStream langStream = null;
Properties modMappings = new Properties();
try {
//Load the default language mappings
langStream = Localization.class.getResourceAsStream(path + default_language + ".properties");
modMappings.load(langStream);
defaultMappings.putAll(modMappings);
langStream.close();
//Try to load the current language mappings.
//If the file doesn't exist use the default mappings.
langStream = Localization.class.getResourceAsStream(path + getCurrentLanguage() + ".properties");
if (langStream != null) {
modMappings.clear();
modMappings.load(langStream);
}
//If the selected language inherits mappings from another language
//we use those first and then we overwrite the common ones with
//those in the selected language
if (modMappings.containsKey("language.parent")) {
langStream = Localization.class.getResourceAsStream(path + modMappings.get("language.parent") + ".properties");
if (langStream != null) {
Properties parentModMappings = new Properties();
parentModMappings.load(langStream);
mappings.putAll(parentModMappings);
}
}
mappings.putAll(modMappings);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (langStream != null)
langStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private static String getCurrentLanguage() {
return CoreProxy.proxy.getCurrentLanguage();
}
}