I’m writing an app in Python that is based on the Flatpak template provided by the Builder IDE. I’m wondering how can I make currency signs and dates localised?
In my app I present dates and currency signs. Up until yesterday I just hard coded these to the way used in the UK. Even though it’s only me that’s probably going to use the app I thought I’d see how localisation would work. But it’s left me confused.
In the code that shows a currency sign I have…
value = 123.45
self.total.set_text(locale.currency(fvalue, grouping=True))
and although I have my computer setup using en_UK I get $123.45
If I set locale.setlocale(locale.LC_ALL, 'en_GB.UTF-8') in my main.py I get £123.45. If I set it to anything else locale.Error: unsupported locale setting . As far I understand I seem to have more than UK and US generated on my system.
I don’t want to hard set it to anything in main.py but I do want it to use the default on the user’s system.
Am I going about this completely the wrong way? I’m guessing it could be something to do with the Flatpak build but I don’t understand how that works yet… I’m just using what Builder gave me.
Thank you for any help!
EDIT: I should add that I tried C but this gives me ValueError: Currency formatting is not possible using the 'C' locale.
Disclaimer: I’m not an expert on this, and never localized currencies in Python.
Anyway, to set up the locale to the system’s default, you need to use locale.setlocale(locale.LC_ALL, '') (or a variation, but the important part being the empty locale value to ask for the system default).
And this seems to work, at least outside of any sandboxing:
Flatpak splits the localisation data into separate extensions, so that people don’t have to download and install localisation data for languages they don’t use. If you’re running your app from Builder, you should already have the localisation data for your system, though.
GTK does this at initialisation time already.
Note: Python devs using GTK cannot use this function, because it needs to be invoked before initialising GTK, and PyGObject initialises GTK at import time.
Sorry, I feel like I wasted people’s time… I commented out the line in main.py, exported and installed (instead of running from Builder) and it worked. I got the ‘£’ sign.
Running from Builder…
I did try locale.setlocale(locale.LC_ALL, '') but it gave the ‘$’ sign still.
When I get that error the app won’t even start. If I set it to ‘C’ it starts and stays running but I get the error ValueError: Currency formatting is not possible using the 'C' locale.