translate.convert.po2symb

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