Last active
July 17, 2022 06:07
-
-
Save asus4/a2f26f78e15f7af91dec2591eadd2b0a to your computer and use it in GitHub Desktop.
Converts a TSV file into Xcode Localizable strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import argparse | |
import csv | |
import datetime | |
# A simple script that converts a TSV file into Xcode Localizable strings | |
# TSV format: | |
''' | |
id, comment, en, ja | |
hello, comment, Hello, こんにちは | |
world, comment, World, 世界 | |
''' | |
# Then convert to Xcode Localizable strings like this: | |
# ja.lproj/Localizable.strings | |
''' | |
/* comment */ | |
"hello" = "こんにちは"; | |
/* comment */ | |
"world" = "世界"; | |
''' | |
# en.lproj/Localizable.strings | |
''' | |
/* comment */ | |
"hello" = "Hello"; | |
/* comment */ | |
"world" = "World"; | |
''' | |
# Note: This script overrides the existing Localizable.strings | |
def main(input: str, out_dir: str): | |
with open(input, 'r') as f: | |
reader = csv.reader(f, delimiter="\t") | |
header = next(reader) | |
languages = header[2:] | |
header_comment = f"""/* | |
Generated from {input} | |
Use tsv2localizable.py to update | |
Last Updated: {datetime.date.today()} | |
*/ | |
""" | |
localizables = [[header_comment] for _ in range(len(languages))] | |
print(f"Found {len(languages)} languages: {languages}") | |
for row in reader: | |
id = row[0] | |
comment = row[1] | |
for i, _ in enumerate(languages): | |
localizables[i].append(f'/* {comment} */') | |
localizables[i].append(f'"{id}" = "{row[i+2]}";') | |
print(localizables) | |
# Write localizables to files | |
for i, language in enumerate(languages): | |
with open(f"{out_dir}/{language}.lproj/Localizable.strings", 'w') as f: | |
f.write('\n'.join(localizables[i])) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser("Convert TSV to Xcode Localizable.strings") | |
parser.add_argument("-i", "--input", type=str, help="Input TSV file") | |
parser.add_argument("-o", "--out_dir", type=str, default='.', help="Output Xcode Localizable.strings file") | |
args = parser.parse_args() | |
main(args.input, args.out_dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment