PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/translate/tools/pypo2phppo.py

https://github.com/andynicholson/translate
Python | 109 lines | 68 code | 6 blank | 35 comment | 0 complexity | e181717ddcfdedcec4843b1b7c023860 MD5 | raw file
Possible License(s): GPL-2.0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2009 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 Python format .po files to PHP format .po files"""
  21. import re
  22. from translate.storage import po
  23. from translate.misc.multistring import multistring
  24. class pypo2phppo:
  25. def convertstore(self, inputstore):
  26. """Converts a given .po file (Python Format) to a PHP format .po file.
  27. The difference being how variable substitutions work. PHP uses a %1$s
  28. format, and Python uses a {0} format (zero indexed). This method will
  29. convert::
  30. I have {1} apples and {0} oranges
  31. To::
  32. I have %2$s apples and %1$s oranges
  33. This method ignores strings with %s as both languages will recognize
  34. that.
  35. """
  36. thetargetfile = po.pofile(inputfile="")
  37. for unit in inputstore.units:
  38. newunit = self.convertunit(unit)
  39. thetargetfile.addunit(newunit)
  40. return thetargetfile
  41. def convertunit(self, unit):
  42. developer_notes = unit.getnotes(origin="developer")
  43. translator_notes = unit.getnotes(origin="translator")
  44. unit.removenotes()
  45. unit.addnote(self.convertstrings(developer_notes))
  46. unit.addnote(self.convertstrings(translator_notes))
  47. unit.source = self.convertstrings(unit.source)
  48. unit.target = self.convertstrings(unit.target)
  49. return unit
  50. def convertstring(self, string):
  51. return re.sub('\{(\d)\}',
  52. lambda x: "%%%d$s" % (int(x.group(1)) + 1), string)
  53. def convertstrings(self, input):
  54. if isinstance(input, multistring):
  55. strings = input.strings
  56. elif isinstance(input, list):
  57. strings = input
  58. else:
  59. return self.convertstring(input)
  60. for index, string in enumerate(strings):
  61. strings[index] = re.sub('\{(\d)\}',
  62. lambda x: "%%%d$s" % (int(x.group(1)) + 1),
  63. string)
  64. return strings
  65. def convertpy2php(inputfile, outputfile, template=None):
  66. """Converts from Python .po to PHP .po
  67. :param inputfile: file handle of the source
  68. :param outputfile: file handle to write to
  69. :param template: unused
  70. """
  71. convertor = pypo2phppo()
  72. inputstore = po.pofile(inputfile)
  73. outputstore = convertor.convertstore(inputstore)
  74. if outputstore.isempty():
  75. return False
  76. outputfile.write(str(outputstore))
  77. return True
  78. def main(argv=None):
  79. """Converts from Python .po to PHP .po"""
  80. from translate.convert import convert
  81. formats = {"po": ("po", convertpy2php)}
  82. parser = convert.ConvertOptionParser(formats, description=__doc__)
  83. parser.run(argv)
  84. if __name__ == '__main__':
  85. main()