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?
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
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With