Changeset 3055

Show
Ignore:
Timestamp:
11/12/06 17:16:49 (17 years ago)
Author:
jerome
Message:

Simplified i18n and l10n stuff by removing code used with Python<2.3 => not supported anymore.
Fixed it for working in CGI scripts as expected (better with Python 2.4 and higher).

Location:
pykota/trunk
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/cgi-bin/dumpykota.cgi

    r3032 r3055  
    3535from pykota.cgifuncs import getLanguagePreference, getCharsetPreference 
    3636 
    37 header = """Content-type: text/html 
    38  
    39 <?xml version="1.0" encoding="%s"?> 
     37header = """Content-type: text/html;charset=%s 
     38 
    4039<html> 
    4140  <head> 
     
    7069  </head> 
    7170  <body> 
     71    <!-- %s %s --> 
    7272    <p> 
    7373      <form action="dumpykota.cgi" method="GET" name="mainform" onsubmit="return checkvalues()"> 
     
    124124        """Displays the dumper interface.""" 
    125125        global header, footer 
    126         print header % (self.getCharset(), _("PyKota Data Dumper"), \ 
     126        print header % (self.charset, _("PyKota Data Dumper"), \ 
     127                        self.language, self.charset, \ 
    127128                        self.config.getLogoLink(), \ 
    128129                        self.config.getLogoURL(), version.__version__, \ 
     
    228229             
    229230if __name__ == "__main__" : 
    230     os.environ["LC_ALL"] = getLanguagePreference() 
    231     admin = PyKotaDumperGUI(lang=os.environ["LC_ALL"], charset=getCharsetPreference()) 
     231    admin = PyKotaDumperGUI(lang=getLanguagePreference(), charset=getCharsetPreference()) 
    232232    admin.deferredInit() 
    233233    admin.form = cgi.FieldStorage() 
  • pykota/trunk/cgi-bin/printquota.cgi

    r3050 r3055  
    3737from pykota.cgifuncs import getLanguagePreference, getCharsetPreference 
    3838 
    39 header = """Content-type: text/html 
    40  
    41 <?xml version="1.0" encoding="%s"?> 
     39header = """Content-type: text/html;charset=%s 
     40 
    4241<html> 
    4342  <head> 
     
    4645  </head> 
    4746  <body> 
     47    <!-- %s %s --> 
    4848    <p> 
    4949      <form action="printquota.cgi" method="POST"> 
     
    9797        """Displays the administrative interface.""" 
    9898        global header, footer 
    99         print header % (self.getCharset(), _("PyKota Reports"), \ 
     99        print header % (self.charset, _("PyKota Reports"), \ 
     100                        self.language, self.charset, \ 
    100101                        self.config.getLogoLink(), \ 
    101102                        self.config.getLogoURL(), version.__version__, \ 
     
    287288             
    288289if __name__ == "__main__" : 
    289     os.environ["LC_ALL"] = getLanguagePreference() 
    290     admin = PyKotaReportGUI(lang=os.environ["LC_ALL"], charset=getCharsetPreference()) 
     290    admin = PyKotaReportGUI(lang=getLanguagePreference(), charset=getCharsetPreference()) 
    291291    admin.deferredInit() 
    292292    admin.form = cgi.FieldStorage() 
  • pykota/trunk/cgi-bin/pykotme.cgi

    r2909 r3055  
    3737     
    3838 
    39 header = """Content-type: text/html 
    40  
    41 <?xml version="1.0" encoding="%s"?> 
     39header = """Content-type: text/html;charset=%s 
     40 
    4241<html> 
    4342  <head> 
     
    4645  </head> 
    4746  <body> 
     47    <!-- %s %s --> 
    4848    <p> 
    4949      <form action="pykotme.cgi" method="POST" enctype="multipart/form-data"> 
     
    9797        """Displays the administrative interface.""" 
    9898        global header, footer 
    99         print header % (self.getCharset(), _("PyKota Quotes"), \ 
     99        print header % (self.charset, _("PyKota Quotes"), \ 
     100                        self.language, self.charset, \ 
    100101                        self.config.getLogoLink(), \ 
    101102                        self.config.getLogoURL(), version.__version__, \ 
     
    187188             
    188189if __name__ == "__main__" : 
    189     os.environ["LC_ALL"] = getLanguagePreference() 
    190     admin = PyKotMeGUI(lang=os.environ["LC_ALL"], charset=getCharsetPreference()) 
     190    admin = PyKotMeGUI(lang=getLanguagePreference(), charset=getCharsetPreference()) 
    191191    admin.deferredInit() 
    192192    admin.form = cgi.FieldStorage() 
  • pykota/trunk/pykota/cgifuncs.py

    r2622 r3055  
    2020# 
    2121 
     22import sys 
    2223import os 
    2324 
     
    2627    languages = os.environ.get("HTTP_ACCEPT_LANGUAGE", "") 
    2728    langs = [l.strip().split(';')[0] for l in languages.split(",")] 
    28     return "%s_%s" % (langs[0], langs[0].upper()) 
     29    sys.stderr.write("Languages preferences : %s\n" % langs) 
     30    return langs[0].replace("-", "_") 
    2931     
    3032def getCharsetPreference() : 
     
    3234    charsets = os.environ.get("HTTP_ACCEPT_CHARSET", "UTF-8") 
    3335    charsets = [l.strip().split(';')[0] for l in charsets.split(",")] 
     36    sys.stderr.write("Charsets preferences : %s\n" % charsets) 
    3437    return charsets[0] 
  • pykota/trunk/pykota/dumper.py

    r3050 r3055  
    283283                              and (typval == "str") : 
    284284                        try : 
    285                             strvalue = unicode(strvalue, self.getCharset()).encode("UTF-8") 
     285                            strvalue = unicode(strvalue, self.charset).encode("UTF-8") 
    286286                        except UnicodeError :     
    287287                            pass 
  • pykota/trunk/pykota/tool.py

    r3050 r3055  
    130130         
    131131        # locale stuff 
    132         self.defaultToCLocale = 0 
    133         try : 
    134             locale.setlocale(locale.LC_ALL, lang) 
     132        try : 
     133            locale.setlocale(locale.LC_ALL, (lang, charset)) 
    135134        except (locale.Error, IOError) : 
    136             # locale.setlocale(locale.LC_ALL, "C") 
    137             self.defaultToCLocale = 1 
    138         try : 
    139             gettext.install("pykota") 
     135            locale.setlocale(locale.LC_ALL, None) 
     136        (self.language, self.charset) = locale.getlocale() 
     137        self.language = self.language or "C" 
     138        self.charset = self.charset or locale.getpreferredencoding() 
     139         
     140        # translation stuff 
     141        try : 
     142            try : 
     143                trans = gettext.translation("pykota", languages=["%s.%s" % (self.language, self.charset)], codeset=self.charset) 
     144            except TypeError : # Python <2.4 
     145                trans = gettext.translation("pykota", languages=["%s.%s" % (self.language, self.charset)]) 
     146            trans.install() 
    140147        except : 
    141148            gettext.NullTranslations().install() 
    142              
    143         # Finds the correct charset 
    144         self.localecharset = sys.getfilesystemencoding() 
    145         if self.localecharset is None : 
    146             try : 
    147                 try : 
    148                     self.localecharset = locale.getpreferredencoding() 
    149                 except AttributeError :     
    150                     try : 
    151                         self.localecharset = locale.getlocale()[1] 
    152                         self.localecharset = self.localecharset or locale.getdefaultlocale()[1] 
    153                     except ValueError :     
    154                         pass        # Unknown locale, strange... 
    155             except locale.Error :             
    156                 pass 
    157         self.charset = charset or self.localecharset or "UTF-8" 
    158149     
    159150        # pykota specific stuff 
     
    189180        self.softwareJobPrice = 0.0 
    190181         
    191         if self.defaultToCLocale : 
    192             self.printInfo("Incorrect locale settings. PyKota falls back to the default locale.", "warn") 
    193182        if environHome : 
    194183            self.printInfo("PYKOTA_HOME environment variable is set. Configuration files were searched in %s" % confdir, "info") 
     
    197186                self.printInfo("The 'pykota' system account is missing. Configuration files were searched in %s instead." % confdir, "warn") 
    198187         
    199         self.logdebug("Charset detected from locale settings : %s" % self.localecharset) 
     188        self.logdebug("Language in use : %s" % self.language) 
    200189        self.logdebug("Charset in use : %s" % self.charset) 
     190         
    201191        arguments = " ".join(['"%s"' % arg for arg in sys.argv]) 
    202192        self.logdebug("Command line arguments : %s" % arguments) 
     
    237227                self.logdebug(_("Regained priviledges. Now running as root.")) 
    238228                self.privdropped = 0 
    239          
    240     def getCharset(self) :     
    241         """Returns the charset in use.""" 
    242         return self.charset 
    243229         
    244230    def UTF8ToUserCharset(self, text) :