use of org.springframework.context.support.ResourceBundleMessageSource in project spring-framework by spring-projects.
the class DataBinderTests method bindingErrors.
@Test
void bindingErrors() {
TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person");
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("age", "32x");
binder.bind(pvs);
Errors errors = binder.getBindingResult();
FieldError ageError = errors.getFieldError("age");
assertThat(ageError.getCode()).isEqualTo("typeMismatch");
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("org.springframework.validation.messages1");
String msg = messageSource.getMessage(ageError, Locale.getDefault());
assertThat(msg).isEqualTo("Field age did not have correct type");
messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("org.springframework.validation.messages2");
msg = messageSource.getMessage(ageError, Locale.getDefault());
assertThat(msg).isEqualTo("Field Age did not have correct type");
messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("org.springframework.validation.messages3");
msg = messageSource.getMessage(ageError, Locale.getDefault());
assertThat(msg).isEqualTo("Field Person Age did not have correct type");
}
use of org.springframework.context.support.ResourceBundleMessageSource in project spring-framework by spring-projects.
the class JstlUtils method getJstlAwareMessageSource.
/**
* Checks JSTL's "jakarta.servlet.jsp.jstl.fmt.localizationContext"
* context-param and creates a corresponding child message source,
* with the provided Spring-defined MessageSource as parent.
* @param servletContext the ServletContext we're running in
* (to check JSTL-related context-params in {@code web.xml})
* @param messageSource the MessageSource to expose, typically
* the ApplicationContext of the current DispatcherServlet
* @return the MessageSource to expose to JSTL; first checking the
* JSTL-defined bundle, then the Spring-defined MessageSource
* @see org.springframework.context.ApplicationContext
*/
public static MessageSource getJstlAwareMessageSource(@Nullable ServletContext servletContext, MessageSource messageSource) {
if (servletContext != null) {
String jstlInitParam = servletContext.getInitParameter(Config.FMT_LOCALIZATION_CONTEXT);
if (jstlInitParam != null) {
// Create a ResourceBundleMessageSource for the specified resource bundle
// basename in the JSTL context-param in web.xml, wiring it with the given
// Spring-defined MessageSource as parent.
ResourceBundleMessageSource jstlBundleWrapper = new ResourceBundleMessageSource();
jstlBundleWrapper.setBasename(jstlInitParam);
jstlBundleWrapper.setParentMessageSource(messageSource);
return jstlBundleWrapper;
}
}
return messageSource;
}
use of org.springframework.context.support.ResourceBundleMessageSource in project smart-cloud by smart-cloud.
the class LocaleAutoConfiguration method messageSource.
@Bean
public MessageSource messageSource(final SmartProperties smartProperties) {
LocaleProperties localeProperties = smartProperties.getLocale();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = null;
try {
resources = resolver.getResources(LocaleConstant.LOCALE_PATTERN);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
if (resources != null) {
String[] strResources = new String[resources.length];
for (int i = 0; i < resources.length; i++) {
Resource resource = resources[i];
strResources[i] = LocaleConstant.LOCALE_DIR + resource.getFilename().replace(LocaleConstant.LOCALE_PROPERTIES_SUFFIX, "");
}
messageSource.setBasenames(strResources);
}
messageSource.setDefaultEncoding(localeProperties.getEncoding());
messageSource.setCacheSeconds(localeProperties.getCacheSeconds());
return messageSource;
}
use of org.springframework.context.support.ResourceBundleMessageSource in project cloud-pipeline by epam.
the class TestApplicationWithAclSecurity method messageSource.
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
use of org.springframework.context.support.ResourceBundleMessageSource in project libresonic by Libresonic.
the class LibresonicThemeSource method createMessageSource.
@Override
protected MessageSource createMessageSource(String basename) {
ResourceBundleMessageSource messageSource = (ResourceBundleMessageSource) super.createMessageSource(basename);
// Create parent theme recursively.
for (Theme theme : settingsService.getAvailableThemes()) {
if (basename.equals(basenamePrefix + theme.getId()) && theme.getParent() != null) {
String parent = basenamePrefix + theme.getParent();
messageSource.setParentMessageSource(createMessageSource(parent));
break;
}
}
return messageSource;
}
Aggregations