Script to convert between Xcode language files and tab-delimited text

A script that converts Xcode language files (Localizable.strings) into easy-to-handle tab-separated texts (tsv). Until now, I used to manage language files with Linguan, but as the project became huge, it became difficult to manage. To manage with tab-delimited text.

Python module installation

bash



pip install unicodecsv

Convert Localizable.strings to Localizable.tsv

i18n_po2tsv.py



import sys, re
import unicodecsv as csv
from collections import OrderedDict

#
# Default encoding
#

reload(sys)
sys.setdefaultencoding('utf8')

#
# File paths
#

tsv_path = 'Localization/Localizable.tsv'
loc_paths = 'PROJECT_NAME/PROJECT_NAME/Resources/UI/{}.lproj/Localizable.strings'

#
# Convert Localizable.strings to Localizable.tsv
#

langs = [
	'ja',
	'en',
]
rows = {}

for lang_code in langs:
	loc_path = loc_paths.format(lang_code)

	print('Reading loc file... ' + loc_path)

	lines = [line.encode('utf-8').rstrip('\n') for line in open(loc_path)]
	for line in lines:
		# print line
		pattern = re.compile(r'\"(.*)\" = \"(.*)\"')
		match = pattern.search(line)
		if match:
			loc_key = match.group(1).encode('utf-8')
			loc_str = match.group(2).encode('utf-8')
	        if loc_key in rows.keys():
	            rows[loc_key].append(loc_str)
	        else:
	            rows[loc_key] = [loc_key, loc_str]

rows_orderd = OrderedDict(sorted(rows.items()))

print('Saving tsv file... ' + tsv_path)
print('')

with open(tsv_path, 'wb') as f:
    writer = csv.writer(f, csv.excel_tab)
    writer.writerows(rows_orderd.values())

print('All done!')

Convert Localizable.tsv to Localizable.strings

i18n_tsv2po.py



import sys
import unicodecsv as csv
import codecs
from collections import OrderedDict

#
# Default encoding
#

reload(sys)
sys.setdefaultencoding('utf8')

#
# File paths
#

tsv_path = 'Localization/Localizable.tsv'
loc_paths = 'PROJECT_NAME/PROJECT_NAME/Resources/UI/{}.lproj/Localizable.strings'

#
# Read Localizable.tsv
#

langs = {
    'ja': {},
    'en': {},
}

print('Reading tsv file... ' + tsv_path)

with open(tsv_path, 'rb') as tsv_in:
    tsv_in = csv.reader(tsv_in, delimiter='\t')
    # next(tsv_in, None)
    for row in tsv_in:
        loc_key = row[0]
        if row[1]:
            loc_str = row[1].encode('utf-8')
            langs['ja'][loc_key] = loc_str
        if row[2]:
            loc_str = row[2].encode('utf-8')
            langs['en'][loc_key] = loc_str

#
# Save Localizable.string
#

print('')

for key, value in langs.iteritems():
    lang_code = key
    loc_path = loc_paths.format(lang_code)

    print('Saving loc file... ' + loc_path)

    with open(loc_path, 'w') as loc_out:
        loc_orderd = OrderedDict(sorted(value.items()))
        for loc_key, loc_str in loc_orderd.iteritems():
            loc_out.write('"' + loc_key + '" = "' + loc_str +  '";\n')
        loc_out.close()

    print('Done!')
    print('')

print('All done!')

Recommended Posts

Script to convert between Xcode language files and tab-delimited text
A script that converts between Django language files (PO) and tab-separated text (TSV)
Method to convert between Western calendar and Japanese calendar
Convert HTML to text file
Demosaic Bayer FITS files and convert them to color TIFF
Convert a large number of PDF files to text files using pdfminer
I made a script in python to convert .md files to Scrapbox format
Convert PDF attached to email to text format
Convert PDF files to PNG files with GIMP
Convert HEIC files to PNG files with Python
Create an API to convert PDF files to TIF images with FastAPI and Docker
I want to convert horizontal text to vertical text and post it on Twitter etc.