2009-01-06

Using Unicode in Java .property files

Using multi-byte characters in Java .property files? Forget the Sun's native2ascii application. Rather than doing the irritating conversions, create UTF-8 encoded .property files, edit them directly and use Utf8ResourceBundle to access them. The code:

  1. package com.varaneckas.utils;  
  2.   
  3. import java.io.UnsupportedEncodingException;  
  4. import java.util.Enumeration;  
  5. import java.util.PropertyResourceBundle;  
  6. import java.util.ResourceBundle;  
  7.   
  8. /** 
  9.  * UTF-8 friendly ResourceBundle support 
  10.  *  
  11.  * Utility that allows having multi-byte characters inside java .property files. 
  12.  * It removes the need for Sun's native2ascii application, you can simply have 
  13.  * UTF-8 encoded editable .property files. 
  14.  *  
  15.  * Use:  
  16.  * ResourceBundle bundle = Utf8ResourceBundle.getBundle("bundle_name"); 
  17.  *  
  18.  * @author Tomas Varaneckas <tomas.varaneckas@gmail.com> 
  19.  */  
  20. public abstract class Utf8ResourceBundle {  
  21.       
  22.     /** 
  23.      * Gets the unicode friendly resource bundle 
  24.      *  
  25.      * @param baseName 
  26.      * @see ResourceBundle#getBundle(String) 
  27.      * @return Unicode friendly resource bundle 
  28.      */  
  29.     public static final ResourceBundle getBundle(final String baseName) {  
  30.         return createUtf8PropertyResourceBundle(  
  31.                 ResourceBundle.getBundle(baseName));  
  32.     }  
  33.   
  34.     /** 
  35.      * Creates unicode friendly {@link PropertyResourceBundle} if possible. 
  36.      *  
  37.      * @param bundle  
  38.      * @return Unicode friendly property resource bundle 
  39.      */  
  40.     private static ResourceBundle createUtf8PropertyResourceBundle(  
  41.             final ResourceBundle bundle) {  
  42.         if (!(bundle instanceof PropertyResourceBundle)) {  
  43.             return bundle;  
  44.         }  
  45.         return new Utf8PropertyResourceBundle((PropertyResourceBundle) bundle);  
  46.     }  
  47.   
  48.     /** 
  49.      * Resource Bundle that does the hard work 
  50.      */  
  51.     private static class Utf8PropertyResourceBundle extends ResourceBundle {  
  52.   
  53.         /** 
  54.          * Bundle with unicode data 
  55.          */  
  56.         private final PropertyResourceBundle bundle;  
  57.   
  58.         /** 
  59.          * Initializing constructor 
  60.          *  
  61.          * @param bundle 
  62.          */  
  63.         private Utf8PropertyResourceBundle(final PropertyResourceBundle bundle) {  
  64.             this.bundle = bundle;  
  65.         }  
  66.   
  67.         @Override  
  68.         @SuppressWarnings("unchecked")  
  69.         public Enumeration getKeys() {  
  70.             return bundle.getKeys();  
  71.         }  
  72.   
  73.         @Override  
  74.         protected Object handleGetObject(final String key) {  
  75.             final String value = bundle.getString(key);  
  76.             if (value == null)  
  77.                 return null;  
  78.             try {  
  79.                 return new String(value.getBytes("ISO-8859-1"), "UTF-8");  
  80.             } catch (final UnsupportedEncodingException e) {  
  81.                 throw new RuntimeException("Encoding not supported", e);  
  82.             }  
  83.         }  
  84.     }  
  85. }  

2 comments:

  1. Great idea, thanks!

    ReplyDelete
  2. I just wanted to let you know that this code that you wrote has helped me out immensely. Thanks.

    It allows me to have .property files containing the UTF-8 characters I expect, in the language of choice and be able to display/use them in Jasper reports.

    ReplyDelete

Spam comments (i.e. ones that contain links to web development services) will be reported along with user profiles!

Note: only a member of this blog may post a comment.