Example usage for org.springframework.context MessageSource getMessage

List of usage examples for org.springframework.context MessageSource getMessage

Introduction

In this page you can find the example usage for org.springframework.context MessageSource getMessage.

Prototype

String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException;

Source Link

Document

Try to resolve the message.

Usage

From source file:org.gbif.portal.web.download.Field.java

/**
 * Gets the field value applying i18n if necessary.
 * /*from www.  ja v  a 2s . co  m*/
 * @param messageSource
 * @param locale
 * @param propertyValue
 * @return property value
 */
public String getFieldValue(MessageSource messageSource, Locale locale, String propertyValue) {

    String formattedValue = propertyValue;

    if (useI18nOnValue && propertyValue != null) {
        formattedValue = messageSource.getMessage(i18nPrefix + "." + propertyValue.replace(" ", ""), null,
                locale);
    } else {
        if (removeNewLines)
            formattedValue = propertyValue.replaceAll("\n", "");
    }
    return formattedValue;
}

From source file:org.mifos.framework.util.helpers.ConversionError.java

public String toLocalizedMessage(MifosCurrency currency) {
    // nothing found so return the key
    String message = this.name();
    PersonnelServiceFacade personnelServiceFacade = ApplicationContextProvider
            .getBean(PersonnelServiceFacade.class);
    MessageSource messageSource = ApplicationContextProvider.getBean(MessageSource.class);
    if (personnelServiceFacade != null) {
        String value = messageSource.getMessage(this.name(), null,
                personnelServiceFacade.getUserPreferredLocale());
        if (StringUtils.isNotEmpty(message)) {
            message = value;/*from   w w w  .j ava  2  s  .  c o m*/
        }
    }
    return message;

}

From source file:org.openlmis.referencedata.util.Message.java

/**
 * Gets the localized version of this message as it's intended for a human.
 * @param messageSource the source of localized text.
 * @param locale the locale to determine which localized text to use.
 * @return this message localized in a format suitable for serialization.
 * @throws org.springframework.context.NoSuchMessageException if the message doesn't exist in
 *     the messageSource./*from  ww  w .ja  va2s .com*/
 */
public LocalizedMessage localMessage(MessageSource messageSource, Locale locale) {
    return new LocalizedMessage(key, messageSource.getMessage(key, params, locale));
}

From source file:org.sparkcommerce.openadmin.server.service.persistence.validation.FieldLengthValidator.java

@Override
public PropertyValidationResult validate(Entity entity, Serializable instance,
        Map<String, FieldMetadata> entityFieldMetadata, BasicFieldMetadata propertyMetadata,
        String propertyName, String value) {
    boolean valid = true;
    String errorMessage = "";
    if (propertyMetadata.getLength() != null) {
        valid = StringUtils.length(value) <= propertyMetadata.getLength();
    }/*from  w w w  .j av  a  2  s. c om*/

    if (!valid) {
        SparkRequestContext context = SparkRequestContext.getSparkRequestContext();
        MessageSource messages = context.getMessageSource();
        errorMessage = messages.getMessage("fieldLengthValidationFailure",
                new Object[] { propertyMetadata.getLength(), StringUtils.length(value) },
                context.getJavaLocale());
    }

    return new PropertyValidationResult(valid, errorMessage);
}

From source file:org.sparkcommerce.openadmin.server.service.persistence.validation.RequiredIfPropertyValidator.java

@Override
public PropertyValidationResult validate(Entity entity, Serializable instance,
        Map<String, FieldMetadata> entityFieldMetadata, Map<String, String> validationConfiguration,
        BasicFieldMetadata propertyMetadata, String propertyName, String value) {
    String errorMessage = "";

    String compareFieldName = lookupCompareFieldName(propertyName, validationConfiguration);
    String compareFieldValue = validationConfiguration.get("compareFieldValue");
    String compareFieldRegEx = validationConfiguration.get("compareFieldRegEx");
    Property compareFieldProperty = null;

    boolean valid = true;
    if (StringUtils.isEmpty(value)) {
        compareFieldProperty = entity.getPMap().get(compareFieldName);

        if (compareFieldProperty != null) {
            if (compareFieldValue != null) {
                valid = !compareFieldValue.equals(compareFieldProperty.getValue());
            } else if (compareFieldRegEx != null) {
                String expression = validationConfiguration.get("compareFieldRegEx");
                valid = !compareFieldProperty.getValue().matches(expression);
            }/*w w  w. j av a 2s. co  m*/

        }
    }

    if (!valid) {
        SparkRequestContext context = SparkRequestContext.getSparkRequestContext();
        MessageSource messages = context.getMessageSource();

        FieldMetadata fmd = entityFieldMetadata.get(compareFieldName);
        String fieldName = messages.getMessage(fmd.getFriendlyName(), null, context.getJavaLocale());
        errorMessage = messages.getMessage("requiredIfValidationFailure",
                new Object[] { fieldName, compareFieldProperty.getValue() }, context.getJavaLocale());
    }

    return new PropertyValidationResult(valid, errorMessage);
}

From source file:org.springframework.richclient.application.docking.jide.editor.AbstractEditor.java

/**
 * Method to obtain a message from the message source
 * defined via the services locator, at the default locale.
 *///from  w w w . jav a 2s.  c o m
protected String getMessage(String key, Object[] params) {
    MessageSource messageSource = (MessageSource) ApplicationServicesLocator.services()
            .getService(MessageSource.class);
    return messageSource.getMessage(key, params, Locale.getDefault());
}

From source file:ro.cs.om.web.controller.root.ControllerUtils.java

public List<List> getMonthsAndDaysAndYears(HttpServletRequest request, MessageSource messageSource) {
    List<List> result = new ArrayList<List>();
    Locale locale = LocaleContextHolder.getLocale();
    List<Month> months = new ArrayList<Month>();
    List<String> days = new ArrayList<String>();
    List<String> years = new ArrayList<String>();
    Date date = new Date();
    SimpleDateFormat simpleDateformat = new SimpleDateFormat("yyyy");
    String year = simpleDateformat.format(date);
    logger.debug("year = " + year);

    for (int i = Integer.parseInt(year); i >= 1900; i--) {
        years.add(Integer.toString(i));
    }/*from  www  .j  a  va 2  s  .  c o  m*/

    for (int i = 0; i < 12; i++) {
        Month month = new Month();
        month.setMonthId(i);
        month.setMonthName(messageSource.getMessage("month.".concat(Integer.toString(i)), null, locale));
        months.add(month);
        days.add(Integer.toString(i + 1));
    }
    //the remaining days
    for (int i = 13; i < 32; i++) {
        days.add(Integer.toString(i));
    }
    result.add(days);
    result.add(months);
    result.add(years);
    return result;
}

From source file:ro.cs.om.web.controller.root.ControllerUtils.java

/**
 * Returns a string (JSON pattern) of all persons from an organization
 *
 * @author alu/*from  ww  w.  ja  v  a  2  s  .  c o  m*/
 * 
 * @param orgId
 * @param locale
 * @param errorMessages
 * @return
 */
public String getPersonsFromOrgAsJSON(int orgId, Locale locale, ArrayList<String> errorMessages,
        MessageSource messageSource) {
    logger.debug("getPersonsFromOrgAsJSON - START - orgID:".concat(String.valueOf(orgId)));
    JSONArray jsonArray = new JSONArray();
    try {
        // get all persons from user's organization
        // Tools.getInstance().printList(logger, BLPerson.getInstance().getPersonsByOrganizationId(orgId));
        Iterator<Person> it = BLPerson.getInstance().getPersonsByOrganizationId(orgId).iterator();
        JSONObject jsonObj = new JSONObject();
        while (it.hasNext()) {
            Person pers = (Person) it.next();

            // add the name and the id
            jsonObj.accumulate("id", pers.getPersonId());
            jsonObj.accumulate("name", pers.getFirstName().concat(" ").concat(pers.getLastName()));
            jsonArray.add(jsonObj);
            jsonObj.clear();
        }
    } catch (BusinessException be) {
        logger.error("", be);
        errorMessages.add(messageSource.getMessage(GET_PERSONS_FROM_ORG_AS_JSON_ERROR,
                new Object[] { be.getCode(), ControllerUtils.getInstance().getFormattedCurrentTime() },
                locale));
    } catch (Exception e) {
        logger.error("", e);
        errorMessages.add(messageSource.getMessage(GET_PERSONS_FROM_ORG_AS_JSON_ERROR,
                new Object[] { null, ControllerUtils.getInstance().getFormattedCurrentTime() }, locale));
    }

    logger.debug("getPersonsFromOrgAsJSON - END - json:".concat(jsonArray.toString()));
    return jsonArray.toString();
}

From source file:ro.cs.om.web.controller.root.ControllerUtils.java

/**
 * Returns a string (JSON pattern) of all persons from an organization containing the name (firstName + lastName) and
 * the firstName and the lastName separately
 *
 * @author coni/*w w  w.  ja v a 2  s .c o  m*/
 * 
 * @param orgId
 * @param locale
 * @param errorMessages
 * @return
 */
public String getPersonsFirstNameLastNameFromOrgAsJSON(int orgId, Locale locale,
        ArrayList<String> errorMessages, MessageSource messageSource) {
    logger.debug("getPersonsFirstNameLastNameFromOrgAsJSON - START - orgID:".concat(String.valueOf(orgId)));
    JSONArray jsonArray = new JSONArray();
    try {
        Iterator<Person> it = BLPerson.getInstance().getPersonsByOrganizationId(orgId).iterator();
        JSONObject jsonObj = new JSONObject();
        while (it.hasNext()) {
            Person pers = (Person) it.next();

            // add the name and the id
            jsonObj.accumulate("name", pers.getFirstName().concat(" ").concat(pers.getLastName()));
            jsonObj.accumulate("firstName", pers.getFirstName());
            jsonObj.accumulate("lastName", pers.getLastName());
            jsonArray.add(jsonObj);
            jsonObj.clear();
        }
    } catch (BusinessException be) {
        logger.error("", be);
        errorMessages.add(messageSource.getMessage(GET_PERSONS_FROM_ORG_AS_JSON_ERROR,
                new Object[] { be.getCode(), ControllerUtils.getInstance().getFormattedCurrentTime() },
                locale));
    } catch (Exception e) {
        logger.error("", e);
        errorMessages.add(messageSource.getMessage(GET_PERSONS_FROM_ORG_AS_JSON_ERROR,
                new Object[] { null, ControllerUtils.getInstance().getFormattedCurrentTime() }, locale));
    }

    logger.debug("getPersonsFromOrgAsJSON - END - json:".concat(jsonArray.toString()));
    return jsonArray.toString();
}

From source file:ro.cs.om.web.controller.root.ControllerUtils.java

/**
 * Returns a string (JSON pattern) of departments from an organization 
 * containing the name's of the departments
 *  /*  ww  w.j a  va2  s  .c  om*/
 * @author Adelina
 * 
 * @param organisationId
 * @param locale
 * @param errorMessages
 * @param messageSource
 * @return
 */
public String getDepartmentFromOrgAsJSON(Integer organisationId, Locale locale, ArrayList<String> errorMessages,
        MessageSource messageSource) {
    logger.debug("getDepartmentFromOrgAsJSON - START - ORGANISATION ".concat(String.valueOf(organisationId)));

    JSONArray jsonArray = new JSONArray();

    try {
        Set<Department> departments = BLOrganisation.getInstance().getDepartments(organisationId);
        JSONObject jsonObj = new JSONObject();
        for (Department department : departments) {
            if (department.getStatus() != IConstant.NOM_DEPARTMENT_FAKE) {
                jsonObj.accumulate("name", department.getName().concat(" "));
                jsonObj.accumulate("id", department.getDepartmentId());
                jsonArray.add(jsonObj);
                jsonObj.clear();
            }
        }
    } catch (BusinessException be) {
        logger.error("", be);
        errorMessages.add(messageSource.getMessage(GET_DEPARTMENTS_FROM_ORG_AS_JSON_ERROR,
                new Object[] { be.getCode(), ControllerUtils.getInstance().getFormattedCurrentTime() },
                locale));
    } catch (Exception e) {
        logger.error("", e);
        errorMessages.add(messageSource.getMessage(GET_DEPARTMENTS_FROM_ORG_AS_JSON_ERROR,
                new Object[] { null, ControllerUtils.getInstance().getFormattedCurrentTime() }, locale));
    }

    logger.debug("getDepartmentFromOrgAsJSON - END - json:".concat(jsonArray.toString()));
    return jsonArray.toString();
}