如何从scala播放大部分静态页面!应用程序像不同语言的“关于”
发布时间:2020-12-16 08:48:20 所属栏目:安全 来源:网络整理
导读:我正在使用Play构建应用程序!框架版本2.0.4.一些法律要求的页面(如印记)主要包含许多(每个约10k)静态文本,我想用不同的语言提供. 在1.x版本中,有一个#include指令允许使用当前的Lang构造实际的资源路径. 与Play 2.x类似的推荐方法是什么? 谢谢你最好的祝福
|
我正在使用Play构建应用程序!框架版本2.0.4.一些法律要求的页面(如印记)主要包含许多(每个约10k)静态文本,我想用不同的语言提供.
在1.x版本中,有一个#include指令允许使用当前的Lang构造实际的资源路径. 与Play 2.x类似的推荐方法是什么? 谢谢你&最好的祝福, 解决方法
我不是100%肯定你现在如何实现它,但这就是我想出来的.
你可以编写自己的include帮助器.将以下内容保存在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")
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
