我在我的项目中实现国际化和本地化,其中区域设置是基于语言的URL映射,例如,/en、/ja和/fr。我花了一整天的时间浏览答案,但我读到的大多数答案都是使用LocaleChangeInterceptor setter方法setParamName("lang")。在截取参数lang时,基本上更改区域设置:
http://localhost:8081/index?lang=fr我想在像http://localhost:8081/index/en这样的URL映射中改变地区
目前的配置:
@Configuration
public class MessageConfig implements WebMvcConfigurer {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.JAPAN);
slr.setLocaleAttributeName("session.current.locale");
slr.setTimeZoneAttributeName("session.current.timezone");
return slr;
}
@Bean
public LocaleChangeInterceptor localChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
return localeChangeInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localChangeInterceptor());
}
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("language/messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}资源:

发布于 2022-09-28 06:54:11
我现在回答我的问题。多亏了先生。
首先,我编写了自己的拦截器,在发送到控制器之前扫描URL模式:
@Component
public class UrlLocaleInterceptor implements HandlerInterceptor {
protected final Log logger = LogFactory.getLog(this.getClass());
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
String[] tokens = request.getRequestURI().trim().split("/");
try {
String newLocale = tokens[1];
if (newLocale != null) {
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
if (localeResolver == null) {
throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
}
if (Languages.getNames().contains(newLocale)) {
localeResolver.setLocale(request, response, parseLocaleValue(newLocale));
} else {
localeResolver.setLocale(request, response, parseLocaleValue("ja"));
}
}
} catch (Exception e) {
logger.error(e);
}
return true;
}
@Nullable
protected Locale parseLocaleValue(String localeValue) {
return StringUtils.parseLocale(localeValue);
}
}第二。通过构造函数注入初始化UrlLocaleInterceptor实例,然后将其注册到InterceptorRegistry
@Configuration
public class MessageConfig implements WebMvcConfigurer {
private final UrlLocaleInterceptor urlInterceptor;
@Autowired
public MessageConfig(UrlLocaleInterceptor urlInterceptor) {
this.urlInterceptor = urlInterceptor;
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(new Locale("ja"));
slr.setLocaleAttributeName("session.current.locale");
slr.setTimeZoneAttributeName("session.current.timezone");
return slr;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(urlInterceptor);
}
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("language/messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}第三。我定义Language枚举来枚举应用程序支持的语言。方法getNames()由preHandle()从UrlLocaleInterceptor调用。
public enum Languages {
en, ja;
public static Set<String> getNames() {
return Arrays.stream(Languages.values())
.map(Enum::name)
.collect(Collectors.toSet());
}
}第四。我向控制器添加了一个路径变量{lang}。
@Controller
@RequestMapping("/{lang}")
public class CustomerController {
@GetMapping("/")
public String root() {
return "index";
}
}https://stackoverflow.com/questions/73784958
复制相似问题