How do I get the current language in Spring?

I am using this method

LocaleContextHolder.getLocale()

to get the included locale (Japanise), but it returns me in English (default). How can I get the jp_JP locale?

+3


source to share


2 answers


RequestContextUtils

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

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

      

Return the LocaleResolver

one that was attached to the request DispatcherServlet

.
@param request

current HTTP request
@ return the current one 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 can load the locale.



LocaleContextHolder

Or as it is believed by Mohammad tanvirul islam:

LocaleContextHolder.getLocale();

      

You can see the doc here:

+2


source


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

      

so you check the current thread first. To set the locale in current thread usage:



setLocale(Locale locale); 

      

and then LocaleContextHolder.getLocale () will return jp_JP locale

0


source







All Articles