Java Spring:localeChangeInterceptor 和子域
我有一个 Spring MVC web 应用程序,想要执行此操作(示例):
- 请求子域 en.foo.com ->语言应为英语
- 请求子域es.foo.com ->语言应该是西班牙语
我已经使用localeChangeInterceptor
根据请求通过 URL 参数更改语言,并且这有效(向我的 URL 添加参数,例如 ?lang=en)。
但是使用 Spring 拥有特定于语言的子域的最佳方式是什么?我搜索了网络和 Spring 文档,但尚未找到答案。
我认为它可以这样工作:
- 拦截任何请求
- 确定使用的子域
- 决定应使用哪种语言/区域设置并像 localeChangeInterceptor 那样设置它
如果这是要走的路,我可以在其中使用提示开始。
提前致谢!
I have a Spring MVC webapp and want to do this (example):
- Request to subdomain en.foo.com -> language should be English
- Request to subdomain es.foo.com -> language should be Spanish
I already use the localeChangeInterceptor
to change the language by URL parameter on request, and this works (adding a parameter e.g. ?lang=en to my URLs).
But what is the best way to have language-specific subdomains with Spring? I searched the web and Spring documentation, but have yet to find an answer.
I think it could work like this:
- Intercept any request
- Determine used subdomain
- Decide which language/locale should be used and set it like the
localeChangeInterceptor
does
If this is the way to go, I could use a hint where to start.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

您将需要实现自己的 LocaleResolver。
Spring 使用 LocaleResolver.resolveLocale(HttpServletRequest) 方法来确定要使用 MessageSource 中的 Locale。您可以从请求中提取子域并返回所需的区域设置。
You will want to implement your own LocaleResolver.
The LocaleResolver.resolveLocale(HttpServletRequest) method is what Spring uses to determine which Locale to use from the MessageSource. You can extract the subdomain from the request and return the desired Locale.