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
|
package gregtech.api.util;
import static gregtech.api.enums.GT_Values.E;
import gregtech.api.GregTech_API;
import java.util.HashMap;
import java.util.Map.Entry;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.StatCollector;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;
import cpw.mods.fml.common.registry.LanguageRegistry;
public class GT_LanguageManager {
public static Configuration sEnglishFile;
public static final HashMap<String, String> TEMPMAP = new HashMap<String, String>(), BUFFERMAP = new HashMap<String, String>();
public static String addStringLocalization(String aKey, String aEnglish) {
return addStringLocalization(aKey, aEnglish, true);
}
public static String addStringLocalization(String aKey, String aEnglish, boolean aWriteIntoLangFile) {
if (aKey == null) return E;
if (aWriteIntoLangFile) aEnglish = writeToLangFile(aKey, aEnglish);
TEMPMAP.put(aKey.trim(), aEnglish);
LanguageRegistry.instance().injectLanguage("en_US", TEMPMAP);
TEMPMAP.clear();
return aEnglish;
}
private static synchronized String writeToLangFile(String aKey, String aEnglish) {
if (aKey == null) return E;
if (sEnglishFile == null) {
BUFFERMAP.put(aKey.trim(), aEnglish);
} else {
if (!BUFFERMAP.isEmpty()) {
for (Entry<String, String> tEntry : BUFFERMAP.entrySet()) {
Property tProperty = sEnglishFile.get("LanguageFile", tEntry.getKey(), tEntry.getValue());
if (!tProperty.wasRead() && GregTech_API.sPostloadFinished) sEnglishFile.save();
}
BUFFERMAP.clear();
}
Property tProperty = sEnglishFile.get("LanguageFile", aKey.trim(), aEnglish);
if (!tProperty.wasRead() && GregTech_API.sPostloadFinished) sEnglishFile.save();
if (sEnglishFile.get("EnableLangFile", "UseThisFileAsLanguageFile", false).getBoolean(false)) aEnglish = tProperty.getString();
}
return aEnglish;
}
public static String getTranslation(String aKey) {
if (aKey == null) return E;
String tTrimmedKey = aKey.trim(), rTranslation = LanguageRegistry.instance().getStringLocalization(tTrimmedKey);
if (GT_Utility.isStringInvalid(rTranslation)) {
rTranslation = StatCollector.translateToLocal(tTrimmedKey);
if (GT_Utility.isStringInvalid(rTranslation) || tTrimmedKey.equals(rTranslation)) {
if (aKey.endsWith(".name")) {
rTranslation = StatCollector.translateToLocal(tTrimmedKey.substring(0, tTrimmedKey.length() - 5));
if (GT_Utility.isStringInvalid(rTranslation) || tTrimmedKey.substring(0, tTrimmedKey.length() - 5).equals(rTranslation)) {
return aKey;
}
} else {
rTranslation = StatCollector.translateToLocal(tTrimmedKey + ".name");
if (GT_Utility.isStringInvalid(rTranslation) || (tTrimmedKey + ".name").equals(rTranslation)) {
return aKey;
}
}
}
}
return rTranslation;
}
public static String getTranslation(String aKey, String aSeperator) {
if (aKey == null) return E;
String rTranslation = E;
for (String tString : aKey.split(aSeperator)) {
rTranslation += getTranslation(tString);
}
return rTranslation;
}
public static String getTranslateableItemStackName(ItemStack aStack) {
if (GT_Utility.isStackInvalid(aStack)) return "null";
NBTTagCompound tNBT = aStack.getTagCompound();
if (tNBT != null && tNBT.hasKey("display")) {
String tName = tNBT.getCompoundTag("display").getString("Name");
if (GT_Utility.isStringValid(tName)) {
return tName;
}
}
return aStack.getUnlocalizedName() + ".name";
}
}
|