0

None of previous similar questions worked for me at least those ones that I saw.

I want to set locale to my desired language. But I get this error.

unsupported locale setting

I don't know what am I doing wrong. I am using Windows 10 and Python 3.5.2

This is my code:

import locale

locale.setlocale(locale.LC_ALL, 'ar_BH.utf8')

print("لحرب في سوريا: طائرات الجيش السوري تشن غارات جوية كثيفة جديدة على مناطق المعارضة شرق حلب")

I also tried

locale.setlocale(locale.LC_ALL, 'ar_BH')

But still doesn't work. Thanks in advance.

Hilal
  • 902
  • 2
  • 22
  • 47
  • `setlocale` has no effect on the console codepage nor the encoding of `sys.stdout`. – Eryk Sun Sep 23 '16 at 10:51
  • I couldn't understand. I am using Visual Studio – Hilal Sep 23 '16 at 10:53
  • do you have that locale **installed**? Note that OSes do *not* come with all locales for all languages installed... The error you are getting is telling you: *This locale is **not** installed*. I answered [this question](http://stackoverflow.com/q/14547631/510937) about installing the locale on Ubuntu, you should search on how to do so on Windows. – Bakuriu Sep 23 '16 at 10:55
  • Actually I tried to install it via pip install language-pack-ar_BH but it failed. I couldn't find enough resource in google how to install locale – Hilal Sep 23 '16 at 11:00
  • (1) UTF-8 will never be supported in Windows locales. (2) All versions of the Windows CRT `setlocale` should support 3-letter country and language codes such as `"ara_BHR"`. (3) The new CRT in VS 2015 (used by Python 3.5+) also supports [locale names](https://msdn.microsoft.com/en-us/library/dd373814) such as `"ar-BH"` (notice it uses a hyphen, not an underscore), but Python's `locale.getlocale` is broken for parsing these locale-name strings. – Eryk Sun Sep 23 '16 at 11:02
  • Thanks, I tried ara_BHR and ar_BH. This time there were no exception. But it printed this: ???? ?? ?????: ?????? ????? ?????? ??? ????? ???? ????? ????? ??? ????? ???????? ??? ??? – Hilal Sep 23 '16 at 11:09
  • When you `open` a file on Windows, you have to set `encoding` or else you'll get the default ANSI encoding of your system locale, which the CRT `setlocale` has no effect on. Also, when writing to the console prior to 3.6 you're limited to the console codepage, which you can change via `chcp.com` before starting Python (e.g. chcp 1256). But if you want full Unicode, don't use codepage 65001 (UTF-8). Support for UTF-8 is severely broken in the console (conhost.exe). Instead, install and activate the `win_unicode_console` module to use the console's UTF-16 API. 3.6+ supports this API by default. – Eryk Sun Sep 23 '16 at 11:11
  • Setting the locale in Windows is really to set rules for number and currency formats and collation, not about setting an encoding - except for CRT APIs such as `mbstowcs` and `wcstombs` which are of little practical use for Unicode-aware programs since they don't work with UTF-8, just the deprecated 8-bit codepages such as 1252, etc. When working with the Windows API itself the preferred encoding is always and everywhere UTF-16, i.e. the wide-character API. There's nothing to configure there. – Eryk Sun Sep 23 '16 at 11:21
  • Okay as I understood, I installed win_unicode_console. Then I tried print(string.encode('UTF-16')) but it did not worked. I think you did not mean that I did but I what else should I do – Hilal Sep 23 '16 at 11:39
  • @eryksun: it looks like your 3 comments could be enough to build an (upvotable) answer :-) – Serge Ballesta Sep 23 '16 at 11:40
  • @SergeBallesta, I could add an answer for the specific question about locale names in Windows, but it seems the OP really wants to print Unicode in the console, which is an unrelated problem. – Eryk Sun Sep 23 '16 at 11:47
  • @eryksun: I understand it, but my opinion is that there is a lot a valuable information there - I did not know the 3.6+ particurlarity for example... – Serge Ballesta Sep 23 '16 at 11:50
  • Actually I don't have to print in console but I need to print on cash register. The application will be work on cash register. So to check that my code will work, should I use mbstowcs or wcstombs – Hilal Sep 23 '16 at 12:00
  • @Hilal, you need to know what encoding the cash register expects. Likely it's either a legacy codepage such as 1256 or UTF-8. You'll probably be manually encoding strings to write to the device, e.g. `encoded_string = string.encode('cp1256')`. – Eryk Sun Sep 23 '16 at 12:11
  • Okay I understand thank you so much. Last thing to check it before loading code to cash register, would those api that you said be useful? – Hilal Sep 23 '16 at 12:17

0 Answers0