Package translate :: Package convert :: Module sub2po

[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.sub2po

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