Java Code Examples for org.springframework.context.MessageSourceResolvable#getDefaultMessage()

The following examples show how to use org.springframework.context.MessageSourceResolvable#getDefaultMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractMessageSource.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Get a default message for the given {@code MessageSourceResolvable}.
 * <p>This implementation fully renders the default message if available,
 * or just returns the plain default message {@code String} if the primary
 * message code is being used as a default message.
 * @param resolvable the value object to resolve a default message for
 * @param locale the current locale
 * @return the default message, or {@code null} if none
 * @since 4.3.6
 * @see #renderDefaultMessage(String, Object[], Locale)
 * @see #getDefaultMessage(String)
 */
@Nullable
protected String getDefaultMessage(MessageSourceResolvable resolvable, Locale locale) {
	String defaultMessage = resolvable.getDefaultMessage();
	String[] codes = resolvable.getCodes();
	if (defaultMessage != null) {
		if (resolvable instanceof DefaultMessageSourceResolvable &&
				!((DefaultMessageSourceResolvable) resolvable).shouldRenderDefaultMessage()) {
			// Given default message does not contain any argument placeholders
			// (and isn't escaped for alwaysUseMessageFormat either) -> return as-is.
			return defaultMessage;
		}
		if (!ObjectUtils.isEmpty(codes) && defaultMessage.equals(codes[0])) {
			// Never format a code-as-default-message, even with alwaysUseMessageFormat=true
			return defaultMessage;
		}
		return renderDefaultMessage(defaultMessage, resolvable.getArguments(), locale);
	}
	return (!ObjectUtils.isEmpty(codes) ? getDefaultMessage(codes[0]) : null);
}
 
Example 2
Source File: DBMessageSource.java    From openemm with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
    String[] codes = resolvable.getCodes();

    if (codes != null) {
        for (String code : codes) {
            if (I18nString.hasMessageForKey(code)) {
                return I18nString.getLocaleString(code, locale, resolvable.getArguments());
            }
        }
    }

    String defaultMessage = resolvable.getDefaultMessage();

    if (defaultMessage == null) {
        throw new NoSuchMessageException(codes == null || ArrayUtils.isEmpty(codes) ? null : codes[codes.length - 1], locale);
    }

    return defaultMessage;
}
 
Example 3
Source File: AbstractMessageSource.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public final String getMessage(MessageSourceResolvable resolvable, Locale locale)
		throws NoSuchMessageException {

	String[] codes = resolvable.getCodes();
	if (codes == null) {
		codes = new String[0];
	}
	for (String code : codes) {
		String msg = getMessageInternal(code, resolvable.getArguments(), locale);
		if (msg != null) {
			return msg;
		}
	}
	String defaultMessage = resolvable.getDefaultMessage();
	if (defaultMessage != null) {
		return renderDefaultMessage(defaultMessage, resolvable.getArguments(), locale);
	}
	if (codes.length > 0) {
		String fallback = getDefaultMessage(codes[0]);
		if (fallback != null) {
			return fallback;
		}
	}
	throw new NoSuchMessageException(codes.length > 0 ? codes[codes.length - 1] : null, locale);
}
 
Example 4
Source File: DelegatingMessageSource.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
	if (this.parentMessageSource != null) {
		return this.parentMessageSource.getMessage(resolvable, locale);
	}
	else {
		if (resolvable.getDefaultMessage() != null) {
			return renderDefaultMessage(resolvable.getDefaultMessage(), resolvable.getArguments(), locale);
		}
		String[] codes = resolvable.getCodes();
		String code = (codes != null && codes.length > 0 ? codes[0] : "");
		throw new NoSuchMessageException(code, locale);
	}
}
 
Example 5
Source File: Rs.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
/**
 * 指定されたロケールから、複数のメッセージキーより該当する値を検索します。
 * もし対応するキー値が存在しない場合は、デフォルトメッセージが返却されます。
 * @param context {@link MessageSourceResolvable}
 * @param locale ロケール
 * @return キーに対応する値
 */
