/convert/tiki2po.py

https://github.com/mellterm/translate · Python · 88 lines · 47 code · 3 blank · 38 comment · 1 complexity · d9e840b3afcd9f3e2f46db1b24807eec MD5 · raw file

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2008 Mozilla Corporation, Zuza Software Foundation
  5. #
  6. # This file is part of translate.
  7. #
  8. # translate 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. # translate 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 translate; if not, write to the Free Software
  20. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. """ Convert TikiWiki's language.php files to GetText PO files. """
  22. import sys
  23. from translate.storage import tiki
  24. from translate.storage import po
  25. class tiki2po:
  26. def __init__(self, includeunused=False):
  27. """
  28. @param includeunused: On conversion, should the "unused" section be preserved? Default: False
  29. """
  30. self.includeunused = includeunused
  31. def convertstore(self, thetikifile):
  32. """Converts a given (parsed) tiki file to a po file.
  33. @param thetikifile: a tikifile pre-loaded with input data
  34. """
  35. thetargetfile = po.pofile()
  36. # Set up the header
  37. targetheader = thetargetfile.init_headers(charset="UTF-8", encoding="8bit")
  38. # For each lang unit, make the new po unit accordingly
  39. for unit in thetikifile.units:
  40. if not self.includeunused and "unused" in unit.getlocations():
  41. continue
  42. newunit = po.pounit()
  43. newunit.source = unit.source
  44. newunit.settarget(unit.target)
  45. locations = unit.getlocations()
  46. if locations:
  47. newunit.addlocations(locations)
  48. thetargetfile.addunit(newunit)
  49. return thetargetfile
  50. def converttiki(inputfile, outputfile, template=None, includeunused=False):
  51. """Converts from tiki file format to po.
  52. @param inputfile: file handle of the source
  53. @param outputfile: file handle to write to
  54. @param template: unused
  55. @param includeunused: Include the "usused" section of the tiki file? Default: False
  56. """
  57. convertor = tiki2po(includeunused=includeunused)
  58. inputstore = tiki.TikiStore(inputfile)
  59. outputstore = convertor.convertstore(inputstore)
  60. if outputstore.isempty():
  61. return False
  62. outputfile.write(str(outputstore))
  63. return True
  64. def main(argv=None):
  65. """Converts tiki .php files to .po."""
  66. from translate.convert import convert
  67. from translate.misc import stdiotell
  68. sys.stdout = stdiotell.StdIOWrapper(sys.stdout)
  69. formats = {"php":("po", converttiki)}
  70. parser = convert.ConvertOptionParser(formats, description=__doc__)
  71. parser.add_option("", "--include-unused", dest="includeunused", action="store_true", default=False, help="Include strings in the unused section")
  72. parser.passthrough.append("includeunused")
  73. parser.run(argv)
  74. if __name__ == '__main__':
  75. main()