Skip to content

Instantly share code, notes, and snippets.

@amandaroos
Created June 21, 2020 02:35
Show Gist options
  • Save amandaroos/d647167c1aabf199db5fc1a6844719f6 to your computer and use it in GitHub Desktop.
Save amandaroos/d647167c1aabf199db5fc1a6844719f6 to your computer and use it in GitHub Desktop.
#Open string resource file, remove last line, add new translated line, for all language codes provided
from TranslationsFunctionReturnDictionary import translate
print ("starting!")
###UPDATE words_to_translate, codes, and myBasePath if necessary###
#word : resource name
words_to_translate = {
"Change Calculator":"app_name"
}
codes = ['af','am','ar','az','be','bg','bn','bs','ca','ceb','co','cs','cy','da','de','el','eo','es','et','eu','fa','fi','fr','fy','ga','gd','gl','gu','ha','haw','iw','hi','hmn','hr','ht','hu','hy','id','ig','is','it','ja','jw','ka','kk','km','kn','ko','ku','ky','la','lb','lo','lt','lv','mg','mi','mk','ml','mn','mr','ms','mt','my','ne','nl','no','ny','pa','pl','ps','pt','ro','ru','sd','si','sk','sl','sm','sn','so','sq','sr','st','su','sv','sw','ta','te','tg','th','tl','tr','uk','ur','uz','vi','xh','yi','yo','zh-CN','zh-TW']
myBasePath = r"path to tranlsations folder"
for key in words_to_translate:
translations = translate(key, codes)
for code in translations:
myPath = myBasePath + r"\values-" + code + r"\strings.xml"
#exceptions where language code is different between Android file name and Google Translate
if code == "tl":
myPath = myBasePath + r"\values-fil\strings.xml"
elif code == "id":
myPath = myBasePath + r"\values-in\strings.xml"
elif code == "he":
myPath = myBasePath + r"\values-iw\strings.xml"
elif code == "jw":
myPath = myBasePath + r"\values-jv\strings.xml"
elif code == "yi":
myPath = myBasePath + r"\values-ji\strings.xml"
elif code == "zh-CN":
myPath = myBasePath + r"\values-zh-rCN\strings.xml"
elif code == "zh-TW":
myPath = myBasePath + r"\values-zh-rTW\strings.xml"
elif code == "ceb":
myPath = myBasePath + r"\values-b+ceb\strings.xml"
elif code == "hmn":
myPath = myBasePath + r"\values-b+hmn\strings.xml"
elif code == "haw":
myPath = myBasePath + r"\values-b+haw\strings.xml"
file = open(myPath, mode = "r", encoding="utf8")
lines = file.readlines()
file.close()
if len(lines) > 1:
lines = lines[:-1]
text = ""
for line in lines:
text += line
#add new translation to text
text += " <string name=\"" + words_to_translate[key] +"\">" + translations[code].replace("'",r"\'")+ "</string> \n </resources>"
file = open(myPath, mode = "w", encoding="utf8")
file = file.write(text)
print ("Translation complete: " + key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment