iOSの Localizable.strings
をprefixルールで管理していて、昇順ソートした方がわかりやすそうだったのでpythonでスクリプトを書きました。 python-localizableというライブラリを使います。
# Usage: `python scripts/localization/sort_localizable_strings.py {Localizable.stringsへのフルパス}`
import sys
import localizable
import codecs
import os
args = sys.argv
if len(args) < 2:
print('first arg should be filePath.')
sys.exit()
filePath = args[1]
if os.path.isfile(filePath):
strings = localizable.parse_strings(filename=filePath)
strings = sorted(strings, key=lambda s: s['key'])
txt = ''
for s in strings:
if s['comment'] != '':
txt += "/*" + s['comment'] + "*/\n"
txt += "\"" + s['key'] + "\" = \""
txt += s['value'].replace('\n', '\\n').replace('\r', '\\r').replace('\"', '\\"') + "\";\n\n"
f_output = codecs.open(filePath, 'w+', 'utf-8')
f_output.write(txt)
f_output.close()
print('Sorted Localizable.strings')
else:
print('Localizable.strings does not exist.')
以上。
python初心者なのでもっといい書き方があれば指摘お願いします