Reading Chinese String from file

Hi Juce,

I stored some Chinese strings in a txt file, encoding the file in UTF-8. When I use File::loadFileAsString() or File::readLines function, cannot get the right Chinese string but some messy code. Would you please give some suggestion for this kind of problem?

Thanks

Those functions definitely read utf-8 correctly, I use them all the time and have unit-tests to check them.

My guess is that the file doesn’t really contain utf-8, or that the string is being read correctly but displayed wrongly (e.g. without a chinese font)

Ok, I try another computer which has Chinese Win7 OS, and I can read Chinese String by a ANSI txt file, but when file change to UTF8, it will show messy code. I add label code as:
String testStr(CharPointer_UTF8(“测试”));
_heading = new Label();
_heading->setText(testStr, true);

Then the application crash as LeakedObjectDetector, but when I change it to String testStr(CharPointer_UTF8(“test”)), the application run well! What’s wrong?

I want to localize and show some Chinese Strings in the app, and I set the Font as “Arial Unicode MS”, would you give me some suggestion?

Thanks

You can’t put extended chars into a c++ file - compilers will get it wrong. Use the introjucer’s “UTF-8 String Literal Converter” from its tools menu.

Hi Juce,

I have used “UTF-8 String Literal Converter” tool, but when I input “好”(Chinese char) in it, it just show a square in the input panel and display “CharPointer_UTF8 (”\xe5\xa5\xbd")" in the output panel. I copied the string into my code, cannot show the Chinese char but a square for that.

I plan to do the localization work from En version to Chinese version, and used LocalisedStrings. I read strings from a txt file and use “translate” function to make it as Chinese. When debugging, I can see the translated Chinese string, but it showed as a square when displaying in treeview. And I have rewriten “paint” function for TreeViewItem, using font.setTypefaceName(“Arial Unicode MS”) and graphic.setFont fuction. Would you please give me some suggestion for why the Chinese string cannot be displayed correctly?

Thanks

1 Like

You need to change the default font to something that can accept Chines Characters. Change  the default font property of your LookAndFeel object that you are using in your project. If you are not using a LookAndFeel object to customize your app, you can use

LookAndFeel::getDefaultLookAndFeel().setDefaultSansSerifTypefaceName(fontThatAcceptsChineseChars);

If you want to know the font that accepts Chinese characters, copy paste your chinese word in wordpad and look at the font name in the toolbar (It was automatically change from your defualt font to the font that accepts Chinese Unicode Characters). Hope this helps.