Skip to content
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

Merged
merged 4 commits into from
Feb 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cadasta/config/settings/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import os

from django.utils.translation import ugettext_lazy as _
from .languages import FORM_LANGS # noqa

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

Expand Down
78 changes: 78 additions & 0 deletions cadasta/config/settings/languages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from django.utils.translation import ugettext_lazy as _


FORM_LANGS = {
'af': _('Afrikaans'),
'ar': _('Arabic'),
'az': _('Azerbaijani'),
'be': _('Belarusian'),
'bg': _('Bulgarian'),
'bn': _('Bengali'),
'br': _('Breton'),
'bs': _('Bosnian'),
'ca': _('Catalan'),
'cs': _('Czech'),
'cy': _('Welsh'),
'da': _('Danish'),
'de': _('German'),
'el': _('Greek'),
'en': _('English'),
'eo': _('Esperanto'),
'es': _('Spanish'),
'et': _('Estonian'),
'eu': _('Basque'),
'fa': _('Persian (Farsi)'),
'fi': _('Finnish'),
'fr': _('French'),
'fy': _('Western Frisian'),
'ga': _('Irish'),
'gd': _('Scottish Gaelic'),
'gl': _('Galician'),
'he': _('Hebrew'),
'hi': _('Hindi'),
'hr': _('Croatian'),
'hu': _('Hungarian'),
'ia': _('Interlingua'),
'id': _('Indonesian'),
'io': _('Ido'),
'is': _('Icelandic'),
'it': _('Italian'),
'ja': _('Japanese'),
'ka': _('Georgian'),
'kk': _('Kazakh'),
'km': _('Khmer'),
'kn': _('Kannada'),
'ko': _('Korean'),
'lb': _('Luxembourgish'),
'lt': _('Lithuanian'),
'lv': _('Latvian'),
'mk': _('Macedonian'),
'ml': _('Malayalam'),
'mn': _('Mongolian'),
'mr': _('Marathi'),
'my': _('Burmese'),
'nb': _('Norwegian (Bokmål)'),
'ne': _('Nepali'),
'nl': _('Dutch'),
'nn': _('Norwegian (Nynorsk)'),
'os': _('Ossetian'),
'pa': _('Eastern Punjabi'),
'pl': _('Polish'),
'pt': _('Portuguese'),
'ro': _('Romanian'),
'ru': _('Russian'),
'sk': _('Slovak'),
'sl': _('Slovene'),
'sq': _('Albanian'),
'sr': _('Serbian'),
'sv': _('Swedish'),
'sw': _('Swahili'),
'ta': _('Tamil'),
'te': _('Telugu'),
'th': _('Thai'),
'tr': _('Turkish'),
'tt': _('Tatar'),
'uk': _('Ukrainian'),
'ur': _('Urdu'),
'vi': _('Vietnamese')
}
64 changes: 61 additions & 3 deletions cadasta/core/form_mixins.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from django.forms import Form, ModelForm

from django.forms import Form, ModelForm, MultipleChoiceField
from jsonattrs.mixins import template_xlang_labels
from jsonattrs.forms import form_field_from_name
from django.contrib.contenttypes.models import ContentType
from tutelary.models import Role

from questionnaires.models import Questionnaire, Question, QuestionOption
from .mixins import SchemaSelectorMixin
from .widgets import XLangSelect, XLangSelectMultiple


class SuperUserCheck:
Expand All @@ -22,12 +24,45 @@ def is_superuser(self, user):


class AttributeFormMixin(SchemaSelectorMixin):
def set_standard_field(self, name, empty_choice=None, field_name=None):
if not field_name:
field_name = name
q = Questionnaire.objects.get(id=self.project.current_questionnaire)
default_lang = q.default_language
try:
question = Question.objects.get(name=name, questionnaire=q)
self.fields[field_name].labels_xlang = template_xlang_labels(
question.label_xlat)

if question.has_options:
choices = QuestionOption.objects.filter(
question=question).values_list('name', 'label_xlat')

try:
choices, xlang_labels = zip(
*[((c[0], c[1].get(default_lang)),
(c[0], c[1])) for c in choices])
except AttributeError:
choices = choices
xlang_labels = ''

choices = ([('', empty_choice)] + list(choices)
if empty_choice else list(choices))
self.fields[field_name].widget = XLangSelect(
attrs=self.fields[field_name].widget.attrs,
choices=choices,
xlang_labels=dict(xlang_labels)
)
except Question.DoesNotExist:
pass

def create_model_fields(self, field_prefix, attribute_map, new_item=False):
for selector, attributes in attribute_map.items():
for name, attr in attributes.items():
fieldname = '{}::{}::{}'.format(
field_prefix, selector.lower(), name)
atype = attr.attr_type

field_kwargs = {
'label': attr.long_name, 'required': attr.required
}
Expand All @@ -40,6 +75,7 @@ def create_model_fields(self, field_prefix, attribute_map, new_item=False):
chs = list(zip(attr.choices, attr.choice_labels))
else:
chs = [(c, c) for c in attr.choices]

field_kwargs['choices'] = chs
if atype.form_field == 'BooleanField':
field_kwargs['required'] = attr.required
Expand All @@ -51,7 +87,29 @@ def create_model_fields(self, field_prefix, attribute_map, new_item=False):
if len(attr.default) > 0 and len(str(
field_kwargs.get('initial', ''))) == 0:
self.set_default(field_kwargs, attr)
self.fields[fieldname] = field(**field_kwargs)

f = field(**field_kwargs)

if hasattr(f.widget, 'choices'):
try:
xlang_labels = dict(zip(attr.choices,
attr.choice_labels_xlat))
except TypeError:
xlang_labels = {}

widget_args = {
'attrs': f.widget.attrs,
'choices': f.widget.choices,
'xlang_labels': xlang_labels
}

if isinstance(f, MultipleChoiceField):
f.widget = XLangSelectMultiple(**widget_args)
else:
f.widget = XLangSelect(**widget_args)

f.labels_xlang = template_xlang_labels(attr.long_name_xlat)
self.fields[fieldname] = f

def set_default(self, field_kwargs, attr, boolean=False):
if len(attr.default) > 0:
Expand Down
46 changes: 46 additions & 0 deletions cadasta/core/static/css/forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,52 @@ tr.contacts-error {
}
}

/* =Project translation select dropdown
-------------------------------------------------------------- */

.langs-select {
Copy link
Contributor

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?

Copy link
Member Author

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?

overflow: hidden;
height: 34px;
position: relative;
margin-right: 30px;
margin-bottom: 0;
select {
color: #fff;
padding: 4px;
padding-right: 25px;
background-color: transparent;
border: 0;
border-radius: 0;
outline: none;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
box-shadow: unset;
-webkit-box-shadow: unset;
> option {
background: #fff;
padding: 6px;
color: $gray-dark;
}
}
&:after {
font-family: 'Glyphicons Halflings';
font-size: 9px;
content:"\e252";
color: #fff;
padding: 12px 8px;
position: absolute;
right: 10px;
top: 0;
z-index: 1;
text-align: center;
width: 10%;
height: 100%;
pointer-events: none;
box-sizing: border-box;
}
}

/* =Required field label
-------------------------------------------------------------- */

Expand Down
Loading