restore-i18n.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #! /usr/bin/python
  2. __copyright__ = '''\
  3. Copyright (C) 2008 Geometer Plus <contact@geometerplus.com>
  4. This program is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free Software
  6. Foundation; either version 2 of the License, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  10. PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
  13. Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. '''
  15. import sys, os, csv
  16. from optparse import OptionParser
  17. from elementtree.ElementTree import Element, SubElement, ElementTree
  18. SPACES = '\t'
  19. def fix_it(elem):
  20. if len(elem):
  21. for child in elem:
  22. fix_it(child)
  23. last = elem.getchildren()[-1]
  24. last.tail = last.tail[:-len(SPACES)]
  25. class Collector:
  26. def __init__(self, dirname, dictionary):
  27. self._dirname = dirname
  28. self._dictionary = dictionary
  29. self._resources = {}
  30. self._resources_order = []
  31. def process(self, lang):
  32. assert len(lang) == 2, 'Language name must be two letters long'
  33. doc = ElementTree(file=os.path.join(self._dirname, '%s.xml' % lang))
  34. root = doc.getroot()
  35. if root.tag == 'resources':
  36. for child in root:
  37. self.walk(child, (child.get('name'),), lang)
  38. def walk(self, element, prefix, lang):
  39. value = element.get('value', None)
  40. if value is not None and element.get('toBeTranslated', None) is None:
  41. if prefix not in self._resources:
  42. tempo = {}
  43. self._resources_order.append(prefix)
  44. else:
  45. tempo = self._resources[prefix]
  46. tempo[lang] = value
  47. self._resources[prefix] = tempo
  48. for child in element:
  49. self.walk(child, prefix + (child.get('name'), ), lang)
  50. def dump(self, output):
  51. tempo = {}
  52. root = Element('resources')
  53. root.tail = '\n'
  54. tempo[()] = root
  55. for key in self._resources_order:
  56. for i in range(1, len(key)+1):
  57. if key[0:i] not in tempo:
  58. parent = tempo[key[0:i-1]]
  59. value = self._resources.get(key[0:i], None)
  60. if value is None:
  61. elem = SubElement(parent, 'node', name=key[i-1])
  62. else:
  63. fullKey = key[0];
  64. for j in range(1, i):
  65. fullKey += '/' + key[j]
  66. newValue = self._dictionary[fullKey]
  67. elem = SubElement(parent, 'node', name=key[i-1], value=newValue)
  68. parent.text = elem.tail = '\n' + i*SPACES
  69. tempo[key[0:i]] = elem
  70. fix_it(root)
  71. print >> output, '<?xml version="1.0" encoding="UTF-8"?>'
  72. ElementTree(root).write(output, 'ascii')
  73. def main():
  74. '''actual worker'''
  75. parser = OptionParser()
  76. options, args = parser.parse_args()
  77. dictionary = {}
  78. reader = csv.reader(sys.stdin)
  79. for pair in reader:
  80. dictionary[pair[0]] = pair[1]
  81. coll = Collector('.', dictionary)
  82. coll.process('en')
  83. coll.dump(sys.stdout)
  84. if __name__ == '__main__':
  85. main()
  86. # vim:ts=4:sw=4:et