Package translate :: Package convert :: Module po2symb

[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.po2symb

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2008 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 Gettext PO localization files to Symbian translation files."""
     22
     23  from translate.storage import factory
     24  from translate.storage.pypo import po_escape_map
     25  from translate.storage.symbian import *
     26
     27
     28 -def escape(text):
     29      for key, val in po_escape_map.iteritems():
     30          text = text.replace(key, val)
     31      return '"%s"' % text
     32
     33
     34 -def replace_header_items(ps, replacments):
     35      match = read_while(ps, header_item_or_end_re.match, lambda match: match is None)
     36      while not ps.current_line.startswith('*/'):
     37          match = header_item_re.match(ps.current_line)
     38          if match is not None:
     39              key = match.groupdict()['key']
     40              if key in replacments:
     41                  ps.current_line = match.expand('\g<key>\g<space>%s\n' % replacments[key])
     42          ps.read_line()
     43
     44
     45 -def parse(ps, header_replacements, body_replacements):
     46      replace_header_items(ps, header_replacements)
     47      try:
     48          while True:
     49              eat_whitespace(ps)
     50              skip_no_translate(ps)
     51              match = string_entry_re.match(ps.current_line)
     52              if match is not None:
     53                  key = match.groupdict()['id']
     54                  if key in body_replacements:
     55                      value = body_replacements[key].target or body_replacements[key].source
     56                      ps.current_line = match.expand(u'\g<start>\g<id>\g<space>%s\n' % escape(value))
     57              ps.read_line()
     58      except StopIteration:
     59          pass
     60
     61
     62 -def line_saver(charset):
     63      result = []
     64
     65      def save_line(line):
     66          result.append(line.encode(charset))
     67      return result, save_line
     68
     69
     70 -def write_symbian(f, header_replacements, body_replacements):
     71      lines = list(f)
     72      charset = read_charset(lines)
     73      result, save_line = line_saver(charset)
     74      parse(ParseState(iter(lines), charset, save_line), header_replacements, body_replacements)
     75      return result
     76
     77
     78 -def build_location_index(store):
     79      po_header = store.parseheader()
     80      index = {}
     81      for unit in store.units:
     82          for location in unit.getlocations():
     83              index[location] = unit
     84      index['r_string_languagegroup_name'] = store.UnitClass(po_header['Language-Team'])
     85      return index
     86
     87
     88 -def build_header_index(store):
     89      po_header = store.parseheader()
     90      return {'Author': po_header['Last-Translator']}
     91
     92
     93 -def convert_symbian(input_file, output_file, template_file, pot=False, duplicatestyle="msgctxt"):
     94      store = factory.getobject(input_file)
     95      location_index = build_location_index(store)
     96      header_index = build_header_index(store)
     97      output = write_symbian(template_file, header_index, location_index)
     98      for line in output:
     99          output_file.write(line)
    100      return 1
    101
    102
    103 -def main(argv=None):
    104      from translate.convert import convert
    105      formats = {"po": ("r0", convert_symbian)}
    106      parser = convert.ConvertOptionParser(formats, usetemplates=True, usepots=True, description=__doc__)
    107      parser.add_duplicates_option()
    108      parser.passthrough.append("pot")
    109      parser.run(argv)
    110
    111
    112  if __name__ == '__main__':
    113      main()
    114