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