Skip to content

Instantly share code, notes, and snippets.

@diaowinner
Last active January 1, 2026 15:25
Show Gist options
  • Select an option

  • Save diaowinner/f8f0d1e18bd6b3845cd74937b1370aa6 to your computer and use it in GitHub Desktop.

Select an option

Save diaowinner/f8f0d1e18bd6b3845cd74937b1370aa6 to your computer and use it in GitHub Desktop.
Generate PO file to "formatted" strings

WARNING: Containing nearly undocumented stuff.


Copyright (C) 2026 by Poxiao Labs poxiao-labs@proton.me

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

from pyicumessageformat import Parser
import polib
def _flatten_data(data, path=""):
items = []
if isinstance(data, dict):
for key, value in data.items():
new_path = f"{path}.{key}" if path else key
items.extend(_flatten_data(value, new_path))
elif isinstance(data, list):
for index, value in enumerate(data):
new_path = f"{path}.{index}"
items.extend(_flatten_data(value, new_path))
else:
items.append((path, data))
return items
def _flatten_mf(mf):
parser = Parser()
ast = parser.parse(mf)
return _flatten_data(ast)
def _gen_tsv(fast):
result = []
for i in fast:
if i[0][-1] in "0123456789":
result.append(i[1])
return "\t".join(result)
splat = lambda v: v + "\t" + _gen_tsv(_flatten_mf(v)) + "\n"
def main(pofile, out):
pofile = polib.pofile(pofile)
with open(out, "w", encoding="gb18030") as f:
for entry in pofile:
f.write(splat(entry.msgid))
import sys
if __name__ == "__main__":
pofile = "messages.po"
out = "messages.txt"
if len(sys.argv) > 1:
pofile = sys.argv[1]
if len(sys.argv) > 2:
out = sys.argv[2]
main(pofile, out)
from json import dumps
def main(m2, m3, out):
d = {}
with open(m2, encoding="gb18030") as f:
with open(m3, encoding="utf8") as g:
for fl in f:
gl = g.readline()
flt = fl.split("\t")
glt = gl.split("\t")
d[flt[0]] = {}
for i in range(1, len(flt)):
flti = flt[i]
glti = glt[i]
d[flt[0]][flti.rstrip()] = glti
# print(d)
rr = {}
for i in d:
def _replace(s, e):
r = s
sk = sorted(e.keys(), key=len, reverse=True)
for j in sk:
# print(r)
k = e[j]
r = r.replace(j, k)
return r
rr[i] = _replace(i, d[i])
print(rr)
with open(out, "w", encoding="utf8") as f:
for i in rr:
j = rr[i]
f.write(f"\nmsgid {dumps(i)}\nmsgstr {dumps(j)}\n")
import sys
if __name__ == "__main__":
m2 = "messages.txt"
m3 = "messages3.txt"
out = "final.po"
if len(sys.argv) > 1:
m2 = sys.argv[1]
if len(sys.argv) > 2:
m3 = sys.argv[2]
if len(sys.argv) > 3:
out = sys.argv[3]
main(m2, m3, out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment