Package translate :: Package convert :: Module ini2po

[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.ini2po

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