genstrings.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # Created by Johannes Schriewer on 2011-11-30. Modified by Roy Marmelstein 2015-08-05
  2. # Copyright (c) 2011 planetmutlu.
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions
  7. # are met:
  8. #
  9. # - Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # - Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  17. # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  18. # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
  19. # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. #
  27. # This script is heavily copied from: https://github.com/dunkelstern/Cocoa-Localisation-Helper
  28. # Copied from https://github.com/marmelroy/Localize-Swift By QiuYuzhou
  29. import os, re, subprocess
  30. import fnmatch
  31. def fetch_files_recursive(directory, extension):
  32. matches = []
  33. for root, dirnames, filenames in os.walk(directory):
  34. for filename in fnmatch.filter(filenames, '*' + extension):
  35. matches.append(os.path.join(root, filename))
  36. return matches
  37. # prepare regexes
  38. localizedStringComment = re.compile('NSLocalizedString\("([^"]*)",\s*"([^"]*)"\s*\)', re.DOTALL)
  39. localizedStringNil = re.compile('NSLocalizedString\("([^"]*)",\s*nil\s*\)', re.DOTALL)
  40. localized = re.compile('Localized\("([^"]*)"[^\n\r]*\)', re.DOTALL)
  41. localizedProperty = re.compile('"([^"]*)".localized', re.DOTALL)# Add By QiuYuzhou
  42. localizedSwift2 = re.compile('"([^"]*)".localized\(\)', re.DOTALL)
  43. localizedSwift2WithFormat = re.compile('"([^"]*)".localizedFormat\([^\n\r]*\)', re.DOTALL)
  44. # get string list
  45. uid = 0
  46. strings = []
  47. for file in fetch_files_recursive('.', '.swift'):
  48. with open(file, 'r') as f:
  49. content = f.read()
  50. for result in localizedStringComment.finditer(content):
  51. uid += 1
  52. strings.append((result.group(1), result.group(2), file, uid))
  53. for result in localizedStringNil.finditer(content):
  54. uid += 1
  55. strings.append((result.group(1), '', file, uid))
  56. for result in localized.finditer(content):
  57. uid += 1
  58. strings.append((result.group(1), '', file, uid))
  59. # Add By QiuYuzhou, Begin
  60. for result in localizedProperty.finditer(content):
  61. uid += 1
  62. strings.append((result.group(1), '', file, uid))
  63. # Add By QiuYuzhou, End
  64. for result in localizedSwift2.finditer(content):
  65. uid += 1
  66. strings.append((result.group(1), '', file, uid))
  67. for result in localizedSwift2WithFormat.finditer(content):
  68. uid += 1
  69. strings.append((result.group(1), '', file, uid))
  70. # prepare regexes
  71. localizedString = re.compile('"[^=]*=\s*"([^"]*)";')
  72. # Changed By QiuYuzhou, disable for *.xib
  73. # fetch files
  74. #for file in fetch_files_recursive('.', '.xib'):
  75. # tempFile = file + '.strings'
  76. # utf8tempFile = file + '.strings.utf8'
  77. # subprocess.call('ibtool --export-strings-file "' + tempFile + '" "' + file + '" 2>/dev/null', shell=True)
  78. # subprocess.call('iconv -s -f UTF-16 -t UTF-8 "' + tempFile + '" >"'+utf8tempFile+'" 2>/dev/null', shell=True)
  79. #
  80. # f = open(utf8tempFile, 'r')
  81. # for line in f:
  82. # result = localizedString.match(line)
  83. # if result:
  84. # uid += 1
  85. # strings.append((result.group(1), '', file, uid))
  86. # f.close()
  87. #
  88. # os.remove(utf8tempFile)
  89. # os.remove(tempFile)
  90. # find duplicates
  91. duplicated = []
  92. filestrings = {}
  93. for string1 in strings:
  94. dupmatch = 0
  95. for string2 in strings:
  96. if string1[3] == string2[3]:
  97. continue
  98. if string1[0] == string2[0]:
  99. if string1[2] != string2[2]:
  100. dupmatch = 1
  101. break
  102. if dupmatch == 1:
  103. dupmatch = 0
  104. for string2 in duplicated:
  105. if string1[0] == string2[0]:
  106. dupmatch = 1
  107. break
  108. if dupmatch == 0:
  109. duplicated.append(string1)
  110. else:
  111. dupmatch = 0
  112. if string1[2] in filestrings:
  113. for fs in filestrings[string1[2]]:
  114. if fs[0] == string1[0]:
  115. dupmatch = 1
  116. break
  117. else:
  118. filestrings[string1[2]] = []
  119. if dupmatch == 0:
  120. filestrings[string1[2]].append(string1)
  121. print '\n\n\n\n\n'
  122. print '/*\n * SHARED STRINGS\n */\n'
  123. # output filewise
  124. for key in filestrings.keys():
  125. print '/*\n * ' + key + '\n */\n'
  126. strings = filestrings[key]
  127. for string in strings:
  128. if string[1] == '':
  129. print '"' + string[0] + '" = "' + string[0] + '";'
  130. print
  131. else:
  132. print '/* ' + string[1] + ' */'
  133. print '"' + string[0] + '" = "' + string[0] + '";'
  134. print
  135. # output duplicates
  136. for string in duplicated:
  137. if string[1] == '':
  138. print '"' + string[0] + '" = "' + string[0] + '";'
  139. print
  140. else:
  141. print '/* ' + string[1] + ' */'
  142. print '"' + string[0] + '" = "' + string[0] + '";'
  143. print