Question

I downloaded multi-module Scala project from GitHub (https://github.com/henrikengstrom/roygbiv), and one of the module is Play 2.0 module. So I can run whole application using SBT's run command on each module, and all works fine. But when I add to Play 2.0 template (index.scala.html) non-English characters and press F5 in browser I get compilation error:

IO error while decoding C:\Users...\web\target\scala-2.9.1\src_managed\main\views\html\index.template.scala with UTF-8 Please try specifying another one using the -encoding option

Play 2.0 module I run also using SBT's run command, not using Play console.

I checked source file encoding - it is UTF-8. Also tried UTF-8 without BOM.

Where can be problem?

Was it helpful?

Solution 2

Your problem seems to be this: your intermediate scala files are not encoded correctly.

Here is the process:

Play takes your template file (foo.scala.html) and translates this into Scala: target/scala-2.10/src_managed/main/views/html/foo.template.scala. This then gets compiled by sbt to .class files and run by play.

When sbt creates these intermediate files, it creates them with the default encoding (in my case a Windows machine so UTF-8 without BOM - your machine may differ). Importantly, this encoding sticks around, so even if I change the encoding of the original template file (foo.scala.html to UTF-16), the encoding of the .scala file is still the same (UTF-8 without BOM in my case). However, the file no longer compiles because the file can't be read because the scala compiler is expecting ITF-8.

The 'correct' solution is to always use UTF-8, and in fact this was the solution recommended for play 1.x see Play documentation Internationalization. Here is the equivalent for play 2. You can also use normal internationalization messages files.

So, if you specify

JAVA_TOOL_OPTIONS='-Dfile.encoding=UTF8' sbt

as suggested by Bjorn, then this will tell sbt that all files that it reads and writes will be in UTF8. You can also specify the file encoding for the scala compiler in your Build.scala:

val main = play.Project(appName, appVersion, appDependencies).settings(
  scalacOptions ++= Seq("-encoding", "UTF-8")
  // Add your own project settings here      
)

This tells the scala compiler that all files that it reads (i.e. the foo.template.scala) are encoded in UTF-8. If you set this to your default encoding, this may work as well.

Your best bet is to do an sbt clean, ensuring that the offending files have disappeared, and restarting with the JAVA_TOOL_OPTION as suggested above. However, you'll have to ensure that all of your builds take this into account (jenkins, other developers etc).

OTHER TIPS

You could try to startup SBT with forced encoding to UTF-8. I read in this post that for some people it helped to start SBT with the following option:

JAVA_TOOL_OPTIONS='-Dfile.encoding=UTF8'

Then one of the first lines of SBT should display:

Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8

The following works fine for me. Encoded in utf-8 default by eclipse(scala-ide)

@(message: String)

@main("Welcome to Play 2.1") {
    <div>Ελληνικά</div>
    <div>
        @message
    </div>
    <br />
    <ul>
     @for(p<-message) {
     <li>
     @p
     </li>
      }
    </ul>

}

What editor are you using to save these files? There might be a possibility that your characters are double encoded and thus stored incorrectly as UTF-8. Eg. characters encoded in iso-8859-1 are encoded again as UTF-8.

I was having this problem and figured out that it was being caused by some characters of my native language I had in the comments (ã). I removed those and the error disappeared.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top