org.springframework.context.MessageSourceResolvable Java Examples

The following examples show how to use org.springframework.context.MessageSourceResolvable. 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: StaticMessageSourceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void messageSourceResolvable() {
	// first code valid
	String[] codes1 = new String[] {"message.format.example3", "message.format.example2"};
	MessageSourceResolvable resolvable1 = new DefaultMessageSourceResolvable(codes1, null, "default");
	assertTrue("correct message retrieved", MSG_TXT3_US.equals(sac.getMessage(resolvable1, Locale.US)));

	// only second code valid
	String[] codes2 = new String[] {"message.format.example99", "message.format.example2"};
	MessageSourceResolvable resolvable2 = new DefaultMessageSourceResolvable(codes2, null, "default");
	assertTrue("correct message retrieved", MSG_TXT2_US.equals(sac.getMessage(resolvable2, Locale.US)));

	// no code valid, but default given
	String[] codes3 = new String[] {"message.format.example99", "message.format.example98"};
	MessageSourceResolvable resolvable3 = new DefaultMessageSourceResolvable(codes3, null, "default");
	assertTrue("correct message retrieved", "default".equals(sac.getMessage(resolvable3, Locale.US)));

	// no code valid, no default
	String[] codes4 = new String[] {"message.format.example99", "message.format.example98"};
	MessageSourceResolvable resolvable4 = new DefaultMessageSourceResolvable(codes4);

	assertThatExceptionOfType(NoSuchMessageException.class).isThrownBy(() ->
			sac.getMessage(resolvable4, Locale.US));
}
 
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: JsonRenderer.java    From gocd with Apache License 2.0 6 votes vote down vote up
private static Gson gsonBuilder(final GoRequestContext requestContext) {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(JsonUrl.class, (JsonSerializer<JsonUrl>) (src, typeOfSrc, context) -> {
        if (requestContext == null) {
            return new JsonPrimitive(src.getUrl());
        } else {
            return new JsonPrimitive(requestContext.getFullRequestPath() + src.getUrl());
        }
    });

    builder.registerTypeHierarchyAdapter(MessageSourceResolvable.class, (JsonSerializer<MessageSourceResolvable>) (src, typeOfSrc, context) -> {
        if (requestContext == null) {
            return new JsonPrimitive(src.getDefaultMessage());
        } else {
            return new JsonPrimitive(requestContext.getMessage(src));
        }
    });

    builder.serializeNulls();
    return builder.create();
}
 
Example #5
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 #6
Source File: StaticMessageSourceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void messageSourceResolvable() {
	// first code valid
	String[] codes1 = new String[] {"message.format.example3", "message.format.example2"};
	MessageSourceResolvable resolvable1 = new DefaultMessageSourceResolvable(codes1, null, "default");
	assertTrue("correct message retrieved", MSG_TXT3_US.equals(sac.getMessage(resolvable1, Locale.US)));

	// only second code valid
	String[] codes2 = new String[] {"message.format.example99", "message.format.example2"};
	MessageSourceResolvable resolvable2 = new DefaultMessageSourceResolvable(codes2, null, "default");
	assertTrue("correct message retrieved", MSG_TXT2_US.equals(sac.getMessage(resolvable2, Locale.US)));

	// no code valid, but default given
	String[] codes3 = new String[] {"message.format.example99", "message.format.example98"};
	MessageSourceResolvable resolvable3 = new DefaultMessageSourceResolvable(codes3, null, "default");
	assertTrue("correct message retrieved", "default".equals(sac.getMessage(resolvable3, Locale.US)));

	// no code valid, no default
	String[] codes4 = new String[] {"message.format.example99", "message.format.example98"};
	MessageSourceResolvable resolvable4 = new DefaultMessageSourceResolvable(codes4);

	exception.expect(NoSuchMessageException.class);
	sac.getMessage(resolvable4, Locale.US);
}
 
Example #7
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 #8
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 #9
Source File: MessageTagTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void requestContext() throws ServletException {
	PageContext pc = createPageContext();
	RequestContext rc = new RequestContext((HttpServletRequest) pc.getRequest(), pc.getServletContext());
	assertEquals("test message", rc.getMessage("test"));
	assertEquals("test message", rc.getMessage("test", (Object[]) null));
	assertEquals("test message", rc.getMessage("test", "default"));
	assertEquals("test message", rc.getMessage("test", (Object[]) null, "default"));
	assertEquals("test arg1 message arg2",
			rc.getMessage("testArgs", new String[] {"arg1", "arg2"}, "default"));
	assertEquals("test arg1 message arg2",
			rc.getMessage("testArgs", Arrays.asList(new String[] {"arg1", "arg2"}), "default"));
	assertEquals("default", rc.getMessage("testa", "default"));
	assertEquals("default", rc.getMessage("testa", (List) null, "default"));
	MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable(new String[] {"test"});
	assertEquals("test message", rc.getMessage(resolvable));
}
 
Example #10
Source File: AbstractMessageSource.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Searches through the given array of objects, finds any MessageSourceResolvable
 * objects and resolves them.
 * <p>Allows for messages to have MessageSourceResolvables as arguments.
 * @param args array of arguments for a message
 * @param locale the locale to resolve through
 * @return an array of arguments with any MessageSourceResolvables resolved
 */
@Override
protected Object[] resolveArguments(Object[] args, Locale locale) {
	if (args == null) {
		return new Object[0];
	}
	List<Object> resolvedArgs = new ArrayList<Object>(args.length);
	for (Object arg : args) {
		if (arg instanceof MessageSourceResolvable) {
			resolvedArgs.add(getMessage((MessageSourceResolvable) arg, locale));
		}
		else {
			resolvedArgs.add(arg);
		}
	}
	return resolvedArgs.toArray(new Object[resolvedArgs.size()]);
}
 
Example #11
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 #12
Source File: DefaultMessageSourceResolvable.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public boolean equals(Object other) {
	if (this == other) {
		return true;
	}
	if (!(other instanceof MessageSourceResolvable)) {
		return false;
	}
	MessageSourceResolvable otherResolvable = (MessageSourceResolvable) other;
	return (ObjectUtils.nullSafeEquals(getCodes(), otherResolvable.getCodes()) &&
			ObjectUtils.nullSafeEquals(getArguments(), otherResolvable.getArguments()) &&
			ObjectUtils.nullSafeEquals(getDefaultMessage(), otherResolvable.getDefaultMessage()));
}
 
Example #13
Source File: DefaultMessageSourceResolvable.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean equals(Object other) {
	if (this == other) {
		return true;
	}
	if (!(other instanceof MessageSourceResolvable)) {
		return false;
	}
	MessageSourceResolvable otherResolvable = (MessageSourceResolvable) other;
	return (ObjectUtils.nullSafeEquals(getCodes(), otherResolvable.getCodes()) &&
			ObjectUtils.nullSafeEquals(getArguments(), otherResolvable.getArguments()) &&
			ObjectUtils.nullSafeEquals(getDefaultMessage(), otherResolvable.getDefaultMessage()));
}
 
Example #14
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 #15
Source File: ThemeTagTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void requestContext() throws ServletException {
	PageContext pc = createPageContext();
	RequestContext rc = new RequestContext((HttpServletRequest) pc.getRequest());
	assertEquals("theme test message", rc.getThemeMessage("themetest"));
	assertEquals("theme test message", rc.getThemeMessage("themetest", (String[]) null));
	assertEquals("theme test message", rc.getThemeMessage("themetest", "default"));
	assertEquals("theme test message", rc.getThemeMessage("themetest", (Object[]) null, "default"));
	assertEquals("theme test message arg1",
			rc.getThemeMessage("themetestArgs", new String[] {"arg1"}));
	assertEquals("theme test message arg1",
			rc.getThemeMessage("themetestArgs", Arrays.asList(new String[] {"arg1"})));
	assertEquals("default", rc.getThemeMessage("themetesta", "default"));
	assertEquals("default", rc.getThemeMessage("themetesta", (List) null, "default"));
	MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable(new String[] {"themetest"});
	assertEquals("theme test message", rc.getThemeMessage(resolvable));
}
 
Example #16
Source File: StaticMessageSourceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void nestedMessageSourceWithParamInParent() {
	StaticMessageSource source = new StaticMessageSource();
	StaticMessageSource parent = new StaticMessageSource();
	source.setParentMessageSource(parent);

	parent.addMessage("param", Locale.ENGLISH, "value");
	source.addMessage("with.param", Locale.ENGLISH, "put {0} here");

	MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable(
			new String[] {"with.param"}, new Object[] {new DefaultMessageSourceResolvable("param")});

	assertEquals("put value here", source.getMessage(resolvable, Locale.ENGLISH));
}
 
Example #17
Source File: StaticMessageSourceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void nestedMessageSourceWithParamInChild() {
	StaticMessageSource source = new StaticMessageSource();
	StaticMessageSource parent = new StaticMessageSource();
	source.setParentMessageSource(parent);

	source.addMessage("param", Locale.ENGLISH, "value");
	parent.addMessage("with.param", Locale.ENGLISH, "put {0} here");

	MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable(
			new String[] {"with.param"}, new Object[] {new DefaultMessageSourceResolvable("param")});

	assertEquals("put value here", source.getMessage(resolvable, Locale.ENGLISH));
}
 
Example #18
Source File: ThemeTagTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void requestContext() throws ServletException {
	PageContext pc = createPageContext();
	RequestContext rc = new RequestContext((HttpServletRequest) pc.getRequest());
	assertEquals("theme test message", rc.getThemeMessage("themetest"));
	assertEquals("theme test message", rc.getThemeMessage("themetest", (String[]) null));
	assertEquals("theme test message", rc.getThemeMessage("themetest", "default"));
	assertEquals("theme test message", rc.getThemeMessage("themetest", (Object[]) null, "default"));
	assertEquals("theme test message arg1",
			rc.getThemeMessage("themetestArgs", new String[] {"arg1"}));
	assertEquals("theme test message arg1",
			rc.getThemeMessage("themetestArgs", Arrays.asList(new String[] {"arg1"})));
	assertEquals("default", rc.getThemeMessage("themetesta", "default"));
	assertEquals("default", rc.getThemeMessage("themetesta", (List) null, "default"));
	MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable(new String[] {"themetest"});
	assertEquals("theme test message", rc.getThemeMessage(resolvable));
}
 
Example #19
Source File: MessageTagTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void requestContext() throws ServletException {
	PageContext pc = createPageContext();
	RequestContext rc = new RequestContext((HttpServletRequest) pc.getRequest(), pc.getServletContext());
	assertEquals("test message", rc.getMessage("test"));
	assertEquals("test message", rc.getMessage("test", (Object[]) null));
	assertEquals("test message", rc.getMessage("test", "default"));
	assertEquals("test message", rc.getMessage("test", (Object[]) null, "default"));
	assertEquals("test arg1 message arg2",
			rc.getMessage("testArgs", new String[] {"arg1", "arg2"}, "default"));
	assertEquals("test arg1 message arg2",
			rc.getMessage("testArgs", Arrays.asList(new String[] {"arg1", "arg2"}), "default"));
	assertEquals("default", rc.getMessage("testa", "default"));
	assertEquals("default", rc.getMessage("testa", (List) null, "default"));
	MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable(new String[] {"test"});
	assertEquals("test message", rc.getMessage(resolvable));
}
 
Example #20
Source File: StaticMessageSourceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void nestedMessageSourceWithParamInChild() {
	StaticMessageSource source = new StaticMessageSource();
	StaticMessageSource parent = new StaticMessageSource();
	source.setParentMessageSource(parent);

	source.addMessage("param", Locale.ENGLISH, "value");
	parent.addMessage("with.param", Locale.ENGLISH, "put {0} here");

	MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable(
			new String[] {"with.param"}, new Object[] {new DefaultMessageSourceResolvable("param")});

	assertEquals("put value here", source.getMessage(resolvable, Locale.ENGLISH));
}
 
Example #21
Source File: DefaultMessageSourceResolvable.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public boolean equals(Object other) {
	if (this == other) {
		return true;
	}
	if (!(other instanceof MessageSourceResolvable)) {
		return false;
	}
	MessageSourceResolvable otherResolvable = (MessageSourceResolvable) other;
	return (ObjectUtils.nullSafeEquals(getCodes(), otherResolvable.getCodes()) &&
			ObjectUtils.nullSafeEquals(getArguments(), otherResolvable.getArguments()) &&
			ObjectUtils.nullSafeEquals(getDefaultMessage(), otherResolvable.getDefaultMessage()));
}
 
Example #22
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 #23
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 #24
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 #25
Source File: StaticMessageSourceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void nestedMessageSourceWithParamInParent() {
	StaticMessageSource source = new StaticMessageSource();
	StaticMessageSource parent = new StaticMessageSource();
	source.setParentMessageSource(parent);

	parent.addMessage("param", Locale.ENGLISH, "value");
	source.addMessage("with.param", Locale.ENGLISH, "put {0} here");

	MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable(
			new String[] {"with.param"}, new Object[] {new DefaultMessageSourceResolvable("param")});

	assertEquals("put value here", source.getMessage(resolvable, Locale.ENGLISH));
}
 
Example #26
Source File: ThreadSafeClassPathXmlApplicationContext.java    From datawave with Apache License 2.0 5 votes vote down vote up
@Override
public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
    lock.readLock().lock();
    try {
        return configurableApplicationContext.getMessage(resolvable, locale);
    } finally {
        lock.readLock().unlock();
    }
}
 
Example #27
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 #28
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 #29
Source File: MessageSourceBundler.java    From j360-dubbo-app-all with Apache License 2.0 4 votes vote down vote up
public static String getMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException {
    return staticMessageSource.getMessage(resolvable, getLocale());
}
 
Example #30
Source File: StubWebApplicationContext.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
	return this.messageSource.getMessage(resolvable, locale);
}