Last active
October 29, 2017 21:10
-
-
Save MCOfficer/ef3e937b4182dd60d3dd05c7b72508ea to your computer and use it in GitHub Desktop.
Translates Java Code in the working directory and all subdirectories to a specific language (e.g. German). The results are usually hilarious.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from translate_api.translate_api import api | |
import re | |
import glob | |
import os | |
cache = dict() | |
def camelCaseSplit(identifier): | |
matches = re.finditer('.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)', identifier) | |
return [m.group(0) for m in matches] | |
def translate(word): | |
words = camelCaseSplit(word) | |
translated = [] | |
for x in words: | |
if x not in cache: | |
translation = api(x, "en", targetLang, "https://translate.google.com") | |
if x[0].isupper(): | |
translated.append(translation.title()) | |
else: | |
translated.append(translation) | |
cache[x] = translation | |
else: | |
translated.append(cache[x]) | |
return "".join(translated) | |
targetLang = input("Enter the target language (e.g 'de'):") | |
print("Starting. This may take a while.") | |
for f in glob.iglob(os.getcwd() + "/**/*.java", recursive=True): | |
print("opening file " + f) | |
with open(f, "r") as file: | |
text = file.read() | |
lines = text.splitlines() | |
finaltext = [] | |
for i in range(len(lines)): | |
print("translating line " + str(i + 1) + " of " + str(len(lines)) + "...") | |
line = lines[i].split() | |
for j in range(len(line)): | |
line[j] = translate(line[j]) | |
lines[i] = " ".join(line).strip() | |
print("done, saving...") | |
with open(f, 'w') as file: | |
file.write("\n".join(lines).strip()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment