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

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

Source Link

Document

Try to resolve the message.

Usage

From source file:org.sipfoundry.sipxconfig.site.setting.SettingEditor.java

static IPropertySelectionModel localizedModelForType(Setting setting, EnumSetting enumType,
        MessageSource messageSource, Locale locale) {
    Map<String, String> enums = enumType.getEnums();
    int size = enums.size();
    String[] options = new String[size];
    String[] labels = new String[size];

    int i = 0;/* w  w  w  . j a  v  a  2s.  c  o  m*/
    for (Map.Entry<String, String> entry : enums.entrySet()) {
        String key = entry.getKey();
        options[i] = key;
        String code = enumType.getLabelKey(setting, key);
        labels[i] = messageSource.getMessage(code, null, entry.getValue(), locale);
        i++;
    }
    return new NamedValuesSelectionModel(options, labels);
}

From source file:org.sparkcommerce.openadmin.web.rulebuilder.service.AbstractRuleBuilderFieldService.java

@Override
public FieldWrapper buildFields() {
    FieldWrapper wrapper = new FieldWrapper();

    for (FieldData field : getFields()) {
        FieldDTO fieldDTO = new FieldDTO();
        fieldDTO.setLabel(field.getFieldLabel());

        //translate the label to display
        String label = field.getFieldLabel();
        SparkRequestContext context = SparkRequestContext.getSparkRequestContext();
        MessageSource messages = context.getMessageSource();
        label = messages.getMessage(label, null, label, context.getJavaLocale());
        fieldDTO.setLabel(label);//from   w  ww. ja v a  2 s  . c o  m

        fieldDTO.setName(field.getFieldName());
        fieldDTO.setOperators(field.getOperators());
        fieldDTO.setOptions(field.getOptions());
        wrapper.getFields().add(fieldDTO);
    }

    return wrapper;
}

From source file:org.springframework.context.support.AbstractMessageSource.java

/**
 * Try to retrieve the given message from the parent MessageSource, if any.
 * @param code the code to lookup up, such as 'calculator.noRateSet'
 * @param args array of arguments that will be filled in for params
 * within the message/*from w w  w .  j a  v  a2s. c  o m*/
 * @param locale the Locale in which to do the lookup
 * @return the resolved message, or <code>null</code> if not found
 * @see #getParentMessageSource()
 */
protected String getMessageFromParent(String code, Object[] args, Locale locale) {
    MessageSource parent = getParentMessageSource();
    if (parent != null) {
        if (parent instanceof AbstractMessageSource) {
            // Call internal method to avoid getting the default code back
            // in case of "useCodeAsDefaultMessage" being activated.
            return ((AbstractMessageSource) parent).getMessageInternal(code, args, locale);
        } else {
            // Check parent MessageSource, returning null if not found there.
            return parent.getMessage(code, args, null, locale);
        }
    }
    // Not found in parent either.
    return null;
}

From source file:org.springmodules.commons.validator.MessageUtils.java

/**
 * Get a message for the given validator action and field from the specified
 * message source.//  w ww.  j ava  2 s  .c  om
 * <p/>
 * <p/>
 * Note: this implementation uses the key of the Fields message for the
 * given ValidatorAction as the default message.
 * </p>
 *
 * @param messages MessageSource from which to get the message.
 * @param locale Locale for for this message.
 * @param va ValidatorAction for this message.
 * @param field Field field for this message.
 */
public static String getMessage(MessageSource messages, Locale locale, ValidatorAction va, Field field) {
    String code = getMessageKey(va, field);
    Object[] args = getArgs(va, field);
    String defaultMsg = code;
    return messages.getMessage(code, args, defaultMsg, locale);
}