Ein Skript, das Django-Sprachdateien in einfach zu handhabenden tabulatorgetrennten Text konvertiert. Die Referenzquelle ist ein Stackoverflow-Thread. Is there any program to edit multiple po files? Ändern Sie die CSV des Referrer-Codes in TSV und fügen Sie ein Skript hinzu, um die Bestellung von TSV zu aktualisieren. Beachten Sie, dass bei der Konvertierung von TSV in PO nur die in der PO-Datei vorhandene msgid überschrieben wird. Sie wird daher möglicherweise nicht aktualisiert, wenn die Elemente in PO und TSV nicht ausgerichtet sind.
bash
pip install polib unicodecsv
i18n_po2tsv.py
import sys
import unicodecsv as csv
from collections import OrderedDict
import polib
#
# Set default encoding
#
reload(sys)
sys.setdefaultencoding('utf8')
#
# Params
#
tsv_path = 'locale/localization.tsv'
po_paths = 'locale/{}/LC_MESSAGES/django.po'
langs = ['en', 'ja',]
#
# Convert po file to tsv
#
rows = OrderedDict()
print('')
for lang_code in langs:
po_path = po_paths.format(lang_code)
print('Reading po file... ' + po_path)
po = polib.pofile(
po_path,
encoding='utf-8',
)
for entry in po:
msgid = entry.msgid.encode('utf-8')
msgstr = entry.msgstr.encode('utf-8')
if msgid in rows.keys():
rows[msgid].append(msgstr)
else:
rows[msgid] = [msgid, msgstr]
print('Saving tsv file... ' + tsv_path)
print('')
with open(tsv_path, 'wb') as f:
writer = csv.writer(f, csv.excel_tab)
writer.writerows(rows.values())
print('All done!')
print('')
i18n_tsv2po.py
import sys
import unicodecsv as csv
import codecs
import polib
#
# Set system encoding
#
reload(sys)
sys.setdefaultencoding('utf8')
#
# Params
#
tsv_path = 'locale/localization.tsv'
po_paths = 'locale/{}/LC_MESSAGES/django.po'
#
# Read tsv file
#
langs = {
'en': {},
'ja': {},
}
print('')
print('Reading tsv file... ' + tsv_path)
with open(tsv_path, 'rb') as tsv_in:
tsv_in = csv.reader(tsv_in, delimiter='\t')
# next(tsv_in, None)
for row in tsv_in:
if row[1]:
langs['en'][row[0]] = row[1].encode('utf-8')
if row[2]:
langs['ja'][row[0]] = row[2].encode('utf-8')
#
# Read and save po file
#
print('')
for key, value in langs.iteritems():
lang_code = key
po_path = po_paths.format(lang_code)
po = polib.pofile(
po_path,
encoding='utf-8',
)
print('Reading po file... ' + po_path)
lang = langs[lang_code]
for entry in po:
msgid = entry.msgid.encode('utf-8')
if lang.has_key(msgid):
msgstr = lang.get(msgid, '').encode('utf-8')
entry.msgstr = msgstr
print('Saving po file... ' + po_path)
po.save()
print('Done!')
print('')
print('All done!')
print('')
Recommended Posts