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