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