Skip to content

Commit

Permalink
Add way to translate pluralized strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Skiepko committed May 12, 2017
1 parent 9022c52 commit de89794
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 7 deletions.
56 changes: 54 additions & 2 deletions dila/frontend/flask/forms.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,68 @@
import flask_wtf
import wtforms

from dila import application
from dila.application import structures


class PoFileUpload(flask_wtf.FlaskForm):
po_file = wtforms.FileField('Select po file')
apply_translations = wtforms.BooleanField('Apply translations')


class TranslationForm(flask_wtf.FlaskForm):
translation = wtforms.TextAreaField(render_kw={'rows': 5, 'cols': 90})
def get_translation_form(translated_string):
if translated_string.plural:
return PluralTranslationForm(
obj=translated_string,
translation_one=translated_string.translation,
translation_few=translated_string.plural_translations.few,
translation_many=translated_string.plural_translations.many,
translation_other=translated_string.plural_translations.other,
)
else:
return TranslationForm(obj=translated_string)


class BaseTranslationForm(flask_wtf.FlaskForm):
translator_comment = wtforms.TextAreaField(render_kw={'rows': 5, 'cols': 90})

def set_translated_string(self, language_code, pk):
application.set_translated_string(language_code, pk,
**self.get_translated_string_kwargs())

def get_translated_string_kwargs(self):
return {
'translator_comment': self.data['translator_comment'],
}


class TranslationForm(BaseTranslationForm):
translation = wtforms.TextAreaField(render_kw={'rows': 5, 'cols': 90})

def get_translated_string_kwargs(self):
return {
'translation': self.data['translation'],
**super().get_translated_string_kwargs(),
}


class PluralTranslationForm(BaseTranslationForm):
translation_one = wtforms.TextAreaField(render_kw={'rows': 5, 'cols': 90})
translation_few = wtforms.TextAreaField(render_kw={'rows': 5, 'cols': 90})
translation_many = wtforms.TextAreaField(render_kw={'rows': 5, 'cols': 90})
translation_other = wtforms.TextAreaField(render_kw={'rows': 5, 'cols': 90})

def get_translated_string_kwargs(self):
return {
'translation': self.data['translation_one'],
'plural_translations': structures.PluralTranslations(
few=self.data['translation_few'],
many=self.data['translation_many'],
other=self.data['translation_other'],
),
**super().get_translated_string_kwargs(),
}


class NewResourceForm(flask_wtf.FlaskForm):
new_resource_name = wtforms.StringField('New resource')
Expand Down
7 changes: 6 additions & 1 deletion dila/frontend/flask/templates/translated_string.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
enctype=multipart/form-data>
<table class="table-striped">
<tr><td>Base string</td><td>{{ translated_string.base_string }}</td></tr>
{% if form.plural %}<tr><td>Plural</td><td>{{ translated_string.plural }}</td></tr>{% endif %}
<tr><td>Comment</td><td>{{ translated_string.comment }}</td></tr>
<tr><td>Context</td><td>{{ translated_string.context }}</td></tr>
<tr><td>Translation</td><td>{{ form.translation(class_="form-control") }}</td></tr>
{% if form.translation %}<tr><td>Translation</td><td>{{ form.translation(class_="form-control") }}</td></tr>{% endif %}
{% if form.translation_one %}<tr><td>Translation one</td><td>{{ form.translation_one(class_="form-control") }}</td></tr>{% endif %}
{% if form.translation_few %}<tr><td>Translation few</td><td>{{ form.translation_few(class_="form-control") }}</td></tr>{% endif %}
{% if form.translation_many %}<tr><td>Translation many</td><td>{{ form.translation_many(class_="form-control") }}</td></tr>{% endif %}
{% if form.translation_other %}<tr><td>Translation other</td><td>{{ form.translation_other(class_="form-control") }}</td></tr>{% endif %}
<tr><td>Translator Comment</td><td>{{ form.translator_comment(class_="form-control") }}</td></tr>
</table>
{{ form.csrf_token }}
Expand Down
6 changes: 2 additions & 4 deletions dila/frontend/flask/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ def get(self):

def post(self):
if self.form.validate():
application.set_translated_string(self.language_code, self.pk,
translation=self.form.data['translation'],
translator_comment=self.form.data['translator_comment'])
self.form.set_translated_string(self.language_code, self.pk)
flask.flash('Translation changed')
return flask.redirect(
flask.url_for('main.resource',
Expand All @@ -158,7 +156,7 @@ def context(self):

@cached_property
def form(self):
return forms.TranslationForm(obj=self.translated_string)
return forms.get_translation_form(self.translated_string)

@cached_property
def translated_string(self):
Expand Down
73 changes: 73 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,39 @@ def test_get_translation_form(get_translated_string, flask_client):
assert 'action="/lang/pl/edit/34/"' in response.data.decode()


@mock.patch('dila.application.get_translated_string')
@mock.patch('dila.application.get_languages', mock.MagicMock())
@mock.patch('dila.application.get_language', mock.MagicMock())
def test_get_plural_translation_form(get_translated_string, flask_client):
get_translated_string.return_value = structures.TranslatedStringData(
pk='34',
base_string='%d option',
plural='%d options',
translation='%d opcja',
comment='comment',
translator_comment='translator_comment',
context='context',
resource_pk='1',
plural_translations=structures.PluralTranslations(
few='%d opcje',
many='%d opcji',
other='%d opcji',
),
)
response = flask_client.get('/lang/pl/edit/34/')
assert re.search('<textarea class="[^"]*" cols="[^"]*" id="translation_one" name="translation_one" rows="[^"]*">'
'%d opcja</textarea>', response.data.decode())
assert re.search('<textarea class="[^"]*" cols="[^"]*" id="translation_few" name="translation_few" rows="[^"]*">'
'%d opcje</textarea>', response.data.decode())
assert re.search('<textarea class="[^"]*" cols="[^"]*" id="translation_many" name="translation_many" rows="[^"]*">'
'%d opcji</textarea>', response.data.decode())
assert re.search('<textarea class="[^"]*" cols="[^"]*" id="translation_other" name="translation_other" rows="[^"]*">'
'%d opcji</textarea>', response.data.decode())
assert re.search('<textarea class="[^"]*" cols="[^"]*" id="translator_comment" name="translator_comment" '
'rows="[^"]*">translator_comment</textarea>', response.data.decode())
assert 'action="/lang/pl/edit/34/"' in response.data.decode()


@mock.patch('dila.application.get_translated_strings', mock.MagicMock())
@mock.patch('dila.application.get_translated_string')
@mock.patch('dila.application.set_translated_string')
Expand Down Expand Up @@ -353,6 +386,46 @@ def test_post_translation_form(set_translated_string, get_translated_string, fla
set_translated_string.assert_called_with('pl', '34', translation='new-translation',
translator_comment='new-translator-comment')

@mock.patch('dila.application.get_translated_strings', mock.MagicMock())
@mock.patch('dila.application.get_translated_string')
@mock.patch('dila.application.set_translated_string')
@mock.patch('dila.application.get_languages', mock.MagicMock())
@mock.patch('dila.application.get_language', mock.MagicMock())
def test_post_plural_translation_form(set_translated_string, get_translated_string, flask_client):
get_translated_string.return_value = structures.TranslatedStringData(
pk='34',
base_string='%d option',
plural='%d options',
translation='%d alternatywa',
comment='comment',
translator_comment='translator_comment',
context='context',
resource_pk='1',
plural_translations=structures.PluralTranslations(
few='%d alternatywy',
many='%d alternatyw',
other='%d alternatyw',
),
)
response = flask_client.post('/lang/pl/edit/34/', data={
'translation_one': '%d opcja',
'translation_few': '%d opcje',
'translation_many': '%d opcji',
'translation_other': '%d opcji',
'translator_comment': 'new-translator-comment',
})
assert response.status_code == 302
assert response.location == 'http://localhost/lang/pl/res/1/'
response = flask_client.get(response.location)
assert 'Translation changed' in response.data.decode()
set_translated_string.assert_called_with('pl', '34', translation='%d opcja',
translator_comment='new-translator-comment',
plural_translations=structures.PluralTranslations(
few='%d opcje',
many='%d opcji',
other='%d opcji',
))


@mock.patch('dila.application.get_po_file')
@mock.patch('dila.application.get_languages', mock.MagicMock())
Expand Down

0 comments on commit de89794

Please sign in to comment.