Namit Khandelwal February 2016

I am trying to read in .properties files with Chinese characters encoded in utf-8

I am trying to read .properties files with having Chinese characters. when I read the them using keys they are printing like ??????. I am writing JSF application. Where I need to do translation for this application in chinese. On UI in JSF it is showing all characters correctly as it should be. But in my java code it is showing like ?????. I donot why it is. I also tried "手提電話" and also tried "\u88dc\u7fd2\u500b\u6848" and tried to print them on console with main function, it is printing correctly in chinese lang chars. my properties file having encoding utf-8.

To clarify your confusion about it, I am able to display them console in chinese like

 String str="Алексей";
    String str="\u88dc\u7fd2\u500b\u6848";
    System.out.println("direct output: "+str);

working fine in psvm. but after reading using properties file is shows ???.e Hope it is clear now. Also My database is receiving the ?? in place for actual chinese charactersets.

Please help. any other clarification required then please confirm so I can update my lines over here.

Here is my code for reading the properties file which returns the bundle of relevant locale.

 FacesContext context = FacesContext.getCurrentInstance();
                    bundle = context.getApplication().getResourceBundle(context, "hardvalue");

after this I am going to call this to access the value:

bundle.getString("tutorsearch.header"); // results in ?????

Any other code you need then please confirm.

Answers


Rick James February 2016

Multiple question marks usually comes from

  • you had utf8-encoded data (good)
  • SET NAMES latin1 was in effect (default, but wrong)
  • the column was declared CHARACTER SET latin1 (default, but wrong)

For Chinese (or Emoji), you need to use MySQL's utf8mb4.

The cure (for future INSERTs):

  • utf8-encoded data (good)
  • mysqli_set_charset('utf8mb4') (or whatever your client needs for establishing the CHARACTER SET)
  • check that the column(s) and/or table default are CHARACTER SET utf8mb4
  • If you are displaying on a web page, <meta...charset=utf-8> should be near the top. (Note different spelling.)


Namit Khandelwal March 2016

adding below tagging have resolved my issue.

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

Thanks to All

Post Status

Asked in February 2016
Viewed 3,109 times
Voted 14
Answered 2 times

Search




Leave an answer