PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/translate/convert/po2tiki.py

https://github.com/andynicholson/translate
Python | 84 lines | 64 code | 1 blank | 19 comment | 0 complexity | 6631e2b5aa8e29c6a3cb3961698e7176 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 .po files to TikiWiki's language.php files. """
  21. import sys
  22. from translate.storage import tiki
  23. from translate.storage import po
  24. class po2tiki:
  25. def convertstore(self, thepofile):
  26. """Converts a given (parsed) po file to a tiki file.
  27. :param thepofile: a pofile pre-loaded with input data
  28. """
  29. thetargetfile = tiki.TikiStore()
  30. for unit in thepofile.units:
  31. if not (unit.isblank() or unit.isheader()):
  32. newunit = tiki.TikiUnit(unit.source)
  33. newunit.settarget(unit.target)
  34. locations = unit.getlocations()
  35. if locations:
  36. newunit.addlocations(locations)
  37. # If a word is "untranslated" but the target isn't empty and isn't the same as the source
  38. # it's been translated and we switch it. This is an assumption but should remain true as long
  39. # as these scripts are used.
  40. if newunit.getlocations() == ["untranslated"] and unit.source != unit.target and unit.target != "":
  41. newunit.location = []
  42. newunit.addlocation("translated")
  43. thetargetfile.addunit(newunit)
  44. return thetargetfile
  45. def convertpo(inputfile, outputfile, template=None):
  46. """Converts from po file format to tiki.
  47. :param inputfile: file handle of the source
  48. :param outputfile: file handle to write to
  49. :param template: unused
  50. """
  51. inputstore = po.pofile(inputfile)
  52. if inputstore.isempty():
  53. return False
  54. convertor = po2tiki()
  55. outputstore = convertor.convertstore(inputstore)
  56. outputfile.write(str(outputstore))
  57. return True
  58. def main(argv=None):
  59. """Will convert from .po to tiki style .php"""
  60. from translate.convert import convert
  61. from translate.misc import stdiotell
  62. sys.stdout = stdiotell.StdIOWrapper(sys.stdout)
  63. formats = {"po": ("tiki", convertpo)}
  64. parser = convert.ConvertOptionParser(formats, description=__doc__)
  65. parser.run(argv)
  66. if __name__ == '__main__':
  67. main()