How can I serve mostly static pages from scala Play! How is the About page in different languages?

I am creating an application using Play! framework version 2.0.4. Some pages that are legally required, such as a fingerprint, mainly contain lots (about 10 thousand each) of static text, which I would like to provide in different languages.

In versions 1.x, there is a #include directive that allows you to create the actual resource path using the current Lang .

What is the recommended way to implement sth with Play 2.x?

Thanks and best regards, Erich

+4
source share
4 answers

I’m not 100% sure how you implement it, but here’s what I came up with.

You can simply write your own include helper. Save the following in the Helpers.scala file in the view folder. Explanations are given in the code comments.

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

Now in imprint.scala.html you can call it like this:

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

The path shown by Schleichardt was used in play-authenticate to select letter templates in different languages, and now it has changed to work with reflections on the controller, so this may be interesting for you. In any case, he intended to support the standard features of the templates (so that each mail was personalized before sending)

For static information pages, you can simply save the code for each language with a suffix, i.e. impressum_en.html , impressum_de.html in the file system and use a simple controller that will find the file with the appropriate suffix and return its contents exactly as it is ... all you need to return Ok (fileContent) and set Content -Type manually to text/html .

Another option does a similar thing, but saves it in the database, so you can create a simple server and edit it in a browser.

If you still need to replace some elements, you can do this with some ###MARKER### in code operations + simple String or with client-side JavaScript.

+1
source

Templates in 2.0 work a little differently. Scala-based templates compiled. From one template, instead of including another, you can call him. I do not quite understand what you mean by language. In 2.0, however, the parameter for the template may be the language.

An example template named 'included' in anypackage.

 @(lang: Lang) ... 

An example of calling a template with the name "enabled":

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

All Articles