Results 1 to 2 of 2

Thread: Wrong localized string returned by message source...

  1. #1
    Join Date
    Apr 2011
    Posts
    20

    Default Wrong localized string returned by message source...

    Hi,

    I have a bit of a problem with my Validator class. It is returning error strings with part of the message in Spanish, and part of it in English..

    messageSource bean definition:
    Code:
    	<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    		<property name="basenames" >
    			<list>
    			   <value>WEB-INF/content/messages</value>
    			   <value>WEB-INF/content/validations</value>
    			 </list>
    		 </property>
    		 <property name="fallbackToSystemLocale" value="false"></property>
    	</bean>
    Code:
    public class ModelXValidator implements Validator{
    
    	private final String DEFAULT_MESSAGE= "Default Message";
    	private String format = "dd/M/yyyy";
    	
    	@Autowired
    	private MessageSource messageSource;
    	
            private String fieldlabelPhone;
            ...
    }
    I have a function that will load fieldLabel variables:

    Code:
    	public void setFieldLabelsX()
    	{
    	    
    	    this.fieldlabelPhone= messageSource.getMessage("fieldlabel.general.phone",null,null);
    	}
    Then I'll use the fieldLabelPhone value in the following manner:

    Code:
    public void validateStart(BusinessProfile business, Errors errors)
    {
    		//phone
    		if(GenericValidator.isBlankOrNull(business.getContactInformation().getPhoneNumber()))
    			errors.rejectValue("contactInformation.phoneNumber","general.validation.field.required", new Object[] {fieldlabelPhone}, DEFAULT_MESSAGE);
    ...
    }
    My expectation is for it to return:

    Teléfono es requerido. (Spanish, default)
    Phone Number is required. (English)

    However, it's returning: "Phone es requerido." (a mix of Spanish and English) when the locale is Spanish. That is because messageSource.getMessage("fieldlabel.general.phone ",null,null); is returning an english String. Yet, the English locale works like a charm.

    Is there any way to access the locale inside a Validator class so that I can pass it on to the messageSource? Why does it return the proper error message (spanish) but not the proper field label String?

    thanks,
    del

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Is there any way to access the locale inside a Validator class so that I can pass it on to the messageSource? Why does it return the proper error message (spanish) but not the proper field label String?
    Why because that is what you requested it to do... You are getting the fieldLabel with no local which basically defaults to the default locale and not the selected locale. The rejectValue uses the current locale to resolve the message. So in short don't initialize your default fieldLabels but retrieve them when needed and use the current locale.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •