Last active
March 23, 2018 22:14
-
-
Save dneprDroid/b850c6ccb0795216930b5a82ef54ff5b to your computer and use it in GitHub Desktop.
Import localization strings from xls (Excel) file (for ios and android).
This file contains 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
print('Usage (where PATH is .xlsx file): python import_localization.py <PATH> <LANG_COLUMN_NAME>') | |
# python import_localization.py spreadsheet-3.xlsx "Eng Value" | |
try: | |
import pandas as pd | |
except: | |
print('Install pandas!!!!') | |
print('Try: pip install pandas') | |
raise | |
import os | |
import sys | |
def create_lang_file(path): | |
try: | |
os.remove(path) | |
except OSError: | |
pass | |
try: | |
file = open(path, 'r') | |
except IOError: | |
file = open(path, 'w') | |
return file | |
def write_ios(file, sheetX, lang_id): | |
id_column = sheetX['ID'] | |
eng_column = sheetX[lang_id] | |
for i in range(len(id_column)): | |
id = id_column[i] | |
eng_value = eng_column[i] | |
if type(id) != unicode or (not id and not eng_value): | |
continue | |
if id.startswith( '//' ): | |
ios_line = id + '\n' | |
else: | |
ios_line = ' "%s" = "%s";\n' % (id, eng_value) | |
#print('New Line: ', ios_line) | |
file.write(ios_line) | |
file.close() | |
print('Saved ios file.') | |
def write_android(file, sheetX, lang_id): | |
file.write('<resources>\n') | |
id_column = sheetX['ID'] | |
eng_column = sheetX[lang_id] | |
for i in range(len(id_column)): | |
id = id_column[i] | |
eng_value = eng_column[i] | |
if type(id) != unicode or (not id and not eng_value): | |
continue | |
if id.startswith( '//' ): | |
android_line = ' <!--%s-->\n' % ( id.replace('//', '')) | |
else: | |
android_line = ' <string name="%s"> %s </string>\n' % (id, eng_value) | |
#print('New Line: ', android_line) | |
file.write(android_line) | |
file.write('</resources>') | |
file.close() | |
print('Saved android file.') | |
path = sys.argv[1] | |
lang_id = sys.argv[2] | |
print('Using lang column id="%s"' % lang_id) | |
if os.path.exists(path): | |
print os.path.basename(path) | |
print('Path: ', path) | |
xls = pd.ExcelFile(path) | |
sheetX = xls.parse(0) #2 is the sheet number | |
android_output_name = path + '-Android.xml' | |
ios_output_name = path + '-iOS.strings' | |
android_file = create_lang_file(android_output_name) | |
ios_file = create_lang_file(ios_output_name) | |
write_android(android_file, sheetX, lang_id) | |
write_ios(ios_file, sheetX, lang_id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment