10

I have a program written in C using ncurses. It let user input and display it. It does not display correctly if user input utf8 chars.

I saved the chars user inputed to a file. And I cat this file directly in Shell, it display correctly.

I searched stackoverflow and google, and tried several methods, such as link with ncursesw, display incorrectly.

And I ldd /usr/bin/screen:libncurses.so.5 => /usr/lib64/libncurses.so.5

screen can display what user input correctly.

How to make ncurses display UTF-8 chars correctly ?

What is the general way to display UTF-8 chars in C using ncurses?

everbox
  • 1,189
  • 3
  • 16
  • 25

2 Answers2

17

You need to have called setlocale(LC_CTYPE, ""); (with a UTF-8 based locale configured) before initializing ncurses. You also need to make sure your ncurses is actually built with wide char support ("ncursesw") but on modern distros this is the default/only build.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • 1
    Is there an easy way to tell if ncurses was built with wide char support? – KyleL Jul 29 '13 at 19:22
  • 2
    @KdawgUD On Ubuntu, you can install the library with sudo apt-get install libncursesw5-dev. Use the ncursesw library when building, and include the ncursesw/curses.h library – voidraen Apr 27 '14 at 05:49
  • As the maintainer [points out](https://stackoverflow.com/questions/50767405/why-wont-ncurses-output-all-the-foreign-characters-of-an-utf8-string#comment88545254_50767529) it might be better to use `setlocale(LC_ALL, "");` – Stefan Schmidt Sep 05 '22 at 17:06
  • @StefanSchmidt: I specifically **don't recommend** `setlocale(LC_ALL,"");` because it breaks parsing/emitting of floating point numbers and possibly other things. If you do it, you should follow up with `setlocale(LC_NUMERIC,"C");` to undo that part. – R.. GitHub STOP HELPING ICE Sep 05 '22 at 21:43
0
#need these as well on top of installation and locate setting
#at least check locale
locale

locale-gen en_US.UTF-8
#vim ~/.bashrc # add 3 lines once ok and fix the profile
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export NCURSES_NO_UTF8_ACS=1
KWC
  • 121
  • 8