Skip to content

Instantly share code, notes, and snippets.

@JulienPalard
Created October 5, 2018 10:17
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 JulienPalard/c430ac23446da2081060ab17bf006ac1 to your computer and use it in GitHub Desktop.
Save JulienPalard/c430ac23446da2081060ab17bf006ac1 to your computer and use it in GitHub Desktop.
Find a word in msgids, show msgstrs.
#!/usr/bin/env python3
import argparse
from glob import glob
import os
from textwrap import fill
import regex
import polib
from tabulate import tabulate
def find_in_po(pattern):
table = []
try:
_, columns = os.popen("stty size", "r").read().split()
available_width = int(columns) // 2 - 3
except:
available_width = 80 // 2 - 3
for file in glob("**/*.po"):
pofile = polib.pofile(file)
for entry in pofile:
if entry.msgstr and regex.search(pattern, entry.msgid):
table.append(
[
fill(entry.msgid, width=available_width),
fill(entry.msgstr, width=available_width),
]
)
print(tabulate(table, tablefmt="fancy_grid"))
def parse_args():
parser = argparse.ArgumentParser(description="Find translated words.")
parser.add_argument("pattern")
return parser.parse_args()
def main():
args = parse_args()
find_in_po(args.pattern)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment