Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
decimal point or comma
#9
Here are my tries to work with locale and gettext.
In other words: It's pain in the a....


from pathlib import Path
import locale
from locale import (
    LC_ALL,
    LC_NUMERIC,
    setlocale,
    getlocale,
    getdefaultlocale,
    currency
    )
from locale import format_string as lformat
import gettext

def translations(domain, localedir):
    localedir = Path(localedir)
    result = {}
    for trans in localedir.glob(f'**/{domain}.mo'):
        lang = trans.parent.parent.name
        print(f'Found translation {trans}. Language: {lang}')
        try:
            result[lang] = gettext.translation(domain, localedir, [lang])
        except Exception as e:
            print(e)
    print()
    return result

trans = translations('messages', 'translations')


def current_setting():
    for symbol in dir(locale):
        if symbol.startswith('LC_') and symbol != 'LC_ALL':
            tp = getattr(locale, symbol)
            print(f'{symbol:<20}{locale.getlocale(tp)}')
    print()

def test():
    current_setting()
    try:
        print(currency(val))
    except ValueError as e:
        print(e)
    print(lformat('%.2f', val, grouping=True))
    print(_('Hello World!'))
    print()

val = 1337.42
defaultlocale = getdefaultlocale()
print('Defaultlocale is', defaultlocale)
print()

setlocale(LC_ALL, 'en_US.utf8')
trans['en_US'].install()
test()

setlocale(LC_ALL, 'en_US.utf8')
print('LC_ALL set to en_US.utf8')
trans['en_GB'].install()
test()

trans['de_DE'].install()
ret = setlocale(LC_ALL, defaultlocale)
print('LC_ALL set to', ret)
test()
Output
Output:
Found translation translations/en_US/LC_MESSAGES/messages.mo. Language: en_US Found translation translations/en_GB/LC_MESSAGES/messages.mo. Language: en_GB Found translation translations/de_DE/LC_MESSAGES/messages.mo. Language: de_DE Defaultlocale is ('de_DE', 'UTF-8') LC_COLLATE ('en_US', 'UTF-8') LC_CTYPE ('en_US', 'UTF-8') LC_MESSAGES ('en_US', 'UTF-8') LC_MONETARY ('en_US', 'UTF-8') LC_NUMERIC ('en_US', 'UTF-8') LC_TIME ('en_US', 'UTF-8') $1337.42 1,337.42 Fuck You! LC_ALL set to en_US.utf8 LC_COLLATE ('en_US', 'UTF-8') LC_CTYPE ('en_US', 'UTF-8') LC_MESSAGES ('en_US', 'UTF-8') LC_MONETARY ('en_US', 'UTF-8') LC_NUMERIC ('en_US', 'UTF-8') LC_TIME ('en_US', 'UTF-8') $1337.42 1,337.42 Hello World! LC_ALL set to de_DE.UTF-8 LC_COLLATE ('de_DE', 'UTF-8') LC_CTYPE ('de_DE', 'UTF-8') LC_MESSAGES ('de_DE', 'UTF-8') LC_MONETARY ('de_DE', 'UTF-8') LC_NUMERIC ('de_DE', 'UTF-8') LC_TIME ('de_DE', 'UTF-8') 1337,42 € 1.337,42 Hallo Welt!
I could not test with locale en_GB, because I haven't installed it on my system.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
decimal point or comma - by Skaperen - Mar-12-2019, 07:45 PM
RE: decimal point or comma - by hshivaraj - Mar-12-2019, 10:37 PM
RE: decimal point or comma - by Skaperen - Mar-15-2019, 06:52 PM
RE: decimal point or comma - by snippsat - Mar-15-2019, 07:24 PM
RE: decimal point or comma - by DeaD_EyE - Mar-15-2019, 09:24 PM
RE: decimal point or comma - by Skaperen - Mar-15-2019, 11:10 PM
RE: decimal point or comma - by farhan275 - Mar-15-2019, 11:40 PM
RE: decimal point or comma - by Skaperen - Mar-16-2019, 06:57 PM
RE: decimal point or comma - by DeaD_EyE - Mar-17-2019, 06:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,087 Aug-19-2022, 08:07 PM
Last Post: Winfried
  How to format Excel column with comma? dee 0 1,337 Jun-13-2022, 10:11 PM
Last Post: dee
  decimal comma DPaul 9 2,216 Feb-22-2022, 12:25 PM
Last Post: DeaD_EyE
  Adding a comma in the resulting value stsxbel 6 2,591 May-22-2021, 09:24 PM
Last Post: stsxbel
  How to instantly add quotation marks and comma for parameters? cheers100 4 7,919 Oct-22-2020, 12:51 PM
Last Post: cheers100
  Printing digits after the decimal point one by one uberman321 1 1,702 Oct-20-2020, 08:10 AM
Last Post: bowlofred
  print scripts example includes comma that seems to serve no purpose flour_power_33 5 2,741 Sep-02-2020, 03:32 AM
Last Post: flour_power_33
  Grabbing comma separed values from SQLite and putting them in a list PythonNPC 8 3,931 Apr-10-2020, 02:39 PM
Last Post: buran
  connecting the first point to the last point Matplotlib omar_mohsen 0 4,525 Jan-15-2020, 01:23 PM
Last Post: omar_mohsen
  Phyton code to load a comma separated csv file in to a dict and then in to a dB mrsenorchuck 2 2,618 Nov-29-2019, 10:59 AM
Last Post: mrsenorchuck

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020