Skip to content

Commit

Permalink
Build plurar strings in resulting po.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Skiepko committed May 12, 2017
1 parent a0e2ece commit 00fd5a4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
26 changes: 22 additions & 4 deletions dila/application/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,33 @@
def get_po_file(language_code, resource_pk):
po = polib.POFile()
for string in data.get_translated_strings(language_code, resource_pk):
po.append(polib.POEntry(
po.append(build_po_entry(string))
return str(po)


def build_po_entry(string):
if string.plural:
return polib.POEntry(
msgid=string.base_string,
msgid_plural=string.plural,
msgstr_plural={
'0': string.translation,
'1': string.plural_translations.few,
'2': string.plural_translations.many,
'3': string.plural_translations.other,
},
msgctxt=string.context,
comment=string.comment,
tcomment=string.translator_comment,
)
else:
return polib.POEntry(
msgid=string.base_string,
msgstr=string.translation,
msgctxt=string.context,
comment=string.comment,
tcomment=string.translator_comment,
))
return str(po)

)

def upload_po_file(resource_pk, content, translated_language_code=None):
po = polib.pofile(content)
Expand Down
37 changes: 37 additions & 0 deletions tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@
msgstr "Żółć"
'''

test_result_plural_po = '''#
msgid ""
msgstr ""
#. Programmer comment
msgctxt ""
msgid "%d option"
msgid_plural "%d options"
msgstr[0] "%d opcję"
msgstr[1] "%d opcje"
msgstr[2] "%d opcji"
msgstr[3] "%d opcji"
'''


@mock.patch('dila.data.add_resource')
def test_add_resource(add_resource):
Expand Down Expand Up @@ -227,3 +241,26 @@ def test_get_po_file(get_translated_strings):
]
result = application.get_po_file('pl', '1')
assert result == test_result_po


@mock.patch('dila.data.get_translated_strings')
def test_get_po_file_with_plural_translations(get_translated_strings):
get_translated_strings.return_value = [
structures.TranslatedStringData(
pk='34',
base_string='%d option',
plural='%d options',
translation='%d opcję',
comment='Programmer comment',
translator_comment='',
context='',
resource_pk='1',
plural_translations=structures.PluralTranslations(
few='%d opcje',
many='%d opcji',
other='%d opcji',
),
)
]
result = application.get_po_file('pl', '1')
assert result == test_result_plural_po

0 comments on commit 00fd5a4

Please sign in to comment.