Skip to content

Instantly share code, notes, and snippets.

@felix-larsen
Created April 16, 2023 09:43
Show Gist options
  • Save felix-larsen/3e99388737aa3ffdf656e862cbd91ccb to your computer and use it in GitHub Desktop.
Save felix-larsen/3e99388737aa3ffdf656e862cbd91ccb to your computer and use it in GitHub Desktop.
Script to convert strings xml from android app to a json file used in flutter using the easy localization package
import os
import json
import xml.etree.ElementTree as ET
def xml_to_json(xml_file):
tree = ET.parse(xml_file)
root = tree.getroot()
strings = {}
for child in root:
if child.tag == 'string':
name = child.attrib['name']
text = child.text
strings[name] = text
return strings
def main():
# TODO change base dir
base_dir = '/Users/felix/code/xyz'
json_dir = 'json_files'
if not os.path.exists(json_dir):
os.makedirs(json_dir)
for root, dirs, files in os.walk(base_dir):
if 'strings.xml' in files:
xml_file = os.path.join(root, 'strings.xml')
strings = xml_to_json(xml_file)
language = root.split('/')[-1]
json_file = os.path.join(json_dir, f'{language}.json')
with open(json_file, 'w') as f:
json.dump(strings, f)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment