⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cblocalization.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        {
            return result;                                      //TE: The locale is "", "", "".
        }

        temp.append(language);

        result.addElement(temp.toString() + ".properties");     //TE: file name is [language].properties
        result.addElement(temp.toString());                     //TE: file name is [language]

        if (countryLength + variantLength == 0)
        {
            return result;                                      //TE: The locale is for example "en", "", "".
        }

        temp.append('_');
        temp.append(country);

        result.addElement(temp.toString() + ".properties");     //TE: file name is [language]_[country].properties
        result.addElement(temp.toString());                     //TE: file name is [language]_[country]

        if (variantLength == 0)
        {
            return result;                                      //TE: The locale is for example "en", "AU", "".
        }

        temp.append('_');
        temp.append(variant);

        result.addElement(temp.toString() + ".properties");     //TE: file name is [language]_[country]_[variant].properties
        result.addElement(temp.toString());                     //TE: file name is [language]_[country]_[variant]

        return result;                                          //TE: The locale is for example "en", "AU", "variant".
    }

    /**
     * This loads the data from a translation file, checking on the way
     * what file format it is in.  (UTF-8, 16bit unicode, or local encoding).
     *
     * @param file the File to read the data InputStream from.
     * @return whether the load data operation was successfull, or whether
     *         it was interupted (for whatever reason; no file, error reading
     *         file, bad encoding, yadda yadda yadda).
     */
    private boolean loadData(File file)
    {
        try
        {
            if (file == null || !file.exists())
                return false;

            log.info("Reading data from " + file.getAbsolutePath());

            // First, slurp all the data from the input stream into a byte array...
            byte[] data = CBUtility.readStream(new FileInputStream(file));

            // Convert the byte array to a String using cunning auto-detecting encoding methods...
            StringBuffer buffy = new StringBuffer(CBUtility.readI18NByteArray(data));

            buffy.insert(buffy.length(), '\n');   //TE: make sure the last line has a '\n'!

            // Load up the translations hashtable with the parsed data found in the string...
            return parseData(buffy.toString());
        }
        catch (Exception e)
        {
            log.log(Level.WARNING, "Unable to read data from file '" + file.getAbsolutePath(), e);
            return false;
        }
    }

    /**
     * Parses the byte array as per a normal resource file
     * (i.e. looking for key/data pairs seperated by an
     * unescaped '=' sign) after first converting the byte
     * array into a String, using whichever language encoding
     * (unicode16, utf8, locale-specific) seems appropriate.
     *
     * @param text the language file as a string.
     * @return true if the language file has been loaded into the
     *         hashtable, false otherwise.
     */
    protected boolean parseData(String text)
    {
        int start = 0, end = 0;
        while ((end = text.indexOf('\n', start)) != -1)
        {
            String line = text.substring(start, end);

            line = line.trim();

            if (line.startsWith("="))
            {
                log.warning("Invalid entry in language file: '" + line + "'");
            }
            else if (line.length() != 0 && line.charAt(0) != '#') // ignore blank lines and commented lines.
            {
                try
                {
                    int equalPos = 0;

                    do         // skip through all escaped equals characters until we find a non-escaped one.
                    {
                        equalPos = line.indexOf('=', equalPos + 1);
                    }
                    while (line.charAt(equalPos - 1) == '\\');

                    String key = unescape(line.substring(0, equalPos)).trim();
                    String trans = line.substring(equalPos + 1).trim();
                    translations.put(key, trans);

                }
                catch (Exception e)
                {
                    log.warning("Exception parsing data line '" + line + "' -> " + e);
                } // prob. array ex. - ignore this line.
            }

            start = end + 1;
        }

        // check if we added any new translations - if we did, then this was
        // at least partially successfull.
        boolean success = (translations.size() > 0);
        if (success == false)
            log.warning("ParseData unsuccessfull - no new data found");
        return success;
    }

    /**
     * Removes all escapes ('\?' -> '?') from a string.
     * -> Not particularly efficient, but o.k. for short strings.
     */
    private String unescape(String escapeMe)
    {
        int pos = 0;
        while ((pos = escapeMe.indexOf('\\', pos)) >= 0)
            escapeMe = escapeMe.substring(0, pos) + escapeMe.substring(pos + 1);

        return escapeMe;
    }

    /**
     * Returns the translation keys.
     *
     * @return an Enumeration of all the known keys (usually translatable
     *         strings).
     */
    public Enumeration keys()
    {
        return translations.keys();
    }

    /**
     * Returns the translation keys.
     *
     * @return an Enumeration of all the known keys (usually translatable
     *         strings).
     */
    public Enumeration getKeys()
    {
        return translations.keys();
    }

    /**
     * Returns the object corresponding to a given key.
     *
     * @param key the original text to translate/look up
     * @return the corresponding translation/object
     */
    public Object get(Object key)
    {
        return translations.get(key);
    }

    /**
     * Returns the object corresponding to a given key.
     *
     * @param key the original text to translate/look up
     * @return the corresponding translation/object
     */
    public Object getObject(Object key)
    {
        return translations.get(key);
    }

    /**
     * Convenience class returning a particular object
     * as a String.  If the object <i>was</i> a String
     * already it is passed back unchanged, otherwise
     * 'toString()' is called on the object before returning.
     * This class never throws a ClassCastException.
     *
     * @param key the original text to translate/look up
     * @return the corresponding translation/object as a String
     */
    public String getString(String key)
    {
        if (key == null) return "";
        Object o = translations.get(key);
        if (o == null) return "";

        return (o instanceof String) ? (String) o : o.toString();
    }

    /**
     * Checks if the country code is a valid one according to
     * Locale.getISOCountries().
     *
     * @param language the country code.
     * @return if the newly created Locale or null if the country
     *         code is not valid
     */
    public static Locale createLocale(String language)
    {
        if (language == null)
            return Locale.getDefault();

        String[] langs = Locale.getISOLanguages();
        for (int i = 0; i < langs.length; i++)
            if (language.equalsIgnoreCase(langs[i]))
                return new Locale(langs[i]);

        return Locale.getDefault();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -