Grails 如何更改当前语言环境 [英] Grails how to change the current locale

查看:31
本文介绍了Grails 如何更改当前语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改当前语言环境?

How can I change the current locale ?

  • 我尝试放置 controller/action?lang=de 但我的语言环境仍然是 en_US
  • 我尝试使用这段代码覆盖该值:

  • I tried to put controller/action?lang=de but my locale is still en_US
  • I tried to override the value using this piece of code:

def key = "org.springframework.web.servlet.DispatcherServlet.LOCALE_RESOLVER"
def localeResolver = request.getAttribute(key)
localeResolver.setLocale(request, response, new Locale("de","DE"))

什么都没有改变.

  • 我尝试使用这段代码覆盖该值:

  • I tried to override the value using this piece of code:

import org.springframework.web.servlet.support.RequestContextUtils as RCU;
RCU.getLocaleResolver(request).setLocale(request, response, new Locale("de","DE"))

然后……什么也没发生.我的语言环境仍然设置为 en_US.

And... nothing happened. i still got my locale set to en_US.

有改变语言环境的想法吗?

Any idea to change the locale ?

推荐答案

根据章节 10.Grails 文档的国际化,Grails 支持 i18n 开箱即用,您确实应该能够使用 lang 参数更改区域设置:

According to the chapter 10. Internationalization of the Grails documentation, Grails supports i18n out of the box and you should indeed be able to change the locale using the lang parameter:

默认情况下检测用户区域设置从传入的Accept-Language标题.但是,您可以为用户提供切换语言环境的能力只需传递一个名为 lang 的参数将 Grails 作为请求参数:

By default the user locale is detected from the incoming Accept-Language header. However, you can provide users the capability to switch locales by simply passing a parameter called lang to Grails as a request parameter:

/book/list?lang=de

Grails 会自动切换用户语言环境并将其存储在 cookie 中所以后续的请求将有新标题.

Grails will automatically switch the user locale and store it in a cookie so subsequent requests will have the new header.

但有时您可能想要预设默认语言,因为并非所有应用程序都是英文的.为此,您只需在 resources.groovy spring 配置文件中设置 localeResolver,如下所示:

But sometimes you may want to preset the default language because not all your applications will be in english. To do this, all you have to do is to set your localeResolver in your resources.groovy spring configuration file as shown below:

beans = {
   localeResolver(org.springframework.web.servlet.i18n.SessionLocaleResolver) {
      defaultLocale = new Locale("de","DE")
      java.util.Locale.setDefault(defaultLocale)
   }
}

现在,没有更多详细信息,我无法说明为什么在您的情况下使用 lang 参数不起作用.以防万一,您怎么知道语言环境仍然是 en_US?.

Now, without more details, I can't say why using the lang parameter isn't working in your case. Just in case, how do you know that the locale is still en_US?.

这篇关于Grails 如何更改当前语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