Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 是否应制作someRestController,以便与LocalResolver一起接收i18n的lang参数?_Spring_Spring Boot_Internationalization_Spring Internationalization - Fatal编程技术网

Spring 是否应制作someRestController,以便与LocalResolver一起接收i18n的lang参数?

Spring 是否应制作someRestController,以便与LocalResolver一起接收i18n的lang参数?,spring,spring-boot,internationalization,spring-internationalization,Spring,Spring Boot,Internationalization,Spring Internationalization,我正在开发Spring Boot应用程序,我需要使用i18n。我看了很多教程,并实现了新的类LocaleConfiguration @Configuration public class LocaleConfiguration implements WebMvcConfigurer { /** * * @return default Locale set by the user */ @Bean(name = "localeResolver") public Local

我正在开发Spring Boot应用程序,我需要使用i18n。我看了很多教程,并实现了新的类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);
  }
}

所以我的问题是,我需要制作新的控制器来处理这个问题,还是由LocalResolver类自动完成?

我会回答你的问题,第一个答案是LocalResolver! 因为您有LocaleSolver Bean,并添加了localeChangeInterceptor,其类层次结构是

LocaleChangeInterceptor是一个拦截器。从源代码可知,它是在请求到达RequestMapping之前执行的。它的作用只是从请求中获取请求参数(默认为locale),然后在LocaleResolver中设置当前的locale

源代码:

@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;
}
看,我不得不发表评论

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

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


你懂吗

我懂,谢谢!但是,当用户想要更改语言时,开发人员将在什么APIaddress上向我发送param lang?直接添加到url。例如:https://{url}?lang=en_us,来自FE的传入参数,API不变
/**
 * Return the name of the parameter that contains a locale specification
 * in a locale change request.
 */
public String getParamName() {
    return this.paramName;
}
/**
 * Default name of the locale specification parameter: "locale".
 */
public static final String DEFAULT_PARAM_NAME = "locale";