Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #86721 > unrolled thread

Pythonic locale

Started byAlbert-Jan Roskam <fomcl@yahoo.com>
First post2015-03-02 03:42 -0800
Last post2015-03-02 03:42 -0800
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  Pythonic locale Albert-Jan Roskam <fomcl@yahoo.com> - 2015-03-02 03:42 -0800

#86721 — Pythonic locale

FromAlbert-Jan Roskam <fomcl@yahoo.com>
Date2015-03-02 03:42 -0800
SubjectPythonic locale
Message-ID<mailman.31.1425296685.13471.python-list@python.org>
Hi,

The Python locale standard libraries has some oddities and (long-standing) bugs.
Example oddity: SETlocale *returns* a locale; getlocale output cannot always be consumed by setlocale. Example bug: resetlocale fails in Windows. What is your opinion about the work-around code below? 


import sys
import os
import locale as locale_

locale_.setlocale(locale_.LC_ALL, "")
    
class PythonicLocale(object):
     
    LC_ALL = locale_.LC_ALL 
    LC_CTYPE = locale_.LC_CTYPE
    
    def __init__(self, failsafe=False):
        self.failsafe = failsafe
    
    @property
    def locale(self):
        """Partial wrapper for locale in standard library"""
        # LANG and LC_ALL sometimes not set
        if not sys.platform.startswith("win"):
            if locale_.getlocale()[0] is None and self.failsafe:
                os.environ["LANG"] = "en_US"
                os.environ["LC_ALL"] = "en_US.UTF-8"
        # getlocale output cannot be consumed by setlocale
        return locale_.setlocale(locale_.LC_ALL)

    @locale.setter
    def locale(self, category_and_locale_tuple):
        locale_.setlocale(*category_and_locale_tuple)

    @locale.deleter
    def locale(self):
        if sys.platform.startswith("win"):
            # resetlocale() is broken in Windows
            locale_.setlocale(locale_.LC_ALL, "")
        else: 
            locale_.resetlocale()

    def getdefaultlocale(self):
        return locale_.getdefaultlocale()


if __name__ == "__main__":
    locale = PythonicLocale() 
    # getter
    print locale.locale
    # setter
    locale.locale = (locale.LC_ALL, "german")
    print locale.locale
    # deleter
    del locale.locale
    # check if deleter worked
    print locale.locale

Thanks!

Regards,

Albert-Jan



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a 

fresh water system, and public health, what have the Romans ever done for us?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web