Skip to content

Commit

Permalink
Add tests for translated form labels
Browse files Browse the repository at this point in the history
  • Loading branch information
julianwachholz committed Apr 7, 2024
1 parent f6465f5 commit 9b69b8f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 1 deletion.
Binary file added tests/app/locale/de/LC_MESSAGES/django.mo
Binary file not shown.
23 changes: 23 additions & 0 deletions tests/app/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-07 11:39+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: app/models.py:203
msgid "title of the post"
msgstr "Titel des Beitrags"
Binary file added tests/app/locale/fr/LC_MESSAGES/django.mo
Binary file not shown.
23 changes: 23 additions & 0 deletions tests/app/locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-07 11:39+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"

#: app/models.py:203
msgid "title of the post"
msgstr "Titre de l'article"
6 changes: 5 additions & 1 deletion tests/app/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.utils.translation import gettext_lazy

from modeltrans.conf import get_default_language
from modeltrans.fields import TranslationField
Expand Down Expand Up @@ -198,7 +199,10 @@ def __str__(self):


class Post(models.Model):
title = models.CharField(max_length=255)
title = models.CharField(
verbose_name=gettext_lazy("title of the post"),
max_length=255,
)
is_published = models.BooleanField(default=False)

i18n = TranslationField(fields=("title",))
Expand Down
20 changes: 20 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,26 @@ def test_setting_of_field_properties(self):
self.assertEqual(title_fr_field.required, True)
self.assertEqual(title_fr_field.widget.__class__, forms.widgets.Textarea)

def test_translated_field_labels(self):
"""Test that a field's verbose_name is translated to the currently active language."""
form_cls = modelform_factory(Post, fields="__all__")
form = form_cls()
self.assertEqual(form.fields["title"].label, "Title of the post")
self.assertEqual(form.fields["title_de"].label, "Title of the post (DE)")
self.assertEqual(form.fields["title_fr"].label, "Title of the post (FR)")

with override("de"):
form = form_cls()
self.assertEqual(form.fields["title"].label, "Titel des Beitrags")
self.assertEqual(form.fields["title_de"].label, "Titel des Beitrags (DE)")
self.assertEqual(form.fields["title_fr"].label, "Titel des Beitrags (FR)")

with override("fr"):
form = form_cls()
self.assertEqual(form.fields["title"].label, "Titre de l'article")
self.assertEqual(form.fields["title_de"].label, "Titre de l'article (DE)")
self.assertEqual(form.fields["title_fr"].label, "Titre de l'article (FR)")

def test_form_initial_values(self):
challenge = Challenge.objects.create(title="english", title_fr="french")
initial_data = {
Expand Down

0 comments on commit 9b69b8f

Please sign in to comment.