import polib
po = polib.POFile()
po.metadata = {'Language': 'en', 'Content-Type': 'text/plain; charset=UTF-8', 'Plural-Forms': 'nplurals=2; plural=(n != 1);'}
for row in output['translations']:
    source = next(item for item in data['entries'] if item['context'] == row['context'])
    placeholders = set(re.findall(r'%\([a-z]+\)[sd]', source['msgid']))
    check('翻译占位符完整：' + row['context'], all(set(re.findall(r'%\([a-z]+\)[sd]', text)) == placeholders for text in row['msgstr']))
    item = polib.POEntry(msgctxt=row['context'], msgid=row['msgid'])
    if row['msgid_plural']:
        item.msgid_plural = row['msgid_plural']
        item.msgstr_plural = {index: text for index, text in enumerate(row['msgstr'])}
    else:
        item.msgstr = row['msgstr'][0]
    po.append(item)
po.save(str(folder / 'messages.po'))
po.save_as_mofile(str(folder / 'messages.mo'))
check('PO和MO均可实际回读', len(polib.pofile(str(folder / 'messages.po'))) == 2 and len(polib.mofile(str(folder / 'messages.mo'))) == 2)
