translate.convert.symb2po

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

# Source Code for Module translate.convert.symb2po

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2008-2009 Zuza Software Foundation
      5  #
      6  # This file is part of the Translate Toolkit.
      7  #
      8  # This program 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  # This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
     20
     21  """Convert Symbian localisation files to Gettext PO localization files."""
     22
     23  from translate.storage import factory
     24  from translate.storage.pypo import extractpoline
     25  from translate.storage.symbian import *
     26
     27 -def read_header_items(ps):
     28      match = read_while(ps, header_item_or_end_re.match, lambda match: match is None)
     29      if match.groupdict()['end_comment'] is not None:
     30          return {}
     31
     32      results = {}
     33      while match:
     34          match_chunks = match.groupdict()
     35          ps.read_line()
     36          results[match_chunks['key']] = match_chunks['value']
     37          match = header_item_re.match(ps.current_line)
     38
     39      match = read_while(ps, identity, lambda line: not line.startswith('*/'))
     40      ps.read_line()
     41      return results
     42
     43 -def parse(ps):
     44      header = read_header_items(ps)
     45      units = []
     46      try:
     47          while True:
     48              eat_whitespace(ps)
     49              skip_no_translate(ps)
     50              match = string_entry_re.match(ps.current_line)
     51              if match is not None:
     52                  units.append((match.groupdict()['id'], extractpoline(match.groupdict()['str'])))
     53              ps.read_line()
     54      except StopIteration:
     55          pass
     56      return header, units
     57
     58 -def read_symbian(f):
     59      lines = list(f)
     60      charset = read_charset(lines)
     61      return parse(ParseState(iter(lines), charset))
     62
     63 -def get_template_dict(template_file):
     64      if template_file is not None:
     65          template_header, template_units = read_symbian(template_file)
     66          return template_header, dict(template_units)
     67      else:
     68          return {}, {}
     69
     70 -def build_output(units, template_header, template_dict):
     71      output_store = factory.classes['po']()
     72      ignore = set(['r_string_languagegroup_name'])
     73      header_entries = {
     74          'Last-Translator': template_header.get('Author', ''),
     75          'Language-Team': template_dict.get('r_string_languagegroup_name', ''),
     76          'Content-Transfer-Encoding': '8bit',
     77          'Content-Type': 'text/plain; charset=UTF-8',
     78          }
     79      output_store.updateheader(add=True, **header_entries)
     80      for id, source in units:
     81          if id in ignore:
     82              continue
     83          unit = output_store.UnitClass(source)
     84          unit.target = template_dict.get(id, '')
     85          unit.addlocation(id)
     86          output_store.addunit(unit)
     87      return output_store
     88
     89 -def convert_symbian(input_file, output_file, template_file, pot=False, duplicatestyle="msgctxt"):
     90      header, units = read_symbian(input_file)
     91      template_header, template_dict = get_template_dict(template_file)
     92      output_store = build_output(units, template_header, template_dict)
     93
     94      if output_store.isempty():
     95          return 0
     96      else:
     97          output_file.write(str(output_store))
     98          return 1
     99
    100 -def main(argv=None):
    101      from translate.convert import convert
    102      formats = {"r01": ("po", convert_symbian)}
    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  if __name__ == '__main__':
    109      main()
    110