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