-
Notifications
You must be signed in to change notification settings - Fork 497
/
Localization.java
140 lines (120 loc) · 4.1 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.utils;
import buildcraft.core.proxy.CoreProxy;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.Properties;
/**
* Simple mod localization class.
*/
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() == null) {
return 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));
}
public static synchronized boolean hasKey(String key) {
if (getCurrentLanguage() == null) {
return false;
}
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)) != null;
}
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();
}
}