Output Japanese characters with git bash

Viewed 226

I am trying to run a Python script with Japanese characters using git bash.

python test_print_jp.py

The code executed by this script is:

jp_str = u'ァアィイゥ'
print(jp_str)

While I am able to print the Japanese string in a notebook, I am not able to print the same string from git bash terminal.

The error is UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-4: character maps to <undefined>

I have tried changing the locale setting in .bash_profile:

export LANG=ja_JP.UTF-8
export LC_CTYPE=ja_JP.UTF-8
export LC_ALL=ja_JP.UTF-8

I have also tried modifying the string encoding:

str = u'ァアィイゥ'
print(str.encode('shift_jis'))

While there is no more error with the second solution, the output of the original string becomes b'\x83@\x83A\x83B\x83C\x83D'

I have also tried modifying the setting from the interface:

change_locale_setting_with_git_bash_interface

Is there a way to print the original Japanese string from git bash terminal?

Thank you for any inputs and suggestions :)

1 Answers

Thanks to the hints from @furas, I found an answer to the posted problem. It was not sufficient to just change the locale setting for the git bash application, I have to also change the locale setting in Windows as well.

Control Panel -> Region -> Administrative -> Change System Locale

After making this change and restarting windows, the Japanese characters are rendered correctly in the terminal without needing to use encoding.

jp_str = 'ァアィイゥ'
print(jp_str)

The output on terminal will also be: ァアィイゥ

Note: I should also mention that using the beta version of utf-8 encoding for worldwide language support, as shown in the following screenshot is also not sufficient.

beta UNICODE UTF-8 for worldwide language support

Although there is no more UnicodeEncodeError, the original Japanese characters become gibberish on the output.

Related