Skip to content

Instantly share code, notes, and snippets.

@DennyWeinberg
Created February 14, 2023 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DennyWeinberg/df9cdcd14f112f200d8507ed45a5cd75 to your computer and use it in GitHub Desktop.
Save DennyWeinberg/df9cdcd14f112f200d8507ed45a5cd75 to your computer and use it in GitHub Desktop.
Print the android strings.xml with it's items name, original (en) value and specific language values with a \t separator, so we can copy paste them into a sheet or create a CSV file to manual translations
from glob import glob
from os.path import join, exists, basename, dirname
from lxml import etree
# Input
res_path = r'/Users/dennyweinberg/git-clones/android-photos/app/src/main/res'
languages_to_extract = ['', 'it']
def get_translations(incl_non_translatable):
languages = []
translations_dict = {}
# Loop over possible languages/values folders
for folder in glob(join(res_path, 'values*')):
strings_xml_filepath = join(folder, 'strings.xml')
strings_xml_filename = basename(strings_xml_filepath)
# Check if language file exists in the folder
if not exists(strings_xml_filepath): continue
# Extract language code if possible
strings_xml_folder_name = basename(dirname(strings_xml_filepath))
language = strings_xml_folder_name.split('-')[1] if '-' in strings_xml_folder_name else ''
# Language filter
if languages_to_extract and language not in languages_to_extract:
continue
# Add to language list
if language not in languages:
languages.append(language)
# Open XML file
tree = etree.parse(strings_xml_filepath)
# Loop over all translations
for item in etree.parse(strings_xml_filepath).getroot():
# Check if we need to skip non translatable items
if item.attrib.get('translatable', True) == 'false' and not incl_non_translatable: continue
# Add to items
translations_dict.setdefault(item.attrib['name'], {})
translations_dict[item.attrib['name']][language] = item.text
# Sort languages
languages = sorted(languages)
return languages, translations_dict
# Run it
languages, translations_dict = get_translations(False)
# Print
sep = '\t'
print(f'Key{sep}{sep.join(languages)}')
for key, language_dict in translations_dict.items():
print(f'{key}{sep}{sep.join([language_dict.get(language, "") for language in languages])}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment