Skip to content

Instantly share code, notes, and snippets.

@mmaridev
Created March 19, 2019 12:22
Show Gist options
  • Select an option

  • Save mmaridev/8df38f408d08abb220443959abb4f9b2 to your computer and use it in GitHub Desktop.

Select an option

Save mmaridev/8df38f408d08abb220443959abb4f9b2 to your computer and use it in GitHub Desktop.
Translating django.po files using external tools
#!/usr/bin/python
a = open("django.po")
h = [x.strip() for x in a.readlines()]
a.close()
counter = 0
while counter < len(h):
pre = counter
line = h[counter]
if line.startswith("msgid "):
counter += 1
while not h[counter].startswith("msgstr "):
counter += 1
if h[counter] == "msgstr \"\"" and not h[counter+1].startswith('"'):
print(counter, h[pre][7:-1])
counter += 1
#!/usr/bin/python
a = open("django.po")
h = [x.strip() for x in a.readlines()]
a.close()
a = open("load.txt")
t = [x.strip() for x in a.readlines()]
a.close()
for line in t:
# just place the translated string
num = int(line.split(" ")[0])+1
txt = " ".join(line.split(" ")[1:])
if txt.strip():
h[num] = "msgstr \"%s\"" % txt
a = open("django.po.new", "w")
for line in h:
a.write(line)
a.write("\n")
a.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment