Search in sources :

Example 1 with MessageSourceResolvable

use of cn.taketoday.context.MessageSourceResolvable in project today-infrastructure by TAKETODAY.

the class StaticMessageSourceTests method nestedMessageSourceWithParamInChild.

@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") });
    assertThat(source.getMessage(resolvable, Locale.ENGLISH)).isEqualTo("put value here");
}
Also used : MessageSourceResolvable(cn.taketoday.context.MessageSourceResolvable) Test(org.junit.jupiter.api.Test)

Example 2 with MessageSourceResolvable

use of cn.taketoday.context.MessageSourceResolvable in project today-infrastructure by TAKETODAY.

the class StaticMessageSourceTests method nestedMessageSourceWithParamInParent.

@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") });
    assertThat(source.getMessage(resolvable, Locale.ENGLISH)).isEqualTo("put value here");
}
Also used : MessageSourceResolvable(cn.taketoday.context.MessageSourceResolvable) Test(org.junit.jupiter.api.Test)

Example 3 with MessageSourceResolvable

use of cn.taketoday.context.MessageSourceResolvable in project today-framework by TAKETODAY.

the class StaticMessageSourceTests method nestedMessageSourceWithParamInParent.

@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") });
    assertThat(source.getMessage(resolvable, Locale.ENGLISH)).isEqualTo("put value here");
}
Also used : MessageSourceResolvable(cn.taketoday.context.MessageSourceResolvable) Test(org.junit.jupiter.api.Test)

Example 4 with MessageSourceResolvable

use of cn.taketoday.context.MessageSourceResolvable in project today-framework by TAKETODAY.

the class ResourceBundleMessageSourceTests method doTestMessageAccess.

protected void doTestMessageAccess(boolean reloadable, boolean fallbackToSystemLocale, boolean expectGermanFallback, boolean useCodeAsDefaultMessage, boolean alwaysUseMessageFormat) {
    StaticApplicationContext ac = new StaticApplicationContext();
    if (reloadable) {
        StaticApplicationContext parent = new StaticApplicationContext();
        parent.refresh();
        ac.setParent(parent);
    }
    PropertyValues pvs = new PropertyValues();
    String basepath = "cn/taketoday/context/support/";
    String[] basenames;
    if (reloadable) {
        basenames = new String[] { "classpath:" + basepath + "messages", "classpath:" + basepath + "more-messages" };
    } else {
        basenames = new String[] { basepath + "messages", basepath + "more-messages" };
    }
    pvs.add("basenames", basenames);
    if (!fallbackToSystemLocale) {
        pvs.add("fallbackToSystemLocale", Boolean.FALSE);
    }
    if (useCodeAsDefaultMessage) {
        pvs.add("useCodeAsDefaultMessage", Boolean.TRUE);
    }
    if (alwaysUseMessageFormat) {
        pvs.add("alwaysUseMessageFormat", Boolean.TRUE);
    }
    Class<?> clazz = reloadable ? ReloadableResourceBundleMessageSource.class : ResourceBundleMessageSource.class;
    ac.registerSingleton("messageSource", clazz, pvs);
    ac.refresh();
    Locale.setDefault(expectGermanFallback ? Locale.GERMAN : Locale.CANADA);
    assertThat(ac.getMessage("code1", null, Locale.ENGLISH)).isEqualTo("message1");
    Object expected = fallbackToSystemLocale && expectGermanFallback ? "nachricht2" : "message2";
    assertThat(ac.getMessage("code2", null, Locale.ENGLISH)).isEqualTo(expected);
    assertThat(ac.getMessage("code2", null, Locale.GERMAN)).isEqualTo("nachricht2");
    assertThat(ac.getMessage("code2", null, new Locale("DE", "at"))).isEqualTo("nochricht2");
    assertThat(ac.getMessage("code2", null, new Locale("DE", "at", "oo"))).isEqualTo("noochricht2");
    if (reloadable) {
        assertThat(ac.getMessage("code2", null, Locale.GERMANY)).isEqualTo("nachricht2xml");
    }
    MessageSourceAccessor accessor = new MessageSourceAccessor(ac);
    LocaleContextHolder.setLocale(new Locale("DE", "at"));
    try {
        assertThat(accessor.getMessage("code2")).isEqualTo("nochricht2");
    } finally {
        LocaleContextHolder.setLocale(null);
    }
    assertThat(ac.getMessage("code3", null, Locale.ENGLISH)).isEqualTo("message3");
    MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable("code3");
    assertThat(ac.getMessage(resolvable, Locale.ENGLISH)).isEqualTo("message3");
    resolvable = new DefaultMessageSourceResolvable(new String[] { "code4", "code3" });
    assertThat(ac.getMessage(resolvable, Locale.ENGLISH)).isEqualTo("message3");
    assertThat(ac.getMessage("code3", null, Locale.ENGLISH)).isEqualTo("message3");
    resolvable = new DefaultMessageSourceResolvable(new String[] { "code4", "code3" });
    assertThat(ac.getMessage(resolvable, Locale.ENGLISH)).isEqualTo("message3");
    Object[] args = new Object[] { "Hello", new DefaultMessageSourceResolvable(new String[] { "code1" }) };
    assertThat(ac.getMessage("hello", args, Locale.ENGLISH)).isEqualTo("Hello, message1");
    // test default message without and with args
    assertThat(ac.getMessage(null, null, null, Locale.ENGLISH)).isNull();
    assertThat(ac.getMessage(null, null, "default", Locale.ENGLISH)).isEqualTo("default");
    assertThat(ac.getMessage(null, args, "default", Locale.ENGLISH)).isEqualTo("default");
    assertThat(ac.getMessage(null, null, "{0}, default", Locale.ENGLISH)).isEqualTo("{0}, default");
    assertThat(ac.getMessage(null, args, "{0}, default", Locale.ENGLISH)).isEqualTo("Hello, default");
    // test resolvable with default message, without and with args
    resolvable = new DefaultMessageSourceResolvable(null, null, "default");
    assertThat(ac.getMessage(resolvable, Locale.ENGLISH)).isEqualTo("default");
    resolvable = new DefaultMessageSourceResolvable(null, args, "default");
    assertThat(ac.getMessage(resolvable, Locale.ENGLISH)).isEqualTo("default");
    resolvable = new DefaultMessageSourceResolvable(null, null, "{0}, default");
    assertThat(ac.getMessage(resolvable, Locale.ENGLISH)).isEqualTo("{0}, default");
    resolvable = new DefaultMessageSourceResolvable(null, args, "{0}, default");
    assertThat(ac.getMessage(resolvable, Locale.ENGLISH)).isEqualTo("Hello, default");
    // test message args
    assertThat(ac.getMessage("hello", new Object[] { "Arg1", "Arg2" }, Locale.ENGLISH)).isEqualTo("Arg1, Arg2");
    assertThat(ac.getMessage("hello", null, Locale.ENGLISH)).isEqualTo("{0}, {1}");
    if (alwaysUseMessageFormat) {
        assertThat(ac.getMessage("escaped", null, Locale.ENGLISH)).isEqualTo("I'm");
    } else {
        assertThat(ac.getMessage("escaped", null, Locale.ENGLISH)).isEqualTo("I''m");
    }
    assertThat(ac.getMessage("escaped", new Object[] { "some arg" }, Locale.ENGLISH)).isEqualTo("I'm");
    if (useCodeAsDefaultMessage) {
        assertThat(ac.getMessage("code4", null, Locale.GERMAN)).isEqualTo("code4");
    } else {
        assertThatExceptionOfType(NoSuchMessageException.class).isThrownBy(() -> ac.getMessage("code4", null, Locale.GERMAN));
    }
}
Also used : Locale(java.util.Locale) DefaultMessageSourceResolvable(cn.taketoday.context.support.DefaultMessageSourceResolvable) MessageSourceResolvable(cn.taketoday.context.MessageSourceResolvable) NoSuchMessageException(cn.taketoday.context.NoSuchMessageException) PropertyValues(cn.taketoday.beans.PropertyValues) MessageSourceAccessor(cn.taketoday.context.support.MessageSourceAccessor) StaticApplicationContext(cn.taketoday.context.support.StaticApplicationContext) DefaultMessageSourceResolvable(cn.taketoday.context.support.DefaultMessageSourceResolvable)

