    001package net.minecraft.util;
    002
    003import cpw.mods.fml.common.registry.LanguageRegistry;
    004import cpw.mods.fml.relauncher.Side;
    005import cpw.mods.fml.relauncher.SideOnly;
    006import java.io.BufferedReader;
    007import java.io.IOException;
    008import java.io.InputStreamReader;
    009import java.util.Enumeration;
    010import java.util.IllegalFormatException;
    011import java.util.Properties;
    012import java.util.TreeMap;
    013
    014public class StringTranslate
    015{
    016    /** Is the private singleton instance of StringTranslate. */
    017    private static StringTranslate instance = new StringTranslate("en_US");
    018
    019    /**
    020     * Contains all key/value pairs to be translated - is loaded from '/lang/en_US.lang' when the StringTranslate is
    021     * created.
    022     */
    023    public Properties translateTable = new Properties();
    024    private TreeMap languageList;
    025    public String currentLanguage;
    026    private boolean isUnicode;
    027
    028    public StringTranslate(String par1Str)
    029    {
    030        this.loadLanguageList();
    031        this.setLanguage(par1Str);
    032    }
    033
    034    /**
    035     * Return the StringTranslate singleton instance
    036     */
    037    public static StringTranslate getInstance()
    038    {
    039        return instance;
    040    }
    041
    042    private void loadLanguageList()
    043    {
    044        TreeMap var1 = new TreeMap();
    045
    046        try
    047        {
    048            BufferedReader var2 = new BufferedReader(new InputStreamReader(StringTranslate.class.getResourceAsStream("/lang/languages.txt"), "UTF-8"));
    049
    050            for (String var3 = var2.readLine(); var3 != null; var3 = var2.readLine())
    051            {
    052                String[] var4 = var3.split("=");
    053
    054                if (var4 != null && var4.length == 2)
    055                {
    056                    var1.put(var4[0], var4[1]);
    057                }
    058            }
    059        }
    060        catch (IOException var5)
    061        {
    062            var5.printStackTrace();
    063            return;
    064        }
    065
    066        this.languageList = var1;
    067        this.languageList.put("en_US", "English (US)");
    068    }
    069
    070    public TreeMap getLanguageList()
    071    {
    072        return this.languageList;
    073    }
    074
    075    private void loadLanguage(Properties par1Properties, String par2Str) throws IOException
    076    {
    077        BufferedReader var3 = new BufferedReader(new InputStreamReader(StringTranslate.class.getResourceAsStream("/lang/" + par2Str + ".lang"), "UTF-8"));
    078
    079        for (String var4 = var3.readLine(); var4 != null; var4 = var3.readLine())
    080        {
    081            var4 = var4.trim();
    082
    083            if (!var4.startsWith("#"))
    084            {
    085                String[] var5 = var4.split("=");
    086
    087                if (var5 != null && var5.length == 2)
    088                {
    089                    par1Properties.setProperty(var5[0], var5[1]);
    090                }
    091            }
    092        }
    093        LanguageRegistry.instance().loadLanguageTable(par1Properties, par2Str);
    094    }
    095
    096    public void setLanguage(String par1Str)
    097    {
    098        if (!par1Str.equals(this.currentLanguage))
    099        {
    100            Properties var2 = new Properties();
    101
    102            try
    103            {
    104                this.loadLanguage(var2, "en_US");
    105            }
    106            catch (IOException var8)
    107            {
    108                ;
    109            }
    110
    111            this.isUnicode = false;
    112
    113            if (!"en_US".equals(par1Str))
    114            {
    115                try
    116                {
    117                    this.loadLanguage(var2, par1Str);
    118                    Enumeration var3 = var2.propertyNames();
    119
    120                    while (var3.hasMoreElements() && !this.isUnicode)
    121                    {
    122                        Object var4 = var3.nextElement();
    123                        Object var5 = var2.get(var4);
    124
    125                        if (var5 != null)
    126                        {
    127                            String var6 = var5.toString();
    128
    129                            for (int var7 = 0; var7 < var6.length(); ++var7)
    130                            {
    131                                if (var6.charAt(var7) >= 256)
    132                                {
    133                                    this.isUnicode = true;
    134                                    break;
    135                                }
    136                            }
    137                        }
    138                    }
    139                }
    140                catch (IOException var9)
    141                {
    142                    var9.printStackTrace();
    143                    return;
    144                }
    145            }
    146
    147            this.currentLanguage = par1Str;
    148            this.translateTable = var2;
    149        }
    150    }
    151
    152    public String getCurrentLanguage()
    153    {
    154        return this.currentLanguage;
    155    }
    156
    157    @SideOnly(Side.CLIENT)
    158    public boolean isUnicode()
    159    {
    160        return this.isUnicode;
    161    }
    162
    163    /**
    164     * Translate a key to current language.
    165     */
    166    public String translateKey(String par1Str)
    167    {
    168        return this.translateTable.getProperty(par1Str, par1Str);
    169    }
    170
    171    /**
    172     * Translate a key to current language applying String.format()
    173     */
    174    public String translateKeyFormat(String par1Str, Object ... par2ArrayOfObj)
    175    {
    176        String var3 = this.translateTable.getProperty(par1Str, par1Str);
    177
    178        try
    179        {
    180            return String.format(var3, par2ArrayOfObj);
    181        }
    182        catch (IllegalFormatException var5)
    183        {
    184            return "Format error: " + var3;
    185        }
    186    }
    187
    188    /**
    189     * Translate a key with a extra '.name' at end added, is used by blocks and items.
    190     */
    191    public String translateNamedKey(String par1Str)
    192    {
    193        return this.translateTable.getProperty(par1Str + ".name", "");
    194    }
    195
    196    @SideOnly(Side.CLIENT)
    197    public static boolean isBidirectional(String par0Str)
    198    {
    199        return "ar_SA".equals(par0Str) || "he_IL".equals(par0Str);
    200    }
    201}