Announcement Announcement Module
Collapse
No announcement yet.
Wrong localized string returned by message source... Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment

    Working...
    X