如常見的翻譯文案中,當存在多個佔位符狀況下,須要將多個%s佔位符成Android標準佔位符格式,如%1 s,%3$s...不然,在Gradle構建過程當中,某些版本的AAPT上會構建失敗。html
AAPT: multiple substitutions specified in non-positional format.
複製代碼
所以,須要用腳本進行批量替換。 本文Mark下,防止後續用到相似腳本,主要都是一些Python語法細節。markdown
def replace_multi_placeholder_text(input_file):
lines = []
with open(input_file) as src:
for line in src:
num = line.count("%s")
if num > 1:
for i in range(num):
target = '%%%d$s' % (i+1,)
line = line.replace('%s', target, 1)
lines.append(line)
with open(input_file, 'w') as dst:
for line in lines:
dst.write(line)
return
複製代碼