相关文章推荐
痴情的饼干  ·  ffserver - bw_0927 - 博客园·  1 年前    · 
憨厚的佛珠  ·  Sencha Touch ...·  1 年前    · 
打酱油的课本  ·  apriori && ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
//Return the Locale associated with the current thread,
// if any, or the   system default Locale else(English)   
LocaleContextHolder.getLocale();

so first you have to check Locale of your current thread. If you want to set Locale in current thread then use this code :

setLocale(Locale locale); LocaleContextHolder.getLocale() will return jp_JP locale

This should allow you to get the current locale of your request:

RequestContextUtils.getLocaleResolver(request).resolveLocale(request);

Return the LocaleResolver that has been bound to the request by the DispatcherServlet.
@param request current HTTP request
@return the current LocaleResolver, or {@code null} if not found:

public static LocaleResolver getLocaleResolver(HttpServletRequest request) {
    return (LocaleResolver) request.getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE);

This will return a LocaleResolver from where you than can load the locale.

LocaleContextHolder

Or as metioned by: Mohammad tanvirul islam:

LocaleContextHolder.getLocale();

You can have a look at the docu here:

  • RequestContextUtils: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/support/RequestContextUtils.html

  • LocaleResolver: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/LocaleResolver.html

  • LocaleContextHolder: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/i18n/LocaleContextHolder.html

  • Giving a simple example for this.Let say you have resource as :Pick any one code

     @Autowired
        private MessageSource messageSource;
    @GetMapping(path = "/hello-world-I18N")
        public String helloWorldInternationalize() {
            return messageSource.getMessage("good.morning.message", null, 
                LocaleContextHolder.getLocale());
    @GetMapping(path = "/hello-world-I18N")
    public String helloWorldInternationalize(@RequestHeader(name = "Accept-Header", required = false) Locale locale) {
        return messageSource.getMessage("good.morning.message", null, locale);
    

    Now in request with POSTMAN send in Headers as : Accept-Language : US/FN etc any you want.

    Configure one LocaleResolver for the same:

            @Bean
            public LocaleResolver localeResolver() {
                AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
                localeResolver.setDefaultLocale(Locale.US); // set default to US
                return localeResolver;
    

    Now in application.properties file add spring.messages.basename=message // message is base name for your properties file.

    Add more file in resource folder with name as : message_fr.properties, message.properties and add the content here. like (good.morning.message = Bonjour)

    the code will work fine.

    There are several ways to create a Locale object.To get the current Local object.

    Locale locale = LocaleContextHolder.getLocale();
    
    Locale locale;
    

    to get the current language

    Locale locale;
    locale.getLanguage()
    Locale locales = LocaleContextHolder.getLocale();
    locales.getLanguage();
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.