A
download po2xliff.py
Language: Python
License: GPL
Copyright: Copyright 2005, 2006 Zuza Software Foundation
LOC: 66
Project Info
translate
Server: SourceForge
Type: cvs
...late\src\translate\convert\
   .cvsignore
   __init__.py
   convert.py
   csv2po.py
   csv2tbx.py
   dtd2po.py
   html2po.py
   moz2po.py
   mozfunny2prop.py
   nb2po.py
   oo2po.py
   po2csv.py
   po2dtd.py
   po2html.py
   po2moz.py
   po2nb.py
   po2oo.py
   po2prop.py
   po2tmx.py
   po2ts.py
   po2txt.py
   po2xliff.py
   poreplace.py
   pot2po.py
   prop2mozfunny.py
   prop2po.py
   sxw2po.py
   test_convert.py
   test_csv2po.py
   test_dtd2po.py
   test_html2po.py
   test_moz2po.py
   test_mozfunny2prop.py
   test_oo2po.py
   test_po2csv.py
   test_po2dtd.py
   test_po2html.py
   test_po2moz.py
   test_po2oo.py
   test_po2prop.py
   test_po2tmx.py
   test_po2ts.py
   test_po2txt.py
   test_po2xliff.py
   test_pot2po.py
   test_prop2mozfunny.py
   test_prop2po.py
   test_sxw2po.py
   test_ts2po.py
   test_txt2po.py
   test_xliff2po.py
   ts2po.py
   txt2po.py
   xliff2po.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2005, 2006 Zuza Software Foundation
# 
# This file is part of translate.
#
# translate is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# translate is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with translate; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

"""Converts Gettext .po files to .xliff localization files
You can convert back from .xliff to .po using po2xliff"""

from translate.storage import po
from translate.storage import poxliff
from translate.misc import quote
from xml.dom import minidom

class po2xliff:
  def convertunit(self, xlifffile, thepo, filename):
    """creates a transunit node"""
    source = thepo.source
    target = thepo.target
    if thepo.isheader():
      unit = xlifffile.addheaderunit(target, filename)
    else:
      unit = xlifffile.addsourceunit(source, filename, True)
      unit.target = target
      if thepo.isfuzzy():
        unit.markfuzzy()
      elif target:
        #TODO: consider if an empty target can be considered as translated
        unit.marktranslated()
      
      #Handle #: location comments
      for location in thepo.getlocations():
        unit.createcontextgroup("po-reference", self.contextlist(location), purpose="location")
      
      #Handle #. automatic comments
      comment = "\n".join([comment[3:-1] for comment in thepo.automaticcomments])
      if comment:
        unit.createcontextgroup("po-entry", [("x-po-autocomment", comment)], purpose="information")
    
      #TODO: x-format, etc.


    #Handle # other comments
    comment = "\n".join([comment[2:-1] for comment in thepo.othercomments])
    if comment:
      unit.createcontextgroup("po-entry", [("x-po-trancomment", comment)], purpose="information")
      unit.addnote(comment)
      
    return unit

  def contextlist(self, location):
    contexts = []
    if ":" in location:
      sourcefile, linenumber = location.split(":", 1)
    else:
      sourcefile, linenumber = location, None
    contexts.append(("sourcefile", sourcefile))
    if linenumber:
      contexts.append(("linenumber", linenumber))
    return contexts
    
  def convertfile(self, thepofile, templatefile=None):
    """converts a .po file to .xliff format"""
    if templatefile is None: 
      xlifffile = poxliff.PoXliffFile()
    else:
      xlifffile = poxliff.PoXliffFile(templatefile)
    filename = thepofile.filename
    for thepo in thepofile.units:
      if thepo.isblank():
        continue
      transunitnode = self.convertunit(xlifffile, thepo, filename)
    return str(xlifffile)

def convertpo(inputfile, outputfile, templatefile):
  """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
  inputpo = po.pofile(inputfile)
  if inputpo.isempty():
    return 0
  convertor = po2xliff()
  outputxliff = convertor.convertfile(inputpo, templatefile)
  outputfile.write(outputxliff)
  return 1

def main(argv=None):
  from translate.convert import convert
  formats = {"po": ("xliff", convertpo), ("po", "xliff"): ("xliff", convertpo)}
  parser = convert.ConvertOptionParser(formats, usepots=True, usetemplates=True, description=__doc__)
  parser.run(argv)

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us