Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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