def updateCachedCatPaths(self): # should we recreate this each time? self.cats = {} self.iconv = None for domain in self.domains + [None]: for mypath in self.paths: if not domain: path = 'po/%s.mo' else: path = '%s/%s/LC_MESSAGES/%s.mo' % ( mypath, '%s', domain, ) mofile = None for lang in self.langs: try: file_path = path % (lang, ) f = open(file_path) buf = f.read(2) f.close() if buf == "\037\213": mofile = gzip.open(file_path) else: mofile = open(file_path) except IOError: pass if mofile: break if mofile is None: continue catalog = gettext.GNUTranslations(mofile) try: theiconv = iconv.open(self.codeset, catalog.charset()) except Exception, e: sys.stderr.write( 'unable to translate from %s to utf-8: %s\n' % (catalog.charset(), e)) theiconv = iconv.open(self.codeset, 'UTF-8') self.cats[domain] = (catalog, theiconv) if self.iconv is None: self.iconv = theiconv break
def __init__(self): self.encoder = iconv.open(self.codeset, unicodename) self.decoder = iconv.open(unicodename, self.codeset)
def __init__(self): self.encoder = iconv.open(self.codeset,unicodename) self.decoder = iconv.open(unicodename,self.codeset)
def test_basic(self): s = iconv.open("latin1", "utf-8") r = s.iconv(b"Hallo") self.assertEqual(r, b"Hallo")
def test_with_length(self): s = iconv.open("utf-16le", "utf-8") r = s.iconv(b"Hallo", 11) self.assertEqual(r, "Hallo".encode("utf-16le"))
class i18n: def __init__(self, langs=None, conversion=0, domain=None, paths=None): # FIXME: legacy method names self.utf8 = self.convert self.setunicode = self.setconversion self.getunicode = self.getconversion # do we convert to codeset or not? self.conversion = conversion # codeset to convert to self.codeset = 'utf-8' # dictionary of catalogs. keyed by domain, val is (cat, iconv) self.cats = {} # list of domains in search order. setting textdomain adds # to the front of the list self.domains = [] if langs: self.langs = langs else: try: self.langs = getDefaultLangs() except: self.langs = ['C'] if paths is None: self.paths = ["/usr/share/locale"] else: self.paths = paths self.setDomain(domain) # add a dir to our search path for po files def addPoPath(self, path): if path not in self.paths: self.paths.insert(0, path) self.updateCachedCatPaths() def setDomain(self, domain): # add domain to the front of our domain list if type(domain) == type([]): domain.reverse() for dom in domain: if dom in self.domains: self.domains.pop(self.domains.index(dom)) self.domains.insert(0, dom) else: if domain in self.domains: self.domains.pop(self.domains.index(domain)) self.domains.insert(0, domain) # let's pop None out of the list if its there and special case it if None in self.domains: self.domains.pop(self.domains.index(None)) self.updateCachedCatPaths() def updateCachedCatPaths(self): # should we recreate this each time? self.cats = {} self.iconv = None for domain in self.domains + [None]: for mypath in self.paths: if not domain: path = 'po/%s.mo' else: path = '%s/%s/LC_MESSAGES/%s.mo' % ( mypath, '%s', domain, ) mofile = None for lang in self.langs: try: file_path = path % (lang, ) f = open(file_path) buf = f.read(2) f.close() if buf == "\037\213": mofile = gzip.open(file_path) else: mofile = open(file_path) except IOError: pass if mofile: break if mofile is None: continue catalog = gettext.GNUTranslations(mofile) try: theiconv = iconv.open(self.codeset, catalog.charset()) except Exception, e: sys.stderr.write( 'unable to translate from %s to utf-8: %s\n' % (catalog.charset(), e)) theiconv = iconv.open(self.codeset, 'UTF-8') self.cats[domain] = (catalog, theiconv) if self.iconv is None: self.iconv = theiconv break # now let's put None at the beginning of the list as it corresponds # to the "current" local po files for testing self.domains.insert(0, None) if len(self.cats) == 0: encoding = locale.nl_langinfo(locale.CODESET) # sys.stderr.write("WARNING: Unable to find catalog, using %s for codeset, %s for encoding\n" %(self.codeset, encoding)) try: self.iconv = iconv.open(self.codeset, encoding) except: sys.stderr.write( "FAILED to create iconv with codeset %s and encoding %s\n" % (self.codeset, encoding)) return