2

my code has:

def sep(num, lang='en', none_is_zero=False):
    if num is None:
        if none_is_zero is False:
            return None
        else:
            return 0
    try:
        locale.setlocale(locale.LC_ALL, lang)
    except locale.Error:
        locale.setlocale(locale.LC_ALL, 'en')
    return locale.format("%d", num, grouping=True)

since i was getting that error i tried to run this to test:

def test(bot, job):
    print(sep(12343, 'it'))
    print(sep(11425, 'en'))
    print(sep(1234235, None))
    print(sep(42345, ''))
    print(sep(2353636, 'gergreh'))
    print(sep(None, None))

and i get this error:

Traceback (most recent call last):
  File "/home/**/utils.py", line 192, in sep
    locale.setlocale(locale.LC_ALL, lang)
  File "/usr/lib/python3.5/locale.py", line 594, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/telegram/ext/jobqueue.py", line 286, in tick
    job.run(self.bot)
  File "/usr/local/lib/python3.5/dist-packages/telegram/ext/jobqueue.py", line 411, in run
    self.callback(bot, self)
  File "/home/**/utils.py", line 199, in test
    print(sep(12343, 'it'))
  File "/home/**/utils.py", line 194, in sep
    locale.setlocale(locale.LC_ALL, 'en')
  File "/usr/lib/python3.5/locale.py", line 594, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

but i don't get any error on windows and the function works as expected. The problem is only on my ubuntu vps. finding some answers on stackoverflow i saw someone suggesting to use 'en_US.UTF-8' and i tried and it works even on linux. But 'en.UTF-8' still doesn't work. i could append UTF-8 if it's needed but the api i use sometimes returns as lang_code only 'en', 'it', 'es'.

how can i get rid of this problem? on windows i don't have problems and it seems like the problem is just my linux vps with it, en etc

91DarioDev
  • 1,612
  • 1
  • 14
  • 31
  • There's some relevant info [here](https://stackoverflow.com/a/39506697/4014959); also see the other answers (and their comments) on that page. – PM 2Ring Oct 16 '17 at 11:18
  • thank you. as far as i read there i should solve doing `locale.setlocale(locale.LC_ALL, locale.locale_alias[lang])` am i right? @PM2Ring – 91DarioDev Oct 16 '17 at 11:24
  • I linked to the answer that suggests using the 3rd-party `babel` module. Other info on that page suggests that `locale.locale_alias` doesn't work correctly on Windows. – PM 2Ring Oct 16 '17 at 11:27
  • i solved using babel functions to group digits. http://babel.pocoo.org/en/latest/api/numbers.html thank you – 91DarioDev Oct 16 '17 at 14:04
  • Possible duplicate of [Python locale error: unsupported locale setting](https://stackoverflow.com/questions/14547631/python-locale-error-unsupported-locale-setting) – user Nov 29 '17 at 17:50

1 Answers1

1
export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
sudo dpkg-reconfigure locales