-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #861 -- Add language dropdown #1120
Conversation
/* =Project translation select dropdown | ||
-------------------------------------------------------------- */ | ||
|
||
.langs-select { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if the language drop down should be styled more like the 'Add location' and 'More actions' dropdowns?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the dropdown deliberately looks different; @clash99 can you clarify that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me code wise. My only question is the styling of the language drop down, and whether it's in the right place? Maybe it should be moved down to the 'detail' part of the form (beside edit and delete buttons) since it's intended to change the form labels only and not the rest of the UI.. just a thought?
@bjohare - Since the form labels are changed throughout the project - the map, parties, and resource pages - we decided it was best at that top project level. I agree its not ideal and I'll look at it again when we rework those upper right menus. |
@clash99 sounds good! |
f3205a4
to
c47ada7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have several suggestions and questions. Aside from that in some pages, standard fields are not showing the questionnaire-localized labels and/or values:
- location type label and value in location detail page
- party name label and party type label and value in party details page
- party type value in party edit page
- relationship type label and value in relationship detail page
- (maybe OK?) party name label and party type label and value in party list page
- (maybe OK?) relationship type value in location relationship tab
cadasta/spatial/forms.py
Outdated
|
||
if self.project.current_questionnaire: | ||
self.set_standard_field('party_name', | ||
_('Please select a party type'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This empty choice is unnecessary. The field is the party name and you don't select it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
cadasta/party/tests/test_forms.py
Outdated
|
||
from .. import forms | ||
from ..models import Party, TenureRelationshipType | ||
|
||
|
||
class PartyFormTest(UserTestCase, TestCase): | ||
|
||
def test_init_without_form(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the "form" in the test name (and similar tests) is confusing. It can refer to a Django form or a questionnaire question. Maybe something like test_init_without_labels
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed it to "questionnaire"
cadasta/core/static/js/xlang.js
Outdated
|
||
$('#form-langs-select').change(function() { | ||
const new_lang = $(this).val(); | ||
sessionStorage.setItem("form_lang", new_lang); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why sessionStorage
instead of localStorage
? Using sessionStorage
means that the selection won't persist in different browser sessions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@@ -113,7 +114,25 @@ def is_administrator(self): | |||
|
|||
def get_context_data(self, *args, **kwargs): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not update this method to the "usual" style where we get the context data from super()
and then inserting additional context data?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
cadasta/organization/views/mixins.py
Outdated
return super().get_context_data(is_project_member=prj_member, | ||
form_lang_default=form_lang_default, | ||
form_langs=sorted(form_langs, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're sorting form_langs
when it is already sorted earlier?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a very good question. I removed the second sorted
cadasta/core/form_mixins.py
Outdated
choices = QuestionOption.objects.filter( | ||
question=question).values_list('name', 'label_xlat') | ||
choices, xlang_labels = zip(*[((c[0], c[1].get(default_lang)), | ||
(c[0], c[1])) for c in choices]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I use a questionnaire that has only 1 set of labels (only the default language), I get the following error on this line when viewing HTML forms with select fields:
'str' object has no attribute 'get'
I think we need test cases for these types of questionnaires.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's another one of those examples, where inconsistent use of XLSForms creates problems. If you name the the labels column label::en
and define the default language this works fine. Only if you don't specify the language it breaks. But I suppose we already have questionnaires without a specified language in our database, so I'm going to have to spend another hour of my life now to work around this.
@@ -40,6 +40,11 @@ def __init__(self, project=None, *args, **kwargs): | |||
self.project = project | |||
self.add_attribute_fields() | |||
|
|||
if self.project.current_questionnaire: | |||
self.set_standard_field('location_type', | |||
_('Please select a location type'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should remove the empty choice when the form is used for editing, similar to how we do it for tenure types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is no questionnaire for the project, this select field will also have an empty option when the form is used for editing, so I'm not going to change it.
<!-- Project translation dropdown--> | ||
{% if form_langs %} | ||
<div class="langs-select form-group pull-right"> | ||
<label class="control-label sr-only" for="form-langs-select">Language for project fields</label> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap the label text in {% trans %}
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@@ -23,14 +23,15 @@ | |||
</div> | |||
<h3>{% trans "Overview" %}</h3> | |||
<div class="form-group{% if form.type.errors %} has-error{% endif %}"> | |||
<label class="control-label" for="{{ form.type.id_for_label }}">{% trans "2. What type of location is this?" %}</label> | |||
<label class="control-label" for="{{ form.type.id_for_label }}" {{ form.type.field.labels_xlang|safe }}>{% trans "2. What type of location is this?" %}</label> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are translating the label if there are questionnaire labels here, I guess we should remove the "2." in the default label and the earlier "1." in the location geometry section. When labels are translated, the "1." looks weird by itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@seav Addressed your change requests. Needs another review because the latest commit adds quite a lot. |
@oliverroick, I'm currently reviewing it now. So far everything looks good and the single-translation questionnaire is no longer throwing up errors. I did notice one minor corner-case defect. When the localStorage (or sessionStorage) doesn't yet have the |
Also another scenario entered my mind. We are storing the selected language under the key |
316d196
to
785cd9a
Compare
I addressed this in the latest commit. When the page is loaded and no language has been selected before, the JS selects the default language and sets it as the selected language in
I addressed this as well, because given the changes above it would make behaviour weird when switching between projects. The selected language is stored for each project in |
785cd9a
to
6ba66de
Compare
Proposed changes in this pull request
This PR adds a dropdown to select the language to be used in forms to create and edit locations, relationships, and parties (Fixes #861).
Changes of this PR include:
languages.py
which defines a list of languages currently supported for multi-language forms. This list can be extended later to support languages that are currently not supported by Django. The list also provides labels that are used in the language dropdown.core/static/js/xlang.js
which takes care of setting the correct translation once the page is loaded and switching the labels when a new language is selected. The selected language is stored in the session store and read from there. If no language is selected or the selected language is not shown the default language is displayed.set_standard_field
tocore.form_mixins.AttributeFormMixin
to provide translated labels to standard fields (for example location type or party type or party name).XLangSelect
andXLangSelectMultiple
which take care of adding translated labels to options of select and multiple select fields.create_model_fields
ofcore.form_mixins.AttributeFormMixin
so translated labels are provided to all questionnaire form fields.get_context_data
oforganization.views.mixin.ProjectMixin
to add available languages to the context, which are eventually rendered into the language dropdown.label
andoption
elements.When should this PR be merged
Whenever ready
Risks
Low to None
Follow up actions
None
Checklist (for reviewing)
General
migration
label if a new migration is added.Functionality
Code
Tests
Documentation