Example 5 with MessageSourceResolvable

use of cn.taketoday.context.MessageSourceResolvable in project today-framework by TAKETODAY.

the class StaticMessageSourceTests method messageSourceResolvable.

@Test
public void messageSourceResolvable() {
    // first code valid
    String[] codes1 = new String[] { "message.format.example3", "message.format.example2" };
    MessageSourceResolvable resolvable1 = new DefaultMessageSourceResolvable(codes1, null, "default");
    assertThat(MSG_TXT3_US.equals(sac.getMessage(resolvable1, Locale.US))).as("correct message retrieved").isTrue();
    // only second code valid
    String[] codes2 = new String[] { "message.format.example99", "message.format.example2" };
    MessageSourceResolvable resolvable2 = new DefaultMessageSourceResolvable(codes2, null, "default");
    assertThat(MSG_TXT2_US.equals(sac.getMessage(resolvable2, Locale.US))).as("correct message retrieved").isTrue();
    // no code valid, but default given
    String[] codes3 = new String[] { "message.format.example99", "message.format.example98" };
    MessageSourceResolvable resolvable3 = new DefaultMessageSourceResolvable(codes3, null, "default");
    assertThat("default".equals(sac.getMessage(resolvable3, Locale.US))).as("correct message retrieved").isTrue();
    // 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));
}
Also used : MessageSourceResolvable(cn.taketoday.context.MessageSourceResolvable) NoSuchMessageException(cn.taketoday.context.NoSuchMessageException) Test(org.junit.jupiter.api.Test)

Aggregations

MessageSourceResolvable (cn.taketoday.context.MessageSourceResolvable)8 Test (org.junit.jupiter.api.Test)6 NoSuchMessageException (cn.taketoday.context.NoSuchMessageException)4 PropertyValues (cn.taketoday.beans.PropertyValues)2 DefaultMessageSourceResolvable (cn.taketoday.context.support.DefaultMessageSourceResolvable)2 MessageSourceAccessor (cn.taketoday.context.support.MessageSourceAccessor)2 StaticApplicationContext (cn.taketoday.context.support.StaticApplicationContext)2 Locale (java.util.Locale)2