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

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

问题描述




  • 我试着把 controller / action?lang = de 但我的语言环境仍然是 en_US

  • 我尝试使用这段code:

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




没有任何变化。




  • 使用这段代码覆盖这个值:

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




并且......没有任何事情发生。我仍然将我的语言环境设置为 en_US



改变语言环境的任何想法?

解决方案

根据 10。国际化,Grails支持i18n开箱即用,您应该可以使用 lang 参数更改区域设置:


默认情况下,来自接受语言 $ b $的用户区域设置被检测到
b头。但是,您可以向用户
提供将区域设置切换
的功能,只需将一个名为 lang
的参数作为请求参数传递给Grails即可:

  / book / list?lang = de 

Grails将自动切换
用户区域设置并将其存储在一个cookie
中,以便后续请求将拥有
新的标题。


但有时您可能需要预设默认语言,因为并非所有的应用程序都是英文的。要做到这一点,你所要做的就是在你的 resources.groovy spring配置文件中设置你的 localeResolver ,如图所示如下:

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

现在,没有更多细节,我不能说为什么使用 lang 参数不是在你的情况下工作。以防万一,你怎么知道语言环境仍然是en_US?


How can I change the current locale ?

  • 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"))
    

Nothing changed.

  • 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"))
    

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

Any idea to change the locale ?

解决方案

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:

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 will automatically switch the user locale and store it in a cookie so subsequent requests will have the new header.

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)
   }
}

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天全站免登陆