Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.     public class MyResourceBundleMessageSource extends ResourceBundleMessageSource
  2.     {
  3.         public static final String BUNDLE_KEY_DELIMITER = "$";
  4.         public static final String MESSAGE_DELIMITER = "#~#";
  5.         private boolean isInitialized = false;
  6.      
  7.         @Override
  8.         protected ResourceBundle doGetBundle(final String bundlename, final Locale targetLocale)
  9.         {
  10.             return ResourceBundle.getBundle(bundlename, targetLocale, new ResourceBundle.Control()
  11.             {
  12.                 @Override
  13.                 public ResourceBundle newBundle(final String baseName,
  14.                                                 final Locale locale,
  15.                                                 final String format,
  16.                                                 final ClassLoader loader,
  17.                                                 final boolean reload) throws IOException
  18.                 {
  19.                     return new MyResourceBundle(baseName, locale.getLanguage());
  20.                 }
  21.             });
  22.         }
  23.      
  24.         @Override
  25.         protected String resolveCodeWithoutArguments(final String code, final Locale locale)
  26.         {
  27.             if (!isInitialized)
  28.             {
  29.                 setBasenames(TexteUtil.getAllLanguageFiles());
  30.                 isInitialized = true;
  31.             }
  32.             String result = null;
  33.             ResourceBundle bundle = null;
  34.             String basename = "";
  35.             String key = code;
  36.             if (code.contains(MESSAGE_DELIMITER))
  37.             {
  38.                 String[] codes = code.split(MESSAGE_DELIMITER);
  39.                 result = "";
  40.                 for (int i = 0; i < codes.length; i++)
  41.                 {
  42.                     result += " " + resolveCodeWithoutArguments(codes[i], locale);
  43.                 }
  44.             }
  45.             else
  46.             {
  47.                 if (code.contains(BUNDLE_KEY_DELIMITER))
  48.                 {
  49.                     basename = code.substring(0, code.indexOf(BUNDLE_KEY_DELIMITER));
  50.                     key = code.substring(code.indexOf(BUNDLE_KEY_DELIMITER) + 1);
  51.                     bundle = getResourceBundle(basename, locale);
  52.                 }
  53.                 if (bundle != null)
  54.                 {
  55.                     try
  56.                     {
  57.                         result = bundle.getString(key);
  58.                     }
  59.                     catch (MissingResourceException ex)
  60.                     {
  61.                         // Assume key not found
  62.                         // -> do NOT throw the exception to allow for checking parent message source.
  63.                         result = null;
  64.                     }
  65.                 }
  66.                 if ((bundle == null) || (result == null))
  67.                 {
  68.                     result = super.resolveCodeWithoutArguments(key, locale);
  69.                 }
  70.             }
  71.             return result;
  72.         }
  73.      
  74.         @Override
  75.         protected MessageFormat resolveCode(final String code, final Locale locale)
  76.         {
  77.             if (!isInitialized)
  78.             {
  79.                 setBasenames(TexteUtil.getAllLanguageFiles());
  80.                 isInitialized = true;
  81.             }
  82.             MessageFormat result = null;
  83.             ResourceBundle bundle = null;
  84.             String basename = "";
  85.             String key = code;
  86.             if (code.contains(MESSAGE_DELIMITER))
  87.             {
  88.                 String[] codes = code.split(MESSAGE_DELIMITER);
  89.                 result = new MessageFormat("");
  90.                 for (int i = 0; i < codes.length; i++)
  91.                 {
  92.                     result = new MessageFormat(result.toPattern() + " " + resolveCode(codes[i], locale).toPattern());
  93.                 }
  94.             }
  95.             else
  96.             {
  97.                 if (code.contains(BUNDLE_KEY_DELIMITER))
  98.                 {
  99.                     basename = code.substring(0, code.indexOf(BUNDLE_KEY_DELIMITER));
  100.                     key = code.substring(code.indexOf(BUNDLE_KEY_DELIMITER) + 1);
  101.                     bundle = getResourceBundle(basename, locale);
  102.                 }
  103.                 if (bundle != null)
  104.                 {
  105.                     result = getMessageFormat(bundle, key, locale);
  106.                 }
  107.                 if ((bundle == null) || (result == null))
  108.                 {
  109.                     result = super.resolveCode(key, locale);
  110.                 }
  111.             }
  112.             return result;
  113.         }
  114.      
  115.         private static class MyResourceBundle extends ResourceBundle
  116.         {
  117.             private final Properties props;
  118.      
  119.             MyResourceBundle(final String baseName, final String locale) throws IOException
  120.             {
  121.                 props = TexteUtil.getLanguageSupport(locale, baseName);
  122.             }
  123.      
  124.             @Override
  125.             protected Object handleGetObject(final String key)
  126.             {
  127.                 return props.getProperty(key);
  128.             }
  129.      
  130.             @Override
  131.             public Enumeration<String> getKeys()
  132.             {
  133.                 Vector<String> keys = new Vector<String>();
  134.                 Enumeration<Object> keysEnum = props.keys();
  135.                 while (keysEnum.hasMoreElements())
  136.                 {
  137.                     keys.add(keysEnum.nextElement().toString());
  138.                 }
  139.                 return keys.elements();
  140.             }
  141.         }
  142.     }