Last active
September 19, 2022 14:30
-
-
Save tomkralidis/cc5edd18b0d807d870e7c11615ff484c to your computer and use it in GitHub Desktop.
Python PO to CSV to PO roundtripping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unicodecsv as csv | |
import sys | |
import polib | |
def po2csv(po_file): | |
po = polib.pofile(po_file) | |
with open('messages.csv', 'wb') as csvfile: | |
writer = csv.writer(csvfile) | |
writer.writerow(['English']) | |
for entry in po: | |
writer.writerow([entry.msgid, entry.msgstr]) | |
def csv2po(csv_file): | |
po = polib.POFile() | |
with open(csv_file) as csvfile: | |
reader = csv.reader(csvfile) | |
for row in reader: | |
entry = polib.POEntry( | |
msgid=row[0], | |
msgstr=row[1] | |
) | |
po.append(entry) | |
po.save('out.po') | |
if __name__ == '__main__': | |
if len(sys.argv) < 3: | |
print('Usage: {} <po2csv|csv2po> <input_file>'.format(sys.argv[0])) | |
sys.exit(1) | |
if sys.argv[1] == 'po2csv': | |
po2csv(sys.argv[2]) | |
elif sys.argv[1] == 'csv2po': | |
csv2po(sys.argv[2]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
polib | |
unicodecsv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replace:
writer.writerow([entry.msgid])
By:
writer.writerow([entry.msgid, entry.msgstr])