Skip to content

Commit

Permalink
Merge pull request #450 from gushil/fix-446
Browse files Browse the repository at this point in the history
  • Loading branch information
lognaturel authored Jul 24, 2020
2 parents ef8bd00 + 818c452 commit b6093a1
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 19 deletions.
39 changes: 20 additions & 19 deletions pyxform/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,19 +591,21 @@ def _setup_choice_translations(name, choice_value, itext_id):
self._translations = defaultdict(dict) # pylint: disable=W0201
for element in self.iter_descendants():
for d in element.get_translations(self.default_language):

translation_path = d["path"]
form = "long"

if "guidance_hint" in d["path"]:
hint_path = d["path"].replace("guidance_hint", "hint")
self._translations[d["lang"]][hint_path] = self._translations[
d["lang"]
].get(hint_path, {})
self._translations[d["lang"]][hint_path].update(
{"guidance": d["text"]}
)
else:
self._translations[d["lang"]][d["path"]] = self._translations[
d["lang"]
].get(d["path"], {})
self._translations[d["lang"]][d["path"]].update({"long": d["text"]})
translation_path = d["path"].replace("guidance_hint", "hint")
form = "guidance"

self._translations[d["lang"]][translation_path] = self._translations[
d["lang"]
].get(translation_path, {})

self._translations[d["lang"]][translation_path].update(
{form: {"text": d["text"], "output_context": d["output_context"],}}
)

# This code sets up translations for choices in filtered selects.
for list_name, choice_list in self.choices.items():
Expand Down Expand Up @@ -722,12 +724,14 @@ def itext(self):
raise Exception()

for media_type, media_value in content.items():
# There is a odk/jr bug where hints can't have a value
# for the "form" attribute.
# This is my workaround.
if label_type == "hint":
if isinstance(media_value, dict):
value, output_inserted = self.insert_output_values(
media_value["text"], context=media_value["output_context"]
)
else:
value, output_inserted = self.insert_output_values(media_value)

if label_type == "hint":
if media_type == "guidance":
itext_nodes.append(
node(
Expand All @@ -744,14 +748,12 @@ def itext(self):
continue

if media_type == "long":
value, output_inserted = self.insert_output_values(media_value)
# I'm ignoring long types for now because I don't know
# how they are supposed to work.
itext_nodes.append(
node("value", value, toParseString=output_inserted)
)
elif media_type == "image":
value, output_inserted = self.insert_output_values(media_value)
if value != "-":
itext_nodes.append(
node(
Expand All @@ -762,7 +764,6 @@ def itext(self):
)
)
else:
value, output_inserted = self.insert_output_values(media_value)
if value != "-":
itext_nodes.append(
node(
Expand Down
4 changes: 4 additions & 0 deletions pyxform/survey_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ def get_translations(self, default_language):
"path": self._translation_path("jr:constraintMsg"),
"lang": lang,
"text": text,
"output_context": self,
}
required_msg = bind_dict.get("jr:requiredMsg")
if type(required_msg) is dict:
Expand All @@ -281,6 +282,7 @@ def get_translations(self, default_language):
"path": self._translation_path("jr:requiredMsg"),
"lang": lang,
"text": text,
"output_context": self,
}
no_app_error_string = bind_dict.get("jr:noAppErrorString")
if type(no_app_error_string) is dict:
Expand All @@ -289,6 +291,7 @@ def get_translations(self, default_language):
"path": self._translation_path("jr:noAppErrorString"),
"lang": lang,
"text": text,
"output_context": self,
}

for display_element in ["label", "hint", "guidance_hint"]:
Expand Down Expand Up @@ -327,6 +330,7 @@ def get_translations(self, default_language):
"display_element": display_element, # Not used
"path": self._translation_path(display_element),
"element": self, # Not used
"output_context": self,
"lang": lang,
"text": text,
}
Expand Down
64 changes: 64 additions & 0 deletions pyxform/tests_v1/test_repeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,70 @@ def test_indexed_repeat_relative_path(self):
],
)

def test_output_with_translation_relative_path(self):
md = """
| survey | | | | | | | | |
| | type | name | label::English | calculation | hint::English | constraint_message::English | required_message::English | noAppErrorString::English |
| | begin repeat | member | | | | | | |
| | calculate | pos | | position(..) | | | | |
| | text | member_name | Name of ${pos} | | | | | |
| | text | a | A | | hint ${pos} | constraint ${pos} | required ${pos} | app error ${pos} |
| | end repeat | | | | | | | |
"""

self.assertPyxformXform(
md=md,
name="inside-repeat-relative-path",
xml__contains=[
'<translation lang="English">',
'<value> Name of <output value=" ../pos "/> </value>',
'<value> hint <output value=" ../pos "/> </value>',
'<value> constraint <output value=" ../pos "/> </value>',
'<value> required <output value=" ../pos "/> </value>',
'<value> app error <output value=" ../pos "/>',
],
)

def test_output_with_guidance_hint_translation_relative_path(self):
md = """
| survey | | | | | |
| | type | name | label::English | guidance_hint::English | calculation |
| | begin repeat | member | | | |
| | calculate | pos | | | position(..) |
| | text | member_name | Name of ${pos} | More ${pos} | |
| | end repeat | | | | |
"""

self.assertPyxformXform(
md=md,
name="inside-repeat-relative-path",
xml__contains=[
'<translation lang="English">',
'<value> Name of <output value=" ../pos "/> </value>',
'<value form="guidance"> More <output value=" ../pos "/> </value>',
],
)

def test_output_with_multiple_translations_relative_path(self):
md = """
| survey | | | | | |
| | type | name | label::English | label::Indonesia | calculation |
| | begin repeat | member | | | |
| | calculate | pos | | | position(..) |
| | text | member_name | Name of ${pos} | Nama ${pos} | |
| | text | member_address | | Alamat | |
| | end repeat | | | | |
"""

self.assertPyxformXform(
md=md,
name="inside-repeat-relative-path",
xml__contains=[
'<translation lang="English">',
'<value> Name of <output value=" ../pos "/> </value>',
],
)

def test_hints_are_not_present_within_repeats(self):
"""Hints are not present within repeats"""
md = """
Expand Down

0 comments on commit b6093a1

Please sign in to comment.