Commit f5ad6aad by Nimisha Asthagiri

Merge pull request #11506 from openfun/openfun/translate_user_languages_in_account_settings

Translate preferred languages in user account settings
parents f10a3c96 d2317be6
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
from collections import namedtuple from collections import namedtuple
from django.conf import settings from django.conf import settings
from django.utils.translation import ugettext as _
from dark_lang.models import DarkLangConfig from dark_lang.models import DarkLangConfig
...@@ -45,3 +46,16 @@ def released_languages(): ...@@ -45,3 +46,16 @@ def released_languages():
] ]
return released_languages return released_languages
def all_languages():
"""Retrieve the list of all languages, translated and sorted.
Returns:
list of (language code (str), language name (str)): the language names
are translated in the current activated language and the results sorted
alphabetically.
"""
languages = [(lang[0], _(lang[1])) for lang in settings.ALL_LANGUAGES] # pylint: disable=translation-of-non-string
return sorted(languages, key=lambda lang: lang[1])
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
""" Tests for the language API. """ """ Tests for the language API. """
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings
from django.utils import translation
from lang_pref import api as language_api from lang_pref import api as language_api
...@@ -10,3 +12,15 @@ class LanguageApiTest(TestCase): ...@@ -10,3 +12,15 @@ class LanguageApiTest(TestCase):
def test_released_languages(self): def test_released_languages(self):
released_languages = language_api.released_languages() released_languages = language_api.released_languages()
self.assertGreaterEqual(len(released_languages), 1) self.assertGreaterEqual(len(released_languages), 1)
@override_settings(ALL_LANGUAGES=[[u"cs", u"Czech"], [u"nl", u"Dutch"]])
def test_all_languages(self):
with translation.override('fr'):
all_languages = language_api.all_languages()
self.assertEqual(2, len(all_languages))
self.assertLess(all_languages[0][1], all_languages[1][1])
self.assertEqual("nl", all_languages[0][0])
self.assertEqual("cs", all_languages[1][0])
self.assertEqual(u"Hollandais", all_languages[0][1])
self.assertEqual(u"Tchèque", all_languages[1][1])
...@@ -18,7 +18,7 @@ from django.utils.translation import ugettext as _ ...@@ -18,7 +18,7 @@ from django.utils.translation import ugettext as _
from django.views.decorators.csrf import ensure_csrf_cookie from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.decorators.http import require_http_methods from django.views.decorators.http import require_http_methods
from lang_pref.api import released_languages from lang_pref.api import released_languages, all_languages
from edxmako.shortcuts import render_to_response from edxmako.shortcuts import render_to_response
from microsite_configuration import microsite from microsite_configuration import microsite
...@@ -386,7 +386,7 @@ def account_settings_context(request): ...@@ -386,7 +386,7 @@ def account_settings_context(request):
}, 'year_of_birth': { }, 'year_of_birth': {
'options': year_of_birth_options, 'options': year_of_birth_options,
}, 'preferred_language': { }, 'preferred_language': {
'options': settings.ALL_LANGUAGES, 'options': all_languages(),
} }
}, },
'platform_name': settings.PLATFORM_NAME, 'platform_name': settings.PLATFORM_NAME,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment