LoginSignup
5
2

More than 5 years have passed since last update.

Localizable.stringsファイルをソートするスクリプト

Posted at

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初心者なのでもっといい書き方があれば指摘お願いします :bow:

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2