コード例 #1
0
 def _gettext_instance(self, domain, origin):
     import gettext
     for dir in self._path:
         try:
             return gettext.translation(domain, dir, (self._lang,))
         except IOError:
             continue
     # The MO file was not found.
     msg = "No translation file found: domain=%r, path=%r, lang=%r, origin=%r" % \
           (domain, self._path, self._lang, origin)
     if self._fallback or self._lang == origin:
         if self._lang != origin:
             lcg.log(msg)
         return gettext.NullTranslations()
     else:
         raise IOError(msg)
コード例 #2
0
ファイル: read.py プロジェクト: cerha/lcg
 def _read_file(self, name, ext='txt', comment=None, dir=None, lang=None, fallback_lang=None):
     """Return the text read from the source file."""
     filename = self._input_file(name, ext=ext, lang=lang, dir=dir)
     if lang is not None and not os.path.exists(filename):
         filename2 = self._input_file(name, ext=ext, lang=fallback_lang, dir=dir)
         if os.path.exists(filename2):
             lcg.log("File '%s' not found. Using '%s' instead.", filename, filename2)
             filename = filename2
     self._source_filename = filename
     fh = open(filename, 'rb')
     try:
         lines = fh.readlines()
     finally:
         fh.close()
     encoding = self._encoding
     if lines:
         if lines[0].startswith(self._BOM):
             # Strip the Unicode marker (BOM)
             lines[0] = lines[0][len(self._BOM):]
         match = self._ENCODING_HEADER_MATCHER.match(lines[0])
         if match:
             enc = self._EMACS_CODING_EXTENSION_MATCHER.sub('', match.group(1))
             if sys.version_info[0] > 2:
                 enc = str(enc, 'ascii')
             try:
                 codecs.lookup(str(enc))
             except LookupError:
                 lcg.log("File %s: Unknown encoding '%s' in file header, using default '%s'.",
                         filename, enc, encoding)
             else:
                 encoding = enc
             del lines[0]
         if comment is not None:
             # This is a hack (it breaks line numbering).
             comment_matcher = re.compile(comment)
             lines = [l for l in lines if not comment_matcher.match(l)]
     content = b''.join(lines)
     try:
         return unistr(content, encoding=encoding)
     except UnicodeDecodeError as e:
         raise Exception("File %s: %s" % (filename, e))
コード例 #3
0
ファイル: i18n.py プロジェクト: dusek/lcg
    def _gettext_instance(self, domain, origin):
        import gettext

        for dir in self._path:
            try:
                return gettext.translation(domain, dir, (self._lang,))
            except IOError:
                continue
        # The MO file was not found.
        msg = "No translation file found: domain=%r, path=%r, lang=%r, origin=%r" % (
            domain,
            self._path,
            self._lang,
            origin,
        )
        if self._fallback or self._lang == origin:
            if self._lang != origin:
                lcg.log(msg)
            return gettext.NullTranslations()
        else:
            raise IOError(msg)
コード例 #4
0
ファイル: read.py プロジェクト: dusek/lcg
 def _read_file(self, name, ext='txt', comment=None, dir=None, lang=None, fallback_lang=None):
     """Return the text read from the source file."""
     filename = self._input_file(name, ext=ext, lang=lang, dir=dir)
     if lang is not None and not os.path.exists(filename):
         filename2 = self._input_file(name, ext=ext, lang=fallback_lang, dir=dir)
         if os.path.exists(filename2):
             lcg.log("File '%s' not found. Using '%s' instead.", filename, filename2)
             filename = filename2
     self._source_filename = filename
     fh = open(filename)
     try:
         lines = fh.readlines()
     finally:
         fh.close()
     encoding = self._encoding
     if lines:
         if lines[0].startswith(self._BOM):
             # Strip the Unicode marker (BOM)
             lines[0] = lines[0][len(self._BOM):]
         match = self._ENCODING_HEADER_MATCHER.match(lines[0])
         if match:
             enc = self._EMACS_CODING_EXTENSION_MATCHER.sub('', match.group(1))
             try:
                 codecs.lookup(enc)
             except LookupError:
                 lcg.log("File %s: Unknown encoding '%s' in file header, using default '%s'.",
                         filename, enc, encoding)
             else:
                 encoding = enc
             del lines[0]
         if comment is not None:
             # This is a hack (it breaks line numbering).
             comment_matcher = re.compile(comment)
             lines = [l for l in lines if not comment_matcher.match(l)]
     content = ''.join(lines)
     try:
         return unicode(content, encoding=encoding)
     except UnicodeDecodeError as e:
         raise Exception("File %s: %s" % (filename, e))