Ejemplo n.º 1
0
  /*
   *   Processes resource bundle for specified locale
   * */
  private static void saveProperties(
      Locale locale, Hashtable<String, String> bundle, String bundleName) {

    for (Map.Entry property : bundle.entrySet()) {

      String key = (String) property.getKey();
      String value = (String) property.getValue();

      try {

        MessageSourcePK messageSourcePK = new MessageSourcePK(key, locale.toString());
        MessageSource messageSource =
            MessageSourceLocalServiceUtil.fetchMessageSource(messageSourcePK);

        if (messageSource == null) {

          messageSource = MessageSourceUtil.create(messageSourcePK);
          messageSource.setValue(value);
          messageSource.setBundle(bundleName);
          MessageSourceLocalServiceUtil.updateMessageSource(messageSource);
        }

      } catch (SystemException ex) {
        ex.printStackTrace();
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
  }
 public static String getMessage(Locale locale, String key) {
   try {
     // It retrieves the value based on the key and locale, it does not take
     // into account the bundle name
     MessageSourcePK messageSourcePK = new MessageSourcePK(key, locale.toString());
     String value = null;
     try {
       MessageSource ms = MessageSourceLocalServiceUtil.getMessageSource(messageSourcePK);
       value = ms.getValue();
     } catch (NoSuchMessageSourceException ignored) {
     }
     return (String) value;
   } catch (Exception e) {
     e.printStackTrace();
   }
   return null;
 }