Search in sources :

Example 1 with MessageSourceAccessor

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

the class ApplicationContextSupport method setApplicationContext.

@Override
public final void setApplicationContext(@Nullable ApplicationContext context) throws BeansException {
    if (context == null && !isContextRequired()) {
        // Reset internal context state.
        this.applicationContext = null;
        this.messageSourceAccessor = null;
    } else if (this.applicationContext == null) {
        // Initialize with passed-in context.
        if (!requiredContextClass().isInstance(context)) {
            throw new ApplicationContextException("Invalid application context: needs to be of type [" + requiredContextClass().getName() + "]");
        }
        this.applicationContext = context;
        this.messageSourceAccessor = new MessageSourceAccessor(context);
        initApplicationContext(context);
    } else {
        // Ignore reinitialization if same context passed in.
        if (this.applicationContext != context) {
            throw new ApplicationContextException("Cannot reinitialize with different application context: current one is [" + this.applicationContext + "], passed-in one is [" + context + "]");
        }
    }
}
Also used : MessageSourceAccessor(cn.taketoday.context.support.MessageSourceAccessor) ApplicationContextException(cn.taketoday.context.ApplicationContextException)

Example 2 with MessageSourceAccessor

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

the class ApplicationContextSupport method setApplicationContext.

@Override
public final void setApplicationContext(@Nullable ApplicationContext context) throws BeansException {
    if (context == null && !isContextRequired()) {
        // Reset internal context state.
        this.applicationContext = null;
        this.messageSourceAccessor = null;
    } else if (this.applicationContext == null) {
        // Initialize with passed-in context.
        if (!requiredContextClass().isInstance(context)) {
            throw new ApplicationContextException("Invalid application context: needs to be of type [" + requiredContextClass().getName() + "]");
        }
        this.applicationContext = context;
        this.messageSourceAccessor = new MessageSourceAccessor(context);
        initApplicationContext(context);
    } else {
        // Ignore reinitialization if same context passed in.
        if (this.applicationContext != context) {
            throw new ApplicationContextException("Cannot reinitialize with different application context: current one is [" + this.applicationContext + "], passed-in one is [" + context + "]");
        }
    }
}
Also used : MessageSourceAccessor(cn.taketoday.context.support.MessageSourceAccessor) ApplicationContextException(cn.taketoday.context.ApplicationContextException)

Example 3 with MessageSourceAccessor

use of cn.taketoday.context.support.MessageSourceAccessor 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 4 with MessageSourceAccessor

use of cn.taketoday.context.support.MessageSourceAccessor in project today-infrastructure 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)

Aggregations

MessageSourceAccessor (cn.taketoday.context.support.MessageSourceAccessor)4 PropertyValues (cn.taketoday.beans.PropertyValues)2 ApplicationContextException (cn.taketoday.context.ApplicationContextException)2 MessageSourceResolvable (cn.taketoday.context.MessageSourceResolvable)2 NoSuchMessageException (cn.taketoday.context.NoSuchMessageException)2 DefaultMessageSourceResolvable (cn.taketoday.context.support.DefaultMessageSourceResolvable)2 StaticApplicationContext (cn.taketoday.context.support.StaticApplicationContext)2 Locale (java.util.Locale)2