2

I am facing which having different scenario.I have portal which saves the locale of the user in database.When The use get logging.I am getting user detail but I don't know how to set it in controller.

I know that we can use LocaleContextHolder to set the locale but I dont know how it will work with controller. Please will you give some example

This is my controller

@RequestMapping(value = "/showMotionProfile", method = RequestMethod.GET)
public String showMotionProfile(Model model, RedirectAttributes attributes,
        HttpServletRequest request) {

    Locale locale = LocaleContextHolder.getLocale();
    logger.info("---------->country"+locale.getCountry());

    logger.info("\n--------------showMotionProfile-----------\n");
    LocaleContextHolder.setLocale(locale.ITALIAN);
    logger.info("---------->country"+LocaleContextHolder.getLocale());

    return "showMotionProfile";
}

Please tell me if I have done right,cause It is not reflecting.

Thanks for help.

EDIT: I have this resolver config in my spring xml file:

<beans:bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleR‌​esolver"> 
    <beans:property name="defaultLocale" value="en" /> 
</beans:bean> 
<interceptors> 
    <beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeI‌​nterceptor">
        <beans:property name="paramName" value="locale"></beans:property> 
    </beans:bean> 
</interceptors> 

Can I use this by using @Autowired in my controller and setting the locale?

4
  • 1
    Don't. Just implement a custom LocaleResolver and configure it appropriatly. Commented Apr 23, 2015 at 13:29
  • I have this resolver config in my spring xml file. <beans:bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"> <beans:property name="defaultLocale" value="en" /> </beans:bean> <interceptors> <beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> <beans:property name="paramName" value="locale"></beans:property> </beans:bean> </interceptors> Can I use this by doing autowired in my controller and set the locale. Commented Apr 23, 2015 at 13:47
  • Please don't add config as comments, modify your post. I fyou configuration is stored in a database yuo need a custom LocaleResolver which initially reads it from DB, stores in in the session or cookie (or whatever) and has a nice fallback. Then you can use the regular LocaleChangeInterceptor to modify the Locale. Commented Apr 23, 2015 at 13:49
  • I have saved locale in session can I use LocaleChangeIntercepter by my self in controller to change the locale Commented Apr 23, 2015 at 14:00

1 Answer 1

4

In general you are on the right track:

  • Add the SessionLocaleResolver to your application context.
  • Add the LocalChangeInterceptor to your application context, but note that this will only change the current locale, if "locale" is passed as request parameter to any of your web controllers.
  • Alternatively, inject the sessionLocalResolver into your Controller (using @Autowire or the application context XML) and call the setLocale method on it. That way you can pass it the locale value from your database.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.