我怎样才能从 scala Play 中提供大部分静态页面!应用程序喜欢不同语言的"About"页面?



我正在使用Play!框架版本2.0.4。一些法律要求的页面,如印记,大多包含大量(每个约10k)的静态文本,我想以不同的语言提供。

在版本1。x有一个#include指令,允许使用当前的Lang构造实际的资源路径。

与Play 2.x实现类似的东西的推荐方法是什么?

谢谢你&最好的问候,Erich

我不是100%确定你现在如何实现它,但这是我想到的。

您可以编写自己的include helper。将以下内容保存在views文件夹中的Helpers.scala文件中。说明在代码的注释中。

package views.html.helper
object include {
  import play.api.templates.Html
  import play.api.Play
  import play.api.Play.current
  import play.api.i18n._
  // The default is to look in the public dir, but you can change it if necessary
  def apply(filePath: String, rootDir: String = "public")(implicit lang: Lang): Html = {
    // Split filePath at name and suffix to insert the language in between them
    val (fileName, suffix) = filePath.splitAt(filePath.lastIndexOf("."))
    // Retrieve the file with the current language, or as a fallback, without any suffix
    val maybeFile =
      Play.getExistingFile(rootDir + "/" + fileName + "_" + lang.language + suffix).
      orElse(Play.getExistingFile(rootDir + "/" + filePath))
    // Read the file's content and wrap it in HTML or return an error message
    maybeFile map { file =>
      val content = scala.io.Source.fromFile(file).mkString
      Html(content)
    } getOrElse Html("File Not Found")
  }
}

现在在你的imprint.scala.html中你可以这样称呼它:

@()(implicit lang: Lang)
@import helper._
@include("static/imprint.html")

Schleichardt显示的方式在play-authenticate中用于选择不同语言的邮件模板,现在它被更改为与控制器上的反射一起工作,所以它可能对您来说很有趣。无论如何,它有意保持标准模板的可能性(因为每封邮件都需要在发送之前进行个性化设置)

对于静态信息页面,您可以将每种语言的代码以后缀ie保存。impressum_en.html, impressum_de.html在文件系统中,并使用简单的控制器,它将找到具有适当后缀的文件,并返回其内容。您可能需要返回Ok(fileContent)并手动设置Content-Type为text/html

另一种选择是做类似的事情,但将其存储在DB中,因此您可以创建简单的后端并使用浏览器编辑它。

如果您仍然需要替换一些元素,您可以在代码中使用一些###MARKER### +简单的String操作,或者在客户端使用JavaScript来完成。

2.0中的模板工作方式略有不同。编译基于Scala的模板。从一个模板,而不是"包含"另一个模板,你可以调用它。我不太确定你说的语言是什么意思。在2.0中,模板的形参可以是语言。

示例模板名为'included' in package whateverpackage.

@(lang: Lang)
...

示例调用名为'included'的模板:

@import whateverpackage._
@included(Lang("en"))
@()(implicit lang: Lang)
@main(Messages("imprint")) {
    @lang match {
        case Lang("de", _) => { @germanImprint() @* uses app/views/germanImprint.scala.html file *@}
        case _ => { @englishImprint() }
    }
}

相关内容

最新更新