Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read Japanese Characters from a properties file using Resource Bundle

I am working on developing a project and when I attempt to read in a Japanese character string from a properties file using ResourceBundle in Java, all I get are a bunch of question marks (??????????) displayed in my application.

I know that I need to encode the string somehow, but I've tried several different ways with no luck. I've also tried to encode the properties file in a text editor but this doesn't seem to work how I'd like either. My code for retrieving the string is below. I know you can do this with stream readers and other methods but I would like to try to stay with the current code structure using ResourceBundle if possible. If anyone has any questions or needs clarification please feel free to let me know.

private final ResourceBundle bundle = ResourceBundle.getBundle("propertiesFile");

titleText = bundle.getString("title.text");

Just to give you an idea, title.text=会議参加者事前登録用ページ in my property file.

Any suggestions are appreciated. This has turned into a real pain.

Thanks again,

-Dave F.

like image 660
MotoDave452 Avatar asked Nov 28 '25 11:11

MotoDave452


2 Answers

ResourceBundle can only read ISO 8859-1 file. See this thread. How to use UTF-8 in resource properties with ResourceBundle

I had the same problem here, and I've converted my ResourceBundle to a Properties object like in the answer of this thread: read resourcebundle as UTF-8. getString() Method seems to change encoding to ISO-8859

Like this:

InputStream utf8in = getClass().getClassLoader().getResourceAsStream("/path/to/utf8.properties");
Reader reader = new InputStreamReader(utf8in, "UTF-8");
Properties props = new Properties();
props.load(reader);
like image 61
JFPicard Avatar answered Dec 01 '25 01:12

JFPicard


After some research I was actually still able to make use of the ResourceBundle method.

In my properties file that ResourceBundle pulls from, I listed the following in unicode:

title.text=\u4f1a\u8b70\u53c2\u52a0\u8005\u4e8b\u524d\u767b\u9332\u7528\u30da\u30fc\u30b8

When pulled in by ResourceBundle, it translates as:

会議参加者事前登録用ページ 

I'm sure this probably isn't the best practice, but it works without having to change how the entire project pulls in its resource properties, so I just thought I would share with you all for helping me out.

like image 37
MotoDave452 Avatar answered Nov 30 '25 23:11

MotoDave452



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!