How to use UTF-8 in resource properties with ResourceBundle

后端 未结 16 2388
难免孤独
难免孤独 2020-11-22 03:28

I need to use UTF-8 in my resource properties using Java\'s ResourceBundle. When I enter the text directly into the properties file, it displays as mojibake.

16条回答
  •  粉色の甜心
    2020-11-22 03:37

    package com.varaneckas.utils;  
    
    import java.io.UnsupportedEncodingException;  
    import java.util.Enumeration;  
    import java.util.PropertyResourceBundle;  
    import java.util.ResourceBundle;  
    
    /** 
     * UTF-8 friendly ResourceBundle support 
     *  
     * Utility that allows having multi-byte characters inside java .property files. 
     * It removes the need for Sun's native2ascii application, you can simply have 
     * UTF-8 encoded editable .property files. 
     *  
     * Use:  
     * ResourceBundle bundle = Utf8ResourceBundle.getBundle("bundle_name"); 
     *  
     * @author Tomas Varaneckas  
     */  
    public abstract class Utf8ResourceBundle {  
    
        /** 
         * Gets the unicode friendly resource bundle 
         *  
         * @param baseName 
         * @see ResourceBundle#getBundle(String) 
         * @return Unicode friendly resource bundle 
         */  
        public static final ResourceBundle getBundle(final String baseName) {  
            return createUtf8PropertyResourceBundle(  
                    ResourceBundle.getBundle(baseName));  
        }  
    
        /** 
         * Creates unicode friendly {@link PropertyResourceBundle} if possible. 
         *  
         * @param bundle  
         * @return Unicode friendly property resource bundle 
         */  
        private static ResourceBundle createUtf8PropertyResourceBundle(  
                final ResourceBundle bundle) {  
            if (!(bundle instanceof PropertyResourceBundle)) {  
                return bundle;  
            }  
            return new Utf8PropertyResourceBundle((PropertyResourceBundle) bundle);  
        }  
    
        /** 
         * Resource Bundle that does the hard work 
         */  
        private static class Utf8PropertyResourceBundle extends ResourceBundle {  
    
            /** 
             * Bundle with unicode data 
             */  
            private final PropertyResourceBundle bundle;  
    
            /** 
             * Initializing constructor 
             *  
             * @param bundle 
             */  
            private Utf8PropertyResourceBundle(final PropertyResourceBundle bundle) {  
                this.bundle = bundle;  
            }  
    
            @Override  
            @SuppressWarnings("unchecked")  
            public Enumeration getKeys() {  
                return bundle.getKeys();  
            }  
    
            @Override  
            protected Object handleGetObject(final String key) {  
                final String value = bundle.getString(key);  
                if (value == null)  
                    return null;  
                try {  
                    return new String(value.getBytes("ISO-8859-1"), "UTF-8");  
                } catch (final UnsupportedEncodingException e) {  
                    throw new RuntimeException("Encoding not supported", e);  
                }  
            }  
        }  
    }  
    

提交回复
热议问题