Skip to content

Commit

Permalink
warn for each language field that is being blanked due to missing tra…
Browse files Browse the repository at this point in the history
…nslations
  • Loading branch information
KeynesYouDigIt committed Jun 1, 2019
1 parent ac81cda commit 9145b5d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
23 changes: 15 additions & 8 deletions pyxform/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,12 +549,6 @@ def _add_empty_translations(self, warnings=None):
if warnings is None:
warnings = []

default_language_warning = ('\tDefault language not set,'
+ ' with missing default translations.'
' Please consider setting a `default_language` in your'
+ ' settings tab to insure questions and options appear'
+ ' as expected.')

paths = {}
for lang, translation in self._translations.items():
for path, content in translation.items():
Expand All @@ -566,10 +560,23 @@ def _add_empty_translations(self, warnings=None):
self._translations[lang][path] = {}
for content_type in content_types:
if content_type not in self._translations[lang][path]:
if lang == 'default' and default_language_warning not in warnings:
warnings.append(default_language_warning)
missing_warning = self._generate_missing_translation_warning(
lang)
if missing_warning not in warnings:
warnings.append(missing_warning)
self._translations[lang][path][content_type] = u"-"

def _generate_missing_translation_warning(self, lang):
if lang == 'default':
return ('\tDefault language not set,'
+ ' with missing default translations.'
' Please consider setting a `default_language` in your'
+ ' settings tab to insure questions and options appear'
+ ' as expected.')

return ('\tMissing field translations found for ' + lang
+ ' field may not appear as expected')

def _setup_media(self):
"""
Traverse the survey, find all the media, and put in into the \
Expand Down
41 changes: 26 additions & 15 deletions pyxform/tests_v1/test_language_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,37 @@ def test_label_with_unknown_subtag_should_warn(self):

def test_no_default_language_should_warn_with_unlabeled_media_tags(self):
# form should test media tag w NO default language set.
survey = self.md_to_pyxform_survey(
"""
| survey | | | | | |
| | type | name | label::English (en) | choice_filter | media::image|
| | select_one opts | opt | My opt | fake = 1 | opt1.jpg |
| choices | | | | | |
| | list_name | name | label | fake | |
| | opts | opt1 | Opt1 | 1 | |
| | opts | opt2 | Opt2 | 1 | |
| settings| | | | | |
| | form_title | form_id | public_key | | |
| | opts_form | abc1 | | | |
"""
)
survey = self.md_to_pyxform_survey("""
| survey | | | | |
| | type | name | label::English (en) | media::image|
| | integer | nums | How many nums? | opt1.jpg |
""")

warnings = []
tmp = tempfile.NamedTemporaryFile(suffix='.xml', delete=False)
tmp.close()
survey.print_xform_to_file(tmp.name, warnings=warnings)
self.assertTrue(len(warnings) == 1)

# In this survey, when 'default' s selected the label of this question will be '-'.
# Also, when 'English (en)` is selected, the medial will be '-'
self.assertTrue(len(warnings) == 2)
os.unlink(tmp.name)

def test_language_lacking_translation_should_warn(self):
survey = self.md_to_pyxform_survey("""
| survey | | | | |
| | type | name | label::English (en) | media::image::French (fr)|
| | integer | nums | How many nums? | opt1.jpg |
""")

warnings = []
tmp = tempfile.NamedTemporaryFile(suffix='.xml', delete=False)
tmp.close()
survey.print_xform_to_file(tmp.name, warnings=warnings)

# In this survey, when 'French (fr)' s selected the label of this question will be '-'.
# Also, when 'English (en)` is selected, the medial will be '-'
self.assertTrue(len(warnings) == 2)
os.unlink(tmp.name)

def test_default_language_only_should_not_warn(self):
Expand Down

0 comments on commit 9145b5d

Please sign in to comment.