israel-dryer / ttkbootstrap

A supercharged theme extension for tkinter that enables on-demand modern flat style themes inspired by Bootstrap.
MIT License
1.98k stars 393 forks source link

Unsupported locale setting on module import for Norwegian locale #505

Open Lore-Gaviano opened 1 year ago

Lore-Gaviano commented 1 year ago

Desktop (please complete the following information):

ttkbootstrap Version = 1.10.1 OS: Windows 11 Pro

Describe the bug

image

When working on a PC with Norwegian locale, DatePickerDialog throws a "local.Error", failing the import of ttkboostrap. Norwegian locale contains non-ASCII characters "å", which is not accepted in setting the locale in line 566 of \ttkbootstrap\dialogs\dialogs.py".

To Reproduce

Set computer region to Norway and import ttkbootstrap.

Expected behavior

No response

Screenshots

No response

Additional context

No response

ismaelink commented 1 year ago

I'm not sure if my response will help you, but I've had a similar error in the past that had the same root cause as your error, which, in this case, is located in the 'dialogs.py' file at line 566. I believe that if you adapt the solution from that issue to yours, it may resolve your problem.

Based on the given link, do something like this:

set_locale_to_posix()
# Here would be all the code that uses formatting according to your preferred locale
# ....
set_locale_to_system_default()

I recommend creating functions that handle this entire process automatically, avoiding code repetition. Try doing this and then provide some feedback. Good luck! :smile_cat:

Lore-Gaviano commented 1 year ago

Thank you for your answer, but I believe the solution may not be applicable, because the error occurs exactly the same.

import locale 
def set_locale_to_posix():
    locale.setlocale(locale.LC_ALL, "C")

def set_locale_to_system_default():
    # This part was taken from line 566 of the "dialogs.py" file present in the "ttkbootstrap" library.
    locale.setlocale(locale.LC_ALL, locale.setlocale(locale.LC_TIME, "nb_NO"))

set_locale_to_posix()
import ttkbootstrap as ttk
set_locale_to_system_default()

The problem comes from the fact that locale.setlocale(locale.LC_TIME, "")) in line 566 returns "('Norwegian Bokmål_Norway', '1252')" and the letter "å" can't be passed to locale.setlocale(locale.LC_ALL, "Norwegian Bokmål_Norway").

I have been looking into changing the default locale to something of my choice from python, to import ttkbootstrap and reset it to Norwegian at the end of the script.

I don't really have any operation to do with a particular date format, I just need to import the model, but that is when the error is raised.

Neoakay005 commented 1 year ago

Desktop (please complete the following information):

ttkbootstrap Version = 1.10.1 OS: Windows 11 Pro

Describe the bug

image

When working on a PC with Norwegian locale, DatePickerDialog throws a "local.Error", failing the import of ttkboostrap. Norwegian locale contains non-ASCII characters "å", which is not accepted in setting the locale in line 566 of \ttkbootstrap\dialogs\dialogs.py".

To Reproduce

Set computer region to Norway and import ttkbootstrap.

Expected behavior

No response

Screenshots

No response

Additional context

No response

I tried and tried and figure out, solve it just in a simple way:

  1. Just open up the dialogs.py in folder: "C:\Users\"user"\AppData\Local\Programs\Python\Python312\Lib\site-packages\ttkbootstrap\dialogs\dialogs.py", line 566

  2. Overwrite this line locale.setlocale(locale.LC_ALL, locale.setlocale(locale.LC_TIME, "")) with "tr_TR.utf8" (but you can overwrite as default with "en_EN.utf8") locale.setlocale(locale.LC_ALL, locale.setlocale(locale.LC_TIME, "tr_TR.utf8"))

and everything runs without any problem.

Lore-Gaviano commented 1 year ago

@Neoakay005's solution works. Thank you very much. However in case I wanna share my project, the receivers would need to execute the same changes in their own local package. This could potentially represent an annoyance. Perhaps it could be worth making a similar change in the repo too. Thanks!

rdbende commented 1 year ago

Perhaps it could be worth making a similar change in the repo too.

Then please reopen this issue. Although there isn't much activity on the project lately, it's better to leave unresolved issues open.

mmebsout commented 10 months ago

I got the same here and I'm not Norwegian

ttkbootstrap version 1.10.1

(venv) my_project % locale
LANG=""
LC_COLLATE="C"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL=

test.py

import ttkbootstrap as tb

output :

Traceback (most recent call last):
  File "test.py", line 11, in <module>
    import ttkbootstrap as tb
  File "venv/lib/python3.10/site-packages/ttkbootstrap/__init__.py", line 3, in <module>
    from ttkbootstrap.widgets import *
  File "venv/lib/python3.10/site-packages/ttkbootstrap/widgets.py", line 14, in <module>
    from ttkbootstrap.dialogs import Querybox
  File "venv/lib/python3.10/site-packages/ttkbootstrap/dialogs/__init__.py", line 1, in <module>
    from ttkbootstrap.dialogs.dialogs import *
  File "venv/lib/python3.10/site-packages/ttkbootstrap/dialogs/dialogs.py", line 536, in <module>
    class DatePickerDialog:
  File "venv/lib/python3.10/site-packages/ttkbootstrap/dialogs/dialogs.py", line 566, in DatePickerDialog
    locale.setlocale(locale.LC_ALL, locale.setlocale(locale.LC_TIME, ""))
  File ".pyenv/versions/3.10.3/lib/python3.10/locale.py", line 620, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

Platform: Apple M1 - Ventura - 13.6.1 (22G313)

dphdmn commented 6 months ago
import locale 
locale.setlocale(locale.LC_ALL, "C")

Running this at the start of my App helped me fixing "???" characters instead of months (It was probably trying to load Cyrillic)

christianhals commented 5 months ago

Did anyone find a solutions for this? I'm also on nb_NO as all potential users of the app.

Aleksander889 commented 4 months ago

For Norwegians do as Neoakay005 suggested. But instead of changing the line to: locale.setlocale(locale.LC_ALL, locale.setlocale(locale.LC_TIME, "tr_TR.utf8"))

change it to this: locale.setlocale(locale.LC_ALL, locale.setlocale(locale.LC_TIME, "no_NO"))

At least that worked for me.