001package cpw.mods.fml.common.registry;
002
003import java.io.IOException;
004import java.io.InputStream;
005import java.io.InputStreamReader;
006import java.net.URL;
007import java.util.HashMap;
008import java.util.Map;
009import java.util.Properties;
010import java.util.logging.Level;
011
012import com.google.common.base.Charsets;
013
014import cpw.mods.fml.common.FMLLog;
015import cpw.mods.fml.common.Loader;
016import cpw.mods.fml.common.ModContainer;
017
018import net.minecraft.block.Block;
019import net.minecraft.item.Item;
020import net.minecraft.item.ItemStack;
021import net.minecraft.util.StringTranslate;
022
023public class LanguageRegistry
024{
025    private static final LanguageRegistry INSTANCE = new LanguageRegistry();
026
027    private Map<String,Properties> modLanguageData=new HashMap<String,Properties>();
028
029    public static LanguageRegistry instance()
030    {
031        return INSTANCE;
032    }
033
034    public String getStringLocalization(String key)
035    {
036        return getStringLocalization(key, StringTranslate.getInstance().getCurrentLanguage());
037    }
038
039    public String getStringLocalization(String key, String lang)
040    {
041        String localizedString = "";
042        Properties langPack = modLanguageData.get(lang);
043
044        if (langPack != null) {
045            if (langPack.getProperty(key) != null) {
046                localizedString = langPack.getProperty(key);
047            }
048        }
049
050        return localizedString;
051    }
052
053    public void addStringLocalization(String key, String value)
054    {
055        addStringLocalization(key, "en_US", value);
056    }
057    public void addStringLocalization(String key, String lang, String value)
058    {
059        Properties langPack=modLanguageData.get(lang);
060        if (langPack==null) {
061            langPack=new Properties();
062            modLanguageData.put(lang, langPack);
063        }
064        langPack.put(key,value);
065    }
066
067    public void addStringLocalization(Properties langPackAdditions) {
068        addStringLocalization(langPackAdditions, "en_US");
069    }
070
071    public void addStringLocalization(Properties langPackAdditions, String lang) {
072        Properties langPack = modLanguageData.get(lang);
073        if (langPack == null) {
074            langPack = new Properties();
075            modLanguageData.put(lang, langPack);
076        }
077        if (langPackAdditions != null) {
078            langPack.putAll(langPackAdditions);
079        }
080    }
081
082    public static void reloadLanguageTable()
083    {
084        // reload language table by forcing lang to null and reloading the properties file
085        String lang = StringTranslate.getInstance().getCurrentLanguage();
086        StringTranslate.getInstance().currentLanguage = null;
087        StringTranslate.getInstance().setLanguage(lang);
088    }
089
090
091    public void addNameForObject(Object objectToName, String lang, String name)
092    {
093        String objectName;
094        if (objectToName instanceof Item) {
095            objectName=((Item)objectToName).getItemName();
096        } else if (objectToName instanceof Block) {
097            objectName=((Block)objectToName).getBlockName();
098        } else if (objectToName instanceof ItemStack) {
099            objectName=((ItemStack)objectToName).getItem().getItemNameIS((ItemStack)objectToName);
100        } else {
101            throw new IllegalArgumentException(String.format("Illegal object for naming %s",objectToName));
102        }
103        objectName+=".name";
104        addStringLocalization(objectName, lang, name);
105    }
106
107    public static void addName(Object objectToName, String name)
108    {
109        instance().addNameForObject(objectToName, "en_US", name);
110    }
111
112    public void loadLanguageTable(Properties languagePack, String lang)
113    {
114        Properties usPack=modLanguageData.get("en_US");
115        if (usPack!=null) {
116            languagePack.putAll(usPack);
117        }
118        Properties langPack=modLanguageData.get(lang);
119        if (langPack==null) {
120            return;
121        }
122        languagePack.putAll(langPack);
123    }
124
125    public void loadLocalization(String localizationFile, String lang, boolean isXML)
126    {
127        URL urlResource = this.getClass().getResource(localizationFile);
128        if (urlResource != null)
129        {
130            loadLocalization(urlResource, lang, isXML);
131        }
132        else
133        {
134            ModContainer activeModContainer = Loader.instance().activeModContainer();
135            if (activeModContainer!=null)
136            {
137                FMLLog.log(activeModContainer.getModId(), Level.SEVERE, "The language resource %s cannot be located on the classpath. This is a programming error.", localizationFile);
138            }
139            else
140            {
141                FMLLog.log(Level.SEVERE, "The language resource %s cannot be located on the classpath. This is a programming error.", localizationFile);
142            }
143        }
144    }
145
146    public void loadLocalization(URL localizationFile, String lang, boolean isXML)
147    {
148        InputStream langStream = null;
149        Properties langPack = new Properties();
150
151        try    {
152            langStream = localizationFile.openStream();
153
154            if (isXML) {
155                langPack.loadFromXML(langStream);
156            }
157            else {
158                langPack.load(new InputStreamReader(langStream,Charsets.UTF_8));
159            }
160
161            addStringLocalization(langPack, lang);
162        }
163        catch (IOException e) {
164            FMLLog.log(Level.SEVERE, e, "Unable to load localization from file %s", localizationFile);
165        }
166        finally    {
167            try    {
168                if (langStream != null)    {
169                    langStream.close();
170                }
171            }
172            catch (IOException ex) {
173                // HUSH
174            }
175        }
176    }
177}