iOS開発-国際化はPythonスクリプトを使用してLocalizable.stringsをExcelに変換します

序文

  • iOS開発では、翻訳を容易にするためにLocalizable.stringsをExcelテーブルに変換する必要があります。

環境が必要

Pythonのバージョンを確認するPythonのバージョンは2.xである必要があります

  • 端子入力
python --version

Python 2.7.10

pip(pythonパッケージマネージャー)を確認してください

  • 端子入力
pip --version

/Library/Python/2.7/site-packages/pipからのpip19.0(python 2.7)

  • pipがインストールされていない場合
  • 端子入力
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
  • 端子入力
sudo python get-pip.py

pyexceleratorをインストールします

sudo pip install pyExcelerator

xlrdをインストールします

sudo pip install xlrd

ツールコード

  • Strings2Xls.py
# -*- coding:utf-8 -*-

import os
from optparse import OptionParser
from StringsFileUtil import StringsFileUtil
import pyExcelerator
import time

# Add command option


def addParser():
    parser = OptionParser()

    parser.add_option("-f", "--stringsDir",
                      help=".strings files directory.",
                      metavar="stringsDir")

    parser.add_option("-t", "--targetDir",
                      help="The directory where the excel(.xls) files will be saved.",
                      metavar="targetDir")

    parser.add_option("-e", "--excelStorageForm",
                      type="string",
                      default="multiple",
                      help="The excel(.xls) file storage forms including single(single file), multiple(multiple files), default is multiple.",
                      metavar="excelStorageForm")

    (options, _) = parser.parse_args()

    return options

#  convert .strings files to single xls file


def convertToSingleFile(stringsDir, targetDir):
    destDir = targetDir + "/strings-files-to-xls_" + \
        time.strftime("%Y%m%d_%H%M%S")
    if not os.path.exists(destDir):
        os.makedirs(destDir)

    # Create xls sheet
    for _, dirnames, _ in os.walk(stringsDir):
        lprojDirs = [di for di in dirnames if di.endswith(".lproj")]
        for dirname in lprojDirs:
            for _, _, filenames in os.walk(stringsDir+'/'+dirname):
                stringsFiles = [
                    fi for fi in filenames if fi.endswith(".strings")]
                for stringfile in stringsFiles:
                    fileName = stringfile.replace(".strings", "")
                    filePath = destDir + "/" + fileName + ".xls"
                    if not os.path.exists(filePath):
                        workbook = pyExcelerator.Workbook()
                        ws = workbook.add_sheet(fileName)
                        index = 0
                        for dirname in dirnames:
                            if index == 0:
                                ws.write(0, 0, 'keyName')
                            countryCode = dirname.replace(".lproj", "")
                            ws.write(0, index+1, countryCode)

                            path = stringsDir+'/' + dirname + '/' + stringfile
                            (keys, values) = StringsFileUtil.getKeysAndValues(
                                path)
                            for x in range(len(keys)):
                                key = keys[x]
                                value = values[x]
                                if (index == 0):
                                    ws.write(x+1, 0, key)
                                    ws.write(x+1, 1, value)
                                else:
                                    ws.write(x+1, index + 1, value)
                            index += 1
                        workbook.save(filePath)
    print "Convert %s successfully! you can see xls file in %s" % (
        stringsDir, destDir)


#  convert .strings files to multiple xls files


def convertToMultipleFiles(stringsDir, targetDir):
    destDir = targetDir + "/strings-files-to-xls_" + \
        time.strftime("%Y%m%d_%H%M%S")
    if not os.path.exists(destDir):
        os.makedirs(destDir)

    for _, dirnames, _ in os.walk(stringsDir):
        lprojDirs = [di for di in dirnames if di.endswith(".lproj")]
        for dirname in lprojDirs:
            workbook = pyExcelerator.Workbook()
            for _, _, filenames in os.walk(stringsDir+'/'+dirname):
                stringsFiles = [
                    fi for fi in filenames if fi.endswith(".strings")]
                for stringfile in stringsFiles:
                    ws = workbook.add_sheet(stringfile)

                    path = stringsDir+dirname+'/' + stringfile
                    (keys, values) = StringsFileUtil.getKeysAndValues(
                        path)
                    for keyIndex in range(len(keys)):
                        key = keys[keyIndex]
                        value = values[keyIndex]
                        ws.write(keyIndex, 0, key)
                        ws.write(keyIndex, 1, value)

            filePath = destDir + "/" + dirname.replace(".lproj", "") + ".xls"
            workbook.save(filePath)

    print "Convert %s successfully! you can see xls file in %s" % (
        stringsDir, destDir)

# Start convert .strings files to xls


def startConvert(options):
    stringsDir = options.stringsDir
    targetDir = options.targetDir

    print "Start converting"

    if stringsDir is None:
        print ".strings files directory can not be empty! try -h for help."
        return

    if targetDir is None:
        print "Target file directory can not be empty! try -h for help."
        return

    if options.excelStorageForm == "single":
        convertToSingleFile(stringsDir, targetDir)
    else:
        convertToMultipleFiles(stringsDir, targetDir)


def main():
    options = addParser()
    startConvert(options)


main()

使用する

  • 独自のLocalizable.stringsファイルをiOSパスにドラッグします
    ここに画像の説明を挿入
  • ターミナルを開く
cd /Users/xxx/Desktop/Demo

ここに画像の説明を挿入

  • 再入力
python py/Strings2Xls.py -f /Users/lindashuai/Desktop/Demo/ios/ -t /Users/lindashuai/Desktop/Demo/output/

ここに画像の説明を挿入

ツールアドレス

ツールアドレス

おすすめ

転載: blog.csdn.net/weixin_41732253/article/details/110288709