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