generate_metadata.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env python3
  2. # Author: Torsten Grote
  3. # License: GPLv3 or later
  4. # copied on 2017/11/06 from https://github.com/grote/Transportr/blob/master/fastlane/generate_metadata.py
  5. # adapted by Tobias Kaminsky
  6. import os
  7. import shutil
  8. import codecs
  9. from xml.etree import ElementTree
  10. XML_PATH = '../../src/main/res'
  11. METADATA_PATH = '../../fastlane/metadata/android'
  12. DEFAULT_LANG = 'en-US'
  13. LANG_MAP = {
  14. 'values': 'en-US',
  15. 'values-en-rGB': 'en-GB',
  16. 'values-ca': 'ca',
  17. 'values-cs': 'cs-CZ',
  18. 'values-de': 'de-DE',
  19. 'values-es': 'es-ES',
  20. 'values-fr': 'fr-FR',
  21. 'values-hu': 'hu-HU',
  22. 'values-it': 'it-IT',
  23. 'values-pt-rBR': 'pt-BR',
  24. 'values-pt-rPT': 'pt-PT',
  25. 'values-ta': 'ta-IN',
  26. 'values-sv': 'sv-SE',
  27. 'values-sq-rAL': 'sq-AL',
  28. 'values-sq-rMK': 'sq-MK',
  29. 'values-iw-rIL': 'iw-IL',
  30. 'values-ar': 'ar-AR',
  31. 'values-bg-rBG': 'bg-BG',
  32. 'values-da': 'da-DK',
  33. 'values-fi-rFI': 'fi-FI',
  34. 'values-gl-rES': 'gl-ES',
  35. 'values-tr': 'tr-TR',
  36. 'values-uk': 'uk-UK',
  37. 'values-vi': 'vi-VI',
  38. 'values-ro': 'ro-RO',
  39. 'values-ou': 'ru-RU',
  40. 'values-sr': 'sr-SR',
  41. 'values-pl': 'pl-PL',
  42. 'values-el': 'el-GR',
  43. 'values-ko': 'ko-KR',
  44. 'values-nl': 'nl-NL',
  45. 'values-ja': 'ja-JP',
  46. 'values-no-rNO': 'no-NO',
  47. 'values-eu': 'eu-ES',
  48. 'values-lt-rLT': 'lt-LT',
  49. 'values-zh-rKN': 'zh-HK',
  50. 'values-zk': 'zk-CN',
  51. 'values-is': 'is-IS',
  52. 'values-id': 'id-ID',
  53. 'values-cs-rCZ': 'cs-CZ',
  54. 'values-sl': 'sl-SL',
  55. 'values-fa': 'fa-FA'
  56. }
  57. PATH = os.path.dirname(os.path.realpath(__file__))
  58. def main():
  59. path = os.path.join(PATH, XML_PATH)
  60. for entry in os.listdir(path):
  61. directory = os.path.join(path, entry)
  62. if not os.path.isdir(directory) or entry not in LANG_MAP.keys():
  63. continue
  64. strings_file = os.path.join(directory, 'strings.xml')
  65. if not os.path.isfile(strings_file):
  66. print("Error: %s does not exist" % strings_file)
  67. continue
  68. print()
  69. print(LANG_MAP[entry])
  70. print("Parsing %s..." % strings_file)
  71. e = ElementTree.parse(strings_file).getroot()
  72. short_desc = e.find('.//string[@name="store_short_desc"]')
  73. full_desc = e.find('.//string[@name="store_full_desc"]')
  74. if short_desc is None or full_desc is None:
  75. print("Warning: Skipping %s because of incomplete translation" % entry)
  76. continue
  77. save_file(short_desc.text, LANG_MAP[entry], 'short_description.txt')
  78. save_file(full_desc.text, LANG_MAP[entry], 'full_description.txt')
  79. def save_file(text, directory, filename):
  80. directory_path = os.path.join(PATH, METADATA_PATH, directory)
  81. if not os.path.exists(directory_path):
  82. os.makedirs(directory_path)
  83. if filename == 'short_description.txt':
  84. limit = 80
  85. else:
  86. limit = 0
  87. text = clean_text(text, limit)
  88. check_title(directory_path)
  89. file_path = os.path.join(directory_path, filename)
  90. print("Writing %s..." % file_path)
  91. with codecs.open(file_path, 'w', 'utf-8') as f:
  92. f.write(text)
  93. def clean_text(text, limit=0):
  94. text = text.replace('\\\'', '\'')
  95. if limit != 0 and len(text) > limit:
  96. print("Warning: Short Description longer than 80 characters, truncating...")
  97. text = text[:limit]
  98. return text
  99. def check_title(directory):
  100. title_path = os.path.join(directory, 'title.txt')
  101. if os.path.exists(title_path):
  102. return
  103. default_title_path = os.path.join(directory, '..', DEFAULT_LANG, 'title.txt')
  104. shutil.copy(default_title_path, title_path)
  105. if __name__ == "__main__":
  106. main()