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

Sort countries by localized name #1247

Merged
merged 2 commits into from
Feb 8, 2019
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
## Upcoming release
#### Changes
* [[@kollivier](https://github.com/kollivier)] Fix issue with channel content defaults only supporting English characters.
* [[@kollivier](https://github.com/kollivier)] Sort countries by localized country name.


#### Issues Resolved
* [#1004](https://github.com/learningequality/studio/issues/1004)

* [#976](https://github.com/learningequality/studio/issues/1004)

## 2019-02-06 Release
#### Changes
Expand Down
40 changes: 23 additions & 17 deletions contentcuration/contentcuration/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@
REGISTRATION_SALT = getattr(settings, 'REGISTRATION_SALT', 'registration')


def get_sorted_countries(language):
"""
Gets the list of countries sorted by localized name.

NOTE: If we start adding more localization code, we should probably consolidate that code into a localization module.

:param language: Language to localize into and sort
:return: list of countries sorted by localized language
"""
translator = gettext.translation(
domain='iso3166',
localedir=pycountry.LOCALES_DIR,
languages=[language],
codeset='utf-8',
fallback=True,
)

return sorted([(c.name, translator.gettext(c.name)) for c in list(pycountry.countries)],
key=lambda x: x[1])


class ExtraFormMixin(object):

def check_field(self, field, error):
Expand Down Expand Up @@ -116,15 +137,7 @@ def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super(RegistrationInformationForm, self).__init__(*args, **kwargs)

translator = gettext.translation(
domain='iso3166',
localedir=pycountry.LOCALES_DIR,
languages=[self.request.LANGUAGE_CODE],
codeset='utf-8',
fallback=True,
)

countries = [(c.name, translator.gettext(c.name)) for c in list(pycountry.countries)]
countries = get_sorted_countries(self.request.LANGUAGE_CODE)
self.fields['location'] = forms.ChoiceField(required=True, widget=forms.SelectMultiple, label=_(
'Where do you plan to use Kolibri? (select all that apply)'), choices=countries)

Expand Down Expand Up @@ -363,17 +376,10 @@ def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
channels = kwargs.pop('channel_choices', None)
super(StorageRequestForm, self).__init__(*args, **kwargs)
translator = gettext.translation(
domain='iso3166',
localedir=pycountry.LOCALES_DIR,
languages=[self.request.LANGUAGE_CODE],
codeset='utf-8',
fallback=True,
)

self.fields['public'] = forms.MultipleChoiceField(required=False, widget=forms.SelectMultiple(attrs={"class": "multi-select-field"}), choices=channels)

countries = [(c.name, translator.gettext(c.name)) for c in list(pycountry.countries)]
countries = get_sorted_countries(self.request.LANGUAGE_CODE)
self.fields['location'] = forms.ChoiceField(required=False, widget=forms.SelectMultiple(attrs={"class": "multi-select-field"}), choices=countries)

class Meta:
Expand Down
17 changes: 17 additions & 0 deletions contentcuration/contentcuration/tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import copy

from base import BaseTestCase
from contentcuration import forms


class LanguageSortTest(BaseTestCase):
def test_sorted_countries_en(self):
countries = forms.get_sorted_countries("en")
localized_names = [country[1] for country in countries]

# Since we're using English, Python's default sort should give the same language order as the one
# returned from get_sorted_countries. If we want to test sorts in different languages, we'll probably
# need a different approach.
sorted = copy.copy(localized_names)
sorted.sort()
assert localized_names == sorted