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

The following examples show how to use org.springframework.context.MessageSourceResolvable#getCodes() . 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
@Override
public final String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
	String[] codes = resolvable.getCodes();
	if (codes != null) {
		for (String code : codes) {
			String message = getMessageInternal(code, resolvable.getArguments(), locale);
			if (message != null) {
				return message;
			}
		}
	}
	String defaultMessage = getDefaultMessage(resolvable, locale);
	if (defaultMessage != null) {
		return defaultMessage;
	}
	throw new NoSuchMessageException(!ObjectUtils.isEmpty(codes) ? codes[codes.length - 1] : "", locale);
}
 
Example 2
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 3
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 4
Source File: AbstractMessageSource.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public final String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
	String[] codes = resolvable.getCodes();
	if (codes != null) {
		for (String code : codes) {
			String message = getMessageInternal(code, resolvable.getArguments(), locale);
			if (message != null) {
				return message;
			}
		}
	}
	String defaultMessage = getDefaultMessage(resolvable, locale);
	if (defaultMessage != null) {
		return defaultMessage;
	}
	throw new NoSuchMessageException(!ObjectUtils.isEmpty(codes) ? codes[codes.length - 1] : "", locale);
}
 
Example 5
Source File: AbstractMessageSource.java    From lams with GNU General Public License v2.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) {
		for (String code : codes) {
			String message = getMessageInternal(code, resolvable.getArguments(), locale);
			if (message != null) {
				return message;
			}
		}
	}
	String defaultMessage = getDefaultMessage(resolvable, locale);
	if (defaultMessage != null) {
		return defaultMessage;
	}
	throw new NoSuchMessageException(!ObjectUtils.isEmpty(codes) ? codes[codes.length - 1] : null, locale);
}
 
Example 6
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 7
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 8
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 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 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 11
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 12
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 13
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 14
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 15
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 16
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());
}