2012-02-22 33 views
1

我有一個Spring MVC的Web應用程序,並要做到這一點(例如):Java的春天:localeChangeInterceptor和子域

  • 請求子域en.foo.com - >語言應該英語
  • 請求子域es.foo.com - >語言應該西班牙

我已經使用localeChangeInterceptor根據請求通過URL參數更改語言,並且這可以工作(例如添加一個參數?lang = en到我的網址)。

但是什麼是語言特定的子域與Spring的最佳方式是什麼?我搜索了網絡和Spring文檔,但還沒有找到答案。

我認爲它可以工作是這樣的:

  1. 攔截任何請求
  2. 確定所使用的子域
  3. 決定哪些語言/應使用區域,並設置它像localeChangeInterceptor確實

如果這是要走的路,我可以使用提示從哪裏開始。

在此先感謝!

回答

3

你會想要實現自己的LocaleResolver

LocaleResolver.resolveLocale(HttpServletRequest)方法是Spring用來確定從MessageSource使用哪個Locale。您可以從請求中提取子域並返回所需的區域設置。

+0

謝謝,我會試試! – tholu 2012-02-22 17:17:34

+0

我已經用'AcceptHeaderLocaleResolver'作爲後備編寫了自己的'LocaleResolver'。什麼現在不工作是'LocaleChangeInterceptor' - 它調用我的'setLocale'方法,但在那裏'response.setLocale(locale)'不會改變響應的語言環境。標準的'AcceptHeaderLocaleResolver'如何做到這一點?它根本沒有實現'setLocale'方法 - 我很困惑。 – tholu 2012-02-23 14:39:04

+0

你必須在'LocaleResolver'中使用'SessionLocaleResolver'作爲後備,一切正常。 – tholu 2012-02-23 14:45:29