| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,8 +1,11 @@ | |||
| 1 | + import io | ||
| 1 | 2 | import os | |
| 2 | 3 | import pathlib | |
| 3 | 4 | import shutil | |
| 5 | + import string | ||
| 4 | 6 | import subprocess | |
| 5 | 7 | ||
| 8 | + from babel.messages.pofile import read_po, write_po | ||
| 6 | 9 | import fire | |
| 7 | 10 | ||
| 8 | 11 | MSG_DIR = pathlib.Path(__file__).parent.parent.parent | |
@@ -40,6 +43,14 @@ def chdir_Doc(): | |||
| 40 | 43 | os.chdir(MSG_DIR / "../cpython/Doc") | |
| 41 | 44 | ||
| 42 | 45 | ||
| 46 | + def remove_nonprintables(text): | ||
| 47 | + nps = ''.join(sorted(set(chr(i) | ||
| 48 | + for i in range(128)) - set(string.printable))) | ||
| 49 | + table = str.maketrans(nps, nps[0] * len(nps)) | ||
| 50 | + text = text.translate(table).replace(nps[0], '') | ||
| 51 | + return text.lstrip() | ||
| 52 | + | ||
| 53 | + | ||
| 43 | 54 | class Command: | |
| 44 | 55 | def init(self): | |
| 45 | 56 | """Initialize .pdk.""" | |
@@ -63,15 +74,48 @@ def watch(self): | |||
| 63 | 74 | sh("make -e SPHINXOPTS=\"-D language='ko'\" htmllive") | |
| 64 | 75 | ||
| 65 | 76 | def extract(self): | |
| 66 | - """Extract translatable messages into pot files.""" | ||
| 77 | + """Extract translatable messages into .pot files.""" | ||
| 67 | 78 | chdir_Doc() | |
| 68 | 79 | sh("make gettext") | |
| 69 | 80 | ||
| 70 | 81 | def update(self): | |
| 71 | - """Apply the updates from pot files to po files.""" | ||
| 82 | + """Apply the updates from pot files to .po files.""" | ||
| 72 | 83 | chdir_Doc() | |
| 73 | 84 | sh("sphinx-intl update -p build/gettext -l ko") | |
| 74 | 85 | ||
| 86 | + def format(self, pofile): | ||
| 87 | + """Format a .po file.""" | ||
| 88 | + with open(pofile) as f: | ||
| 89 | + idata = f.read() | ||
| 90 | + f = io.StringIO(idata) | ||
| 91 | + catalog = read_po(f, abort_invalid=True) | ||
| 92 | + | ||
| 93 | + for msg in catalog: | ||
| 94 | + if not msg.id or not msg.string or msg.fuzzy: | ||
| 95 | + continue | ||
| 96 | + msg.string = remove_nonprintables(msg.string) | ||
| 97 | + | ||
| 98 | + f = io.BytesIO() | ||
| 99 | + write_po(f, catalog) | ||
| 100 | + odata = f.getvalue() | ||
| 101 | + if idata.encode() != odata: | ||
| 102 | + with open(pofile, 'wb') as f: | ||
| 103 | + f.write(odata) | ||
| 104 | + else: | ||
| 105 | + print('already formatted') | ||
| 106 | + fuzzy_count = empty_count = 0 | ||
| 107 | + for msg in catalog: | ||
| 108 | + if not msg.id: | ||
| 109 | + continue | ||
| 110 | + if msg.fuzzy: | ||
| 111 | + fuzzy_count += 1 | ||
| 112 | + elif not msg.string: | ||
| 113 | + empty_count += 1 | ||
| 114 | + if fuzzy_count: | ||
| 115 | + print(f'{fuzzy_count} fuzzy messages found') | ||
| 116 | + if empty_count: | ||
| 117 | + print(f'{empty_count} untranslated messages found') | ||
| 118 | + | ||
| 75 | 119 | ||
| 76 | 120 | def main(): | |
| 77 | 121 | fire.Fire(Command) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments