LoginSignup
2
2

More than 5 years have passed since last update.

Djangoの言語ファイル(PO)とタブ区切りテキスト(TSV)を相互変換するスクリプト

Last updated at Posted at 2015-11-17

Djangoの言語ファイルをハンドリングしやすいタブ区切りテキストと相互変換するスクリプト。参照元はStackoverflowのスレッド。
Is there any program to edit multiple po files?
参照元のコードのCSVをTSVに変更し、TSVからPOを更新するスクリプトを追加。
TSVからPOの変換は、POファイルに存在するmsgidのみ上書きするので、POとTSVで要素が揃っていないと更新されないケースがあるので注意。

Pythonモジュールのインストール

bash

pip install polib unicodecsv

POからTSVに変換

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('')

TSVからPOに変換

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('')

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2