代码之家  ›  专栏  ›  技术社区  ›  Nonos

Scala国际化:如何在所有模板中设置显式语言

  •  1
  • Nonos  · 技术社区  · 13 年前

    我已经通读了文档: http://www.playframework.com/documentation/2.1.0/ScalaI18N 以及此帖子: i18n error: controller and templates uses different implicit languages 如果存在隐式请求,那么从请求头隐式地设置语言看起来很简单。在我的应用程序中,我需要根据特定的逻辑(这不取决于请求头中的语言)在所有模板中设置语言。有办法做到这一点吗?

    1 回复  |  直到 9 年前
        1
  •  2
  •   EECOLOR    13 年前

    是的,您需要在视图中添加一个额外的隐式参数,如下所示:

    @(form:Form[_])(implicit lang:play.api.i18n.Lang)
    

    然后在控制器中添加一行,如下所示:

    // this retrieves the language from the configuration, 
    // replace it with your own logic
    implicit val defaultLang = Lang.availables.headOption.getOrElse(Lang.defaultLang)