Package translate :: Package convert :: Module ical2po |
[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.ical2po

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2007 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  """convert iCal files to Gettext PO localization files"""
     23
     24  import sys
     25
     26  from translate.storage import po
     27  from translate.storage import ical
     28
     29
     30 -class ical2po:
     31      """convert a iCal file to a .po file for handling the translation..."""
     32
     33 -    def convert_store(self, input_store, duplicatestyle="msgctxt"):
     34          """converts a iCal file to a .po file..."""
     35          output_store = po.pofile()
     36          output_header = output_store.init_headers(charset="UTF-8", encoding="8bit")
     37          output_header.addnote("extracted from %s" % input_store.filename, "developer")
     38          for input_unit in input_store.units:
     39              output_unit = self.convert_unit(input_unit, "developer")
     40              if output_unit is not None:
     41                  output_store.addunit(output_unit)
     42          output_store.removeduplicates(duplicatestyle)
     43          return output_store
     44
     45 -    def merge_store(self, template_store, input_store, blankmsgstr=False, duplicatestyle="msgctxt"):
     46          """converts two iCal files to a .po file..."""
     47          output_store = po.pofile()
     48          output_header = output_store.init_headers(charset="UTF-8", encoding="8bit")
     49          output_header.addnote("extracted from %s, %s" % (template_store.filename, input_store.filename), "developer")
     50
     51          input_store.makeindex()
     52          for template_unit in template_store.units:
     53              origpo = self.convert_unit(template_unit, "developer")
     54              # try and find a translation of the same name...
     55              template_unit_name = "".join(template_unit.getlocations())
     56              if template_unit_name in input_store.locationindex:
     57                  translatedini = input_store.locationindex[template_unit_name]
     58                  translatedpo = self.convert_unit(translatedini, "translator")
     59              else:
     60                  translatedpo = None
     61              # if we have a valid po unit, get the translation and add it...
     62              if origpo is not None:
     63                  if translatedpo is not None and not blankmsgstr:
     64                      origpo.target = translatedpo.source
     65                  output_store.addunit(origpo)
     66              elif translatedpo is not None:
     67                  print >> sys.stderr, "error converting original iCal definition %s" % origpo.name
     68          output_store.removeduplicates(duplicatestyle)
     69          return output_store
     70
     71 -    def convert_unit(self, input_unit, commenttype):
     72          """Converts a .ini unit to a .po unit. Returns None if empty
     73          or not for translation."""
     74          if input_unit is None:
     75              return None
     76          # escape unicode
     77          output_unit = po.pounit(encoding="UTF-8")
     78          output_unit.addlocation("".join(input_unit.getlocations()))
     79          output_unit.addnote(input_unit.getnotes("developer"), "developer")
     80          output_unit.source = input_unit.source
     81          output_unit.target = ""
     82          return output_unit
     83
     84
     85 -def convertical(input_file, output_file, template_file, pot=False, duplicatestyle="msgctxt"):
     86      """Reads in L{input_file} using iCal, converts using L{ical2po}, writes to L{output_file}"""
     87      input_store = ical.icalfile(input_file)
     88      convertor = ical2po()
     89      if template_file is None:
     90          output_store = convertor.convert_store(input_store, duplicatestyle=duplicatestyle)
     91      else:
     92          template_store = ical.icalfile(template_file)
     93          output_store = convertor.merge_store(template_store, input_store, blankmsgstr=pot, duplicatestyle=duplicatestyle)
     94      if output_store.isempty():
     95          return 0
     96      output_file.write(str(output_store))
     97      return 1
     98
     99
    100 -def main(argv=None):
    101      from translate.convert import convert
    102      formats = {"ics": ("po", convertical), ("ics", "ics"): ("po", convertical)}
    103      parser = convert.ConvertOptionParser(formats, usetemplates=True, usepots=True, description=__doc__)
    104      parser.add_duplicates_option()
    105      parser.passthrough.append("pot")
    106      parser.run(argv)
    107
    108
    109  if __name__ == '__main__':
    110      main()
    111