PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/translate/convert/tiki2po.py

https://github.com/andynicholson/translate
Python | 94 lines | 74 code | 1 blank | 19 comment | 0 complexity | 57240559cd5c8a70e6d09810248063fc MD5 | raw file
Possible License(s): GPL-2.0
  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 this program; if not, see <http://www.gnu.org/licenses/>.
  20. """ Convert TikiWiki's language.php files to GetText PO files. """
  21. import sys
  22. from translate.storage import tiki
  23. from translate.storage import po
  24. class tiki2po:
  25. def __init__(self, includeunused=False):
  26. """
  27. :param includeunused: On conversion, should the "unused" section be
  28. 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. # For each lang unit, make the new po unit accordingly
  37. for unit in thetikifile.units:
  38. if not self.includeunused and "unused" in unit.getlocations():
  39. continue
  40. newunit = po.pounit()
  41. newunit.source = unit.source
  42. newunit.settarget(unit.target)
  43. locations = unit.getlocations()
  44. if locations:
  45. newunit.addlocations(locations)
  46. thetargetfile.addunit(newunit)
  47. return thetargetfile
  48. def converttiki(inputfile, outputfile, template=None, includeunused=False):
  49. """Converts from tiki file format to po.
  50. :param inputfile: file handle of the source
  51. :param outputfile: file handle to write to
  52. :param template: unused
  53. :param includeunused: Include the "usused" section of the tiki
  54. file? Default: False
  55. """
  56. convertor = tiki2po(includeunused=includeunused)
  57. inputstore = tiki.TikiStore(inputfile)
  58. outputstore = convertor.convertstore(inputstore)
  59. if outputstore.isempty():
  60. return False
  61. outputfile.write(str(outputstore))
  62. return True
  63. def main(argv=None):
  64. """Converts tiki .php files to .po."""
  65. from translate.convert import convert
  66. from translate.misc import stdiotell
  67. sys.stdout = stdiotell.StdIOWrapper(sys.stdout)
  68. formats = {"php": ("po", converttiki)}
  69. parser = convert.ConvertOptionParser(formats, description=__doc__)
  70. parser.add_option("", "--include-unused", dest="includeunused",
  71. action="store_true", default=False,
  72. help="Include strings in the unused section")
  73. parser.passthrough.append("includeunused")
  74. parser.run(argv)
  75. if __name__ == '__main__':
  76. main()