Un script qui convertit les fichiers de langue Django en texte délimité par des tabulations facile à manipuler. La source de référence est un thread Stackoverflow. Is there any program to edit multiple po files? Changez le CSV du code référent en TSV et ajoutez un script pour mettre à jour PO à partir de TSV. Notez que la conversion de TSV en PO écrase uniquement le msgid qui existe dans le fichier PO, il peut donc ne pas être mis à jour si les éléments ne sont pas alignés dans PO et TSV.
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