Skip to content

Commit

Permalink
config to 20scoops.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jedsada Tiwongvorakul committed Dec 13, 2017
1 parent 8df82f0 commit 3988fea
Showing 1 changed file with 39 additions and 20 deletions.
59 changes: 39 additions & 20 deletions csv2xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,49 @@
roots = []
current_path = os.getcwd()
is_have_arg = False
index_arg = 0

for row in reader:
if row[0] == "key":
translations = row
translations.pop(0)
break

if "arg" in translations :
if "arg" in translations:
is_have_arg = True
index_arg = translations.index("arg")
translations.remove("arg")

for translation in translations:
if "_android" in translations:
is_have_arg = True
index_arg = translations.index("_android")
translations.remove("_android")

translations = list(filter(lambda x: len(x) == 2, translations))

for item in translations:
roots.append(etree.Element("resources"))

def set_name_value_and_comment(count_loop) :
def set_name_value_and_comment(count_loop):
for i in range(0, count_loop):
if "#" in row[0]:
comment_message = row[0].split("#")[1].decode("utf-8")
comment = etree.Comment("=" * 15 + " "+comment_message + " " +"=" * 15)
roots[i].append(comment)
elif len(str(row[0])) > 1:
string_result = row[i + 1].decode('utf-8')
result_arg = row[len(row) - 1].split("=")
arg_traget = "{"+result_arg[0]+"}"
if is_have_arg and arg_traget in string_result:
string_result = string_result.replace(arg_traget, result_arg[1])
string_resource = etree.SubElement(roots[i], "string")
string_resource.set("name", row[0])
string_resource.text = string_result
if "#" in row[0]:
comment_message = row[0].split("#")[1].decode('utf-8')
comment = etree.Comment(
"=" * 15 + " " + comment_message + " " + "=" * 15)
roots[i].append(comment)
elif len(str(row[0])) > 1:
string_result = row[i + 1].decode('utf-8')
if len(row) > index_arg:
result_arg = row[index_arg + 1].split("=")
else:
this.is_have_arg = False
arg_traget = "{" + result_arg[0].decode('utf-8') + "}"
if is_have_arg and arg_traget in string_result:
string_result = string_result.replace(arg_traget,
result_arg[1])
string_resource = etree.SubElement(roots[i], "string")
string_resource.set("name", row[0])
string_resource.text = string_result

for row in reader:
if row[0] != "key":
Expand All @@ -48,11 +62,16 @@ def set_name_value_and_comment(count_loop) :

for i in range(0, len(translations)):
android_dir = current_path + "/app/src/main/res/values"
if translations[i] != "en" :
android_dir += "-"+translations[i].lower()
if translations[i] != "en":
android_dir += "-" + translations[i].lower()
if not os.path.exists(android_dir):
os.makedirs(android_dir)
xml_string = etree.tostring(roots[i], encoding='unicode', method='xml', pretty_print=True, xml_declaration=False)
os.makedirs(android_dir)
xml_string = etree.tostring(
roots[i],
encoding='unicode',
method='xml',
pretty_print=True,
xml_declaration=False)
pretty_xml_string = xml_file_header
pretty_xml_string += xml_string.replace("<!--=======", "\n <!--=======")
xml_file = open(android_dir + "/strings.xml", "w")
Expand Down

0 comments on commit 3988fea

Please sign in to comment.