iOS 国际化.Strings文件自动生成

2023-10-10  本文已影响0人  Kpengs

前言

公司最近做了国际化功能,在开会的时候,同事提出了是否可以进行自动化生成国际化文件,而不是一个个粘贴复制,于是去做了一些调研。整理了一些方案

实际方案

方案一 三方工具

这是朋友自己写的一款Mac App 用于批量导出国际化文件
用法在App里有
https://apps.apple.com/cn/app/pod-publisher/id6443565431?mt=12

方案一 自己通过python实现转换

1.用Excel建立格式化的csv文件

例如


image.png

2.编写python代码

import csv

def generate_strings_file(csv_file_path, output_path_base):
    with open(csv_file_path, newline='', encoding='utf-8') as csvfile:
        reader = csv.reader(csvfile)
        header = next(reader)  # Get the header row

        # Map languages to column indices
        languages = {"English": header.index("English"), "Chinese": header.index("Chinese")}

        for language, language_column in languages.items():
            output_path = f"{output_path_base}_{language.lower()}.strings"
            with open(output_path, 'w', encoding='utf-8') as output_file:
                for row in reader:
                    key = row[0]
                    translation = row[language_column]
                    output_file.write(f'"{key}" = "{translation}";\n')
                csvfile.seek(0)  # Reset file pointer for the next language

if __name__ == "__main__":
    generate_strings_file("fanyiTest.csv", "Localizable")

3.将python代码和csv放在同一目录

cd 到该目录
执行

python3 generate_strings.py

然后就会生成中英文的String格式的文件了


image.png
上一篇 下一篇

猜你喜欢

热点阅读