Skip to content

Instantly share code, notes, and snippets.

@lognaturel
Last active December 14, 2020 21:38
Show Gist options
  • Save lognaturel/9974fab4e7579fac034511cd4944176b to your computer and use it in GitHub Desktop.
Save lognaturel/9974fab4e7579fac034511cd4944176b to your computer and use it in GitHub Desktop.
Quick Python script to pull translations downloaded from Transifex to the ODK Collect Android project
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import shutil
def replace_ellipses_with_special_char(file_path):
with open(file_path, 'r') as file :
filedata = file.read()
filedata = filedata.replace('...', '…')
with open(transifex_file, 'w') as file:
file.write(filedata)
# From Transifex
source_directory = "getodk_collect_strings"
source_filename_structure = "strings_(.*).xml"
# Android project
target_directory = "collect"
strings_location = "/strings/src/main/res"
# Although most country abbreviations match exactly or have a predictable correspondence,
# having an explicit list means it can be inspected and that new languages from Transifex
# will be noticed.
transifex_to_android = {
"af": "af",
"am": "am",
"ar": "ar",
"bn": "bn",
"ca": "ca",
"cs": "cs",
"da": "da",
"de": "de",
"es": "es",
"et": "et",
"fa": "fa",
"fi": "fi",
"fil": "fil",
"fr": "fr",
"ha": "ha",
"hi": "hi",
"hu": "hu",
"id": "in", # Java bug
"it": "it",
"ja": "ja",
"ka": "ka",
"km": "km",
"lo_LA": "lo-rLA",
"ln": "ln",
"lt": "lt",
"mg": "mg",
"ml": "ml",
"ms_MY": "ms",
"mr": "mr",
"my": "my",
"nb": "nb",
"ne_NP": "ne-rNP",
"nl": "nl",
"no": "no",
"pl_PL": "pl",
"ps": "ps",
"pt": "pt",
"ro": "ro",
"ru": "ru",
"si": "si",
"sl": "sl",
"so": "so",
"sq": "sq",
"sr": "sr",
"sv_SE": "sv-rSE",
"sw_KE": "sw-rKE",
"sw": "sw",
"ta": "ta",
"te": "te",
"th_TH": "th-rTH",
"ti": "ti",
"tl": "tl",
"tr": "tr",
"uk": "uk",
"ur": "ur",
"uz": "uz",
"ur_PK": "ur-rPK",
"vi": "vi",
"zh": "zh",
"zu": "zu"
}
for filename in os.listdir(source_directory):
regex = re.compile(source_filename_structure)
transifex_language_code_match = regex.match(filename)
if transifex_language_code_match:
transifex_language_code = transifex_language_code_match.group(1)
if transifex_language_code in transifex_to_android:
transifex_file = source_directory + "/" + filename
replace_ellipses_with_special_char(transifex_file)
android_language_code = transifex_to_android[transifex_language_code]
android_folder = target_directory + strings_location + "/values-" + android_language_code
shutil.move(transifex_file, android_folder + "/strings.xml")
print("Moved " + filename)
else:
print(transifex_language_code + " not found")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment