Skip to content

Commit

Permalink
warn per language when translations are missing
Browse files Browse the repository at this point in the history
  • Loading branch information
KeynesYouDigIt committed May 4, 2019
1 parent e8ee98d commit 45a4b46
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
23 changes: 15 additions & 8 deletions pyxform/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,12 +535,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 @@ -552,10 +546,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
30 changes: 18 additions & 12 deletions pyxform/tests_v1/test_language_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,25 @@ def test_label_with_unknown_subtag_should_warn(self):
'do not contain valid machine-readable codes: English (schm). Learn more' in warnings[0])
os.unlink(tmp.name)

def test_no_default_language_should_warn_with_unlabeled_media_tags(self):
# form should test media tag w NO default language set.
def test_no_default_language_should_warn_with_unlabeled_columns(self):
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 | | | | |
| | 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)
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 = []
Expand Down

0 comments on commit 45a4b46

Please sign in to comment.