-1

Ref: https://www.branah.com/unicode-converter

I'm new in scala and java and trying to writ a .properties file (in few languages like Chinese,french,German etc ) using scala for internationalization functionality. For that I'm using following code:

for ((key, val) <- jsonData.get.asInstanceOf[Map[String, String]]) {
    var file: PrintWriter = null
    file = new PrintWriter(filepath, "UTF-8")
    prop.setProperty(key, val)
    prop.store(file, "")
    file.close()
}

So this code is working but its writing file in UTF-8 format like:

传播特征 设计师 考虑 测量 düşünce

which is not rendering properly in browser so instead of that I want to convert it into the UTF-16 unicode format like:

\u4f20\u64ad\u7279\u5f81 \u8bbe\u8ba1\u5e08 \u8003\u8651 \u6d4b\u91cf \u0064\u00fc\u015f\u00fc\u006e\u0063\u0065

As per this converter: https://www.branah.com/unicode-converter

I don't have access of client side so can't post that code here but I'm sure its same like fetching data from .properties file through ajax and rendering it on browser.

How can I convert it into utf-16 unicode so that it'll render properly in browser.

Any help would be appreciated.

J.K.A.
  • 7,272
  • 25
  • 94
  • 163
  • 1
    "which is not rendering properly in browser" - how does this have anything to do with a browser? You should tell us what you're doing with the properties file after you've written it - perhaps the problem is how you're loading it later. – Jon Skeet Jun 26 '17 at 04:34
  • Simply I'm fetching data from .properties file and displaying it on browser through html – J.K.A. Jun 26 '17 at 04:37
  • 1
    So why don't you show us *that* code? Why assume the problem is with it being UTF-8? And what exactly do you mean by "which is not rendering properly"? How *is* it rendering? – Jon Skeet Jun 26 '17 at 04:39
  • Its writing `?????` on .properties file if I'm doing UTF-8 to UTF-16 and on browser its rendering garbage code like this `设计å¸`. I hope you clear now – J.K.A. Jun 26 '17 at 04:59
  • Not really, because you *still* haven't shown us the code you're using to load the properties file. It sounds like you're looking at the properties file locally in an editor which doesn't understand UTF-8, and you're loading the properties file with code which isn't expecting UTF-8. But while you keep not showing us that code, we can't help you. – Jon Skeet Jun 26 '17 at 05:07
  • Actually I don't have access of that code. (Handling server side only) I just have to write .properties file and to test whether its working properly on browser or not. So let me explain you again, in .properties if I'm writing `interprète` then its not working but If I'm changing `è` to `\u00e8` (i.e. interpr\u00e8te) then its rendering properly on browser. – J.K.A. Jun 26 '17 at 05:20
  • I'm surprise that I'm getting -ve votes after so much explaination :( – J.K.A. Jun 26 '17 at 05:23
  • So you don't even know what that code does? That's unfortunate, and should be in the *question* rather than only in comments. It sounds like you should probably just use `Properties.store(OutputStream, String)` which may do what you want. – Jon Skeet Jun 26 '17 at 05:23
  • Sorry..Yes I've to add in comment. Will update. – J.K.A. Jun 26 '17 at 05:25
  • Can you please explain how to use `Properties.store(OutputStream, String)` not getting this one. – J.K.A. Jun 26 '17 at 05:28
  • You create an `OutputStream` (e.g. `FileOutputStream`) and call `store` with it... just pass in `null` as the second argument if you want. – Jon Skeet Jun 26 '17 at 05:50
  • Note that the code that loads the properties file would still be on the server side, so you *should* have access to that... (Sure, the display part may be in Javascript, but if the file is on the server, it's got to be server code that loads it...) – Jon Skeet Jun 26 '17 at 05:51

2 Answers2

1

You can use the same code and user "UTF-16"for the Charset

for ((key, val) <- jsonData.get.asInstanceOf[Map[String, String]]) {
    var file: PrintWriter = null
    file = new PrintWriter(filepath, "UTF-16")
    prop.setProperty(key, val)
    prop.store(file, "")
    file.close()
}

Please check the supported java Charset https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html

maxmithun
  • 1,089
  • 9
  • 18
0

Solved it myself.

replaced FileOutputStream with PrintWriter and it worked :)

var file: File = null
file = new File(filepath)                            
var fos: FileOutputStream = null
fos = new FileOutputStream(file)
prop.setProperty(key, val)
prop.store(fos, "")

Thanks @Jon Skeet for help.

J.K.A.
  • 7,272
  • 25
  • 94
  • 163