public static String find(MessageSourceResolvable context, Locale locale) {
    try {
        MessageSource source = MessageSourceLocator.get();
        Locale currentLocale = locale;
        if (currentLocale == null) {
            Locale userLocale = LocaleContextHolder.getLocale();
            currentLocale = userLocale != null ? userLocale : Locale.getDefault();
        }
        return source.getMessage(context, currentLocale);
    } catch (NoSuchMessageException e) {
        return context.getDefaultMessage();
    }
}
 
Example 6
Source File: DelegatingMessageSource.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
	if (this.parentMessageSource != null) {
		return this.parentMessageSource.getMessage(resolvable, locale);
	}
	else {
		if (resolvable.getDefaultMessage() != null) {
			return renderDefaultMessage(resolvable.getDefaultMessage(), resolvable.getArguments(), locale);
		}
		String[] codes = resolvable.getCodes();
		String code = (codes != null && codes.length > 0 ? codes[0] : null);
		throw new NoSuchMessageException(code, locale);
	}
}
 
Example 7
Source File: AbstractMessageSource.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get a default message for the given {@code MessageSourceResolvable}.
 * <p>This implementation fully renders the default message if available,
 * or just returns the plain default message {@code String} if the primary
 * message code is being used as a default message.
 * @param resolvable the value object to resolve a default message for
 * @param locale the current locale
 * @return the default message, or {@code null} if none
 * @since 4.3.6
 * @see #renderDefaultMessage(String, Object[], Locale)
 * @see #getDefaultMessage(String)
 */
protected String getDefaultMessage(MessageSourceResolvable resolvable, Locale locale) {
	String defaultMessage = resolvable.getDefaultMessage();
	String[] codes = resolvable.getCodes();
	if (defaultMessage != null) {
		if (!ObjectUtils.isEmpty(codes) && defaultMessage.equals(codes[0])) {
			// Never format a code-as-default-message, even with alwaysUseMessageFormat=true
			return defaultMessage;
		}
		return renderDefaultMessage(defaultMessage, resolvable.getArguments(), locale);
	}
	return (!ObjectUtils.isEmpty(codes) ? getDefaultMessage(codes[0]) : null);
}
 
Example 8
Source File: DelegatingMessageSource.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
	if (this.parentMessageSource != null) {
		return this.parentMessageSource.getMessage(resolvable, locale);
	}
	else {
		if (resolvable.getDefaultMessage() != null) {
			return renderDefaultMessage(resolvable.getDefaultMessage(), resolvable.getArguments(), locale);
		}
		String[] codes = resolvable.getCodes();
		String code = (codes != null && codes.length > 0 ? codes[0] : null);
		throw new NoSuchMessageException(code, locale);
	}
}
 
Example 9
Source File: AbstractMessageSource.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Get a default message for the given {@code MessageSourceResolvable}.
 * <p>This implementation fully renders the default message if available,
 * or just returns the plain default message {@code String} if the primary
 * message code is being used as a default message.
 * @param resolvable the value object to resolve a default message for
 * @param locale the current locale
 * @return the default message, or {@code null} if none
 * @since 4.3.6
 * @see #renderDefaultMessage(String, Object[], Locale)
 * @see #getDefaultMessage(String)
 */
@Nullable
protected String getDefaultMessage(MessageSourceResolvable resolvable, Locale locale) {
	String defaultMessage = resolvable.getDefaultMessage();
	String[] codes = resolvable.getCodes();
	if (defaultMessage != null) {
		if (!ObjectUtils.isEmpty(codes) && defaultMessage.equals(codes[0])) {
			// Never format a code-as-default-message, even with alwaysUseMessageFormat=true
			return defaultMessage;
		}
		return renderDefaultMessage(defaultMessage, resolvable.getArguments(), locale);
	}
	return (!ObjectUtils.isEmpty(codes) ? getDefaultMessage(codes[0]) : null);
}
 
Example 10
Source File: DelegatingMessageSource.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
	if (this.parentMessageSource != null) {
		return this.parentMessageSource.getMessage(resolvable, locale);
	}
	else {
		if (resolvable.getDefaultMessage() != null) {
			return renderDefaultMessage(resolvable.getDefaultMessage(), resolvable.getArguments(), locale);
		}
		String[] codes = resolvable.getCodes();
		String code = (codes != null && codes.length > 0 ? codes[0] : "");
		throw new NoSuchMessageException(code, locale);
	}
}
 
Example 11
Source File: DefaultMessageSourceResolvable.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Copy constructor: Create a new instance from another resolvable.
 * @param resolvable the resolvable to copy from
 */
public DefaultMessageSourceResolvable(MessageSourceResolvable resolvable) {
	this(resolvable.getCodes(), resolvable.getArguments(), resolvable.getDefaultMessage());
}
 
Example 12
Source File: DefaultMessageSourceResolvable.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Copy constructor: Create a new instance from another resolvable.
 * @param resolvable the resolvable to copy from
 */
public DefaultMessageSourceResolvable(MessageSourceResolvable resolvable) {
	this(resolvable.getCodes(), resolvable.getArguments(), resolvable.getDefaultMessage());
}
 
Example 13
Source File: DefaultMessageSourceResolvable.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Copy constructor: Create a new instance from another resolvable.
 * @param resolvable the resolvable to copy from
 */
public DefaultMessageSourceResolvable(MessageSourceResolvable resolvable) {
	this(resolvable.getCodes(), resolvable.getArguments(), resolvable.getDefaultMessage());
}
 
Example 14
Source File: DefaultMessageSourceResolvable.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Copy constructor: Create a new instance from another resolvable.
 * @param resolvable the resolvable to copy from
 */
public DefaultMessageSourceResolvable(MessageSourceResolvable resolvable) {
	this(resolvable.getCodes(), resolvable.getArguments(), resolvable.getDefaultMessage());
}
 
Example 15
Source File: MessageSourceWrapper.java    From jdal with Apache License 2.0 2 votes vote down vote up
/** 
 * Get message from resolvable using default locale
 * @param msr message source resolvable
 * @return message or code if none defined
 */
public String getMessage(MessageSourceResolvable msr) {
	return messageSource == null ?
			msr.getDefaultMessage() : messageSource.getMessage(msr, LocaleContextHolder.getLocale());
}
 
Example 16
Source File: MessageSourceWrapper.java    From jdal with Apache License 2.0 2 votes vote down vote up
/**
 * Resolve a {@link MessageSourceResolvable}
 * @param resolvable
 * @param locale
 * @return message
 * @throws NoSuchMessageException
 * @see org.springframework.context.MessageSource#getMessage(org.springframework.context.MessageSourceResolvable, java.util.Locale)
 */
public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
	return messageSource == null ? resolvable.getDefaultMessage() : messageSource.getMessage(resolvable, locale);
}
 
Example 17
Source File: StaticMessageSource.java    From jdal with Apache License 2.0 2 votes vote down vote up
/** 
 * I18n Support
 * @param msr message source resolvable
 * @return message or code if none defined
 */
 public static String getMessage(MessageSourceResolvable msr) {
	return messageSource == null ?
			msr.getDefaultMessage() : messageSource.getMessage(msr, LocaleContextHolder.getLocale());
}
 
Example 18
Source File: ViewSupport.java    From jdal with Apache License 2.0 2 votes vote down vote up
/** 
 * I18n Support
 * @param msr message source resolvable
 * @return message or code if none defined
 */
protected String getMessage(MessageSourceResolvable msr) {
	return messageSource == null ?
			msr.getDefaultMessage() : messageSource.getMessage(msr, LocaleContextHolder.getLocale());
}