Skip to content

Instantly share code, notes, and snippets.

@brandones
Created May 5, 2020 22:57
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 brandones/ad7ae170e3693a40f83cbc2faecc9994 to your computer and use it in GitHub Desktop.
Save brandones/ad7ae170e3693a40f83cbc2faecc9994 to your computer and use it in GitHub Desktop.
Remove strings matching the english string from a messages_LL.properties file
#!/usr/bin/env python3
import argparse
import re
def main(lang):
en_strings = properties_to_dict(read_file("en"))
t_strings = properties_to_dict(read_file(lang))
translated = {k: t_strings[k] for k in t_strings if en_strings[k] != t_strings[k]}
pairs = sorted(translated.items(), key=lambda t: t[0])
outfile_name = "./messages_" + lang + ".properties"
with open(outfile_name, "w") as outfile:
for k, v in pairs:
outfile.write(k + "=" + v + "\n")
print("Written to " + outfile_name)
def read_file(lang):
text = open("./messages_" + lang + ".properties").read()
text = re.sub(r"\s*\\\n\s*", " ", text)
lines = text.split("\n")
lines = [l.strip() for l in lines if l.strip() and not l.strip().startswith("#")]
return lines
def properties_to_dict(lines):
return {
s.split("=")[0].strip(): (
s.split("=")[1].strip() if len(s.split("=")) > 1 else ""
)
for s in lines
}
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Remove translation strings if they match the English string. Overwrites existing file."
)
parser.add_argument("language", help="e.g. 'es' for messages_es.properties")
args = parser.parse_args()
main(args.language)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment