translate.storage.mozilla_lang

Package translate :: Package storage :: Module mozilla_lang |
[hide private]
[frames] | no frames]

# Source Code for Module translate.storage.mozilla_lang

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2008, 2011 Zuza Software Foundation
     5  #
     6  # This file is part of translate.
     7  #
     8  # translate is free software; you can redistribute it and/or modify
     9  # it under the terms of the GNU General Public License as published by
    10  # the Free Software Foundation; either version 2 of the License, or
    11  # (at your option) any later version.
    12  #
    13  # translate is distributed in the hope that it will be useful,
    14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  # GNU General Public License for more details.
    17  #
    18  # You should have received a copy of the GNU General Public License
    19  # along with translate; if not, write to the Free Software
    20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    21
    22  # Original Author: Dan Schafer <dschafer@mozilla.com>
    23  # Date: 10 Jun 2008
    24
    25  """A class to manage Mozilla .lang files."""
    26
    27  from translate.storage import base
    28  from translate.storage import txt
    29
    30
    31 -class LangUnit(base.TranslationUnit):
    32      """This is just a normal unit with a weird string output"""
    33
    34 -    def __init__(self, source=None):
    35          self.locations = []
    36          base.TranslationUnit.__init__(self, source)
    37
    38 -    def __str__(self):
    39          return u";%s\n%s" % (self.source, self.target)
    40
    41 -    def getlocations(self):
    42          return self.locations
    43
    44 -    def addlocation(self, location):
    45          self.locations.append(location)
    46
    47
    48 -class LangStore(txt.TxtFile):
    49      """We extend TxtFile, since that has a lot of useful stuff for encoding"""
    50      UnitClass = LangUnit
    51
    52 -    def parse(self, lines):
    53          #Have we just seen a ';' line, and so are ready for a translation
    54          readyTrans = False
    55
    56          if not isinstance(lines, list):
    57              lines = lines.split("\n")
    58          for lineoffset, line in enumerate(lines):
    59              line = line.rstrip("\n").rstrip("\r")
    60
    61              if len(line) == 0: #Skip blank lines
    62                  continue
    63
    64              if readyTrans: #If we are expecting a translation, set the target
    65                  u.target = line
    66                  readyTrans = False #We already have our translation
    67                  continue
    68
    69              if line.startswith(';'):
    70                  u = self.addsourceunit(line[1:])
    71                  readyTrans = True # Now expecting a translation on the next line
    72                  u.addlocation("%s:%d" % (self.filename, lineoffset + 1))
    73
    74 -    def __str__(self):
    75          return u"\n\n".join([unicode(unit) for unit in self.units]).encode('utf-8')
    76