springspring-bootinternationalizationspring-internationalization

Should someRestController be made for receiving lang parameter for i18n along with LocaleResolver?


I am developing Spring Boot application, and I need to work with i18n. I watched a lot of tutorials and I implemented new class LocaleConfiguration

@Configuration
public class LocaleConfiguration implements WebMvcConfigurer {

  /**
   * * @return default Locale set by the user
   */
  @Bean(name = "localeResolver")
  public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    slr.setDefaultLocale(Locale.US);
    return slr;
  }

  /**
   * an interceptor bean that will switch to a new locale based on the value of the language parameter appended to a request:
   *
   * @param registry
   * @language should be the name of the request param i.e  localhost:8010/api/get-greeting?language=fr
   * <p>
   * Note: All requests to the backend needing Internationalization should have the "language" request param
   */
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
    localeChangeInterceptor.setParamName("lang");
    registry.addInterceptor(localeChangeInterceptor);
  }
}

And also, I made few messages_code.propertie files with proper languages. I set thymeleaf template just to see if everything is working and that is okay. FrontEnd developer just need to send me lang param and that is it. But my question is, should I make a new controller which will handle that call with lang parameter or all that is somehow automatically done via this LocaleConfiguration class? Because I get proper translations when I make this call in Postman/Browser:

http://localhost:8080/?lang=fra

So my question is, do I need to make new Controller to handle that or is it automatically done by LocaleResolver class?


Solution

  • I will answer your question first answer is LocaleResolver ! Because you have LocaleResolver Bean, and add localeChangeInterceptor, And its class hierarchy is
    enter image description here

    LocaleChangeInterceptor is an interceptor. It is known from the source code that it is executed before the request reaches RequestMapping. Its role is simply to obtain the request parameters from the request (the default is locale), and then set the current locale in LocaleResolver.

    source code:

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws ServletException {
        //Note here
        **String newLocale = request.getParameter(getParamName());**
        if (newLocale != null) {
            if (checkHttpMethod(request.getMethod())) {
                LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
                if (localeResolver == null) {
                    throw new IllegalStateException(
                            "No LocaleResolver found: not in a DispatcherServlet request?");
                }
                try {
                    localeResolver.setLocale(request, response, parseLocaleValue(newLocale));
                }
                catch (IllegalArgumentException ex) {
                    if (isIgnoreInvalidLocale()) {
                        logger.debug("Ignoring invalid locale value [" + newLocale + "]: " + ex.getMessage());
                    }
                    else {
                        throw ex;
                    }
                }
            }
        }
        // Proceed in any case.
        return true;
    }
    

    See i have to comment.

    String newLocale = request.getParameter(getParamName());
    

    transfer

    /**
     * Return the name of the parameter that contains a locale specification
     * in a locale change request.
     */
    public String getParamName() {
        return this.paramName;
    }
    

    among them this.paramName is

    /**
     * Default name of the locale specification parameter: "locale".
     */
    public static final String DEFAULT_PARAM_NAME = "locale";
    

    So do you understand