Allow Django unknown language in LANGUAGES settings

Language preference form expects all languages in LANGUAGES in
openstack_dashboard/settings.py. This leads to an error when
adding a language not listed in django/conf/locale to LANGUAGES list.

This commit allows such language to LANGUAGES in the settings file.

Closes-Bug: #1229026

Change-Id: I37d7f4b089b0299942f25084f741a557467b3be7
This commit is contained in:
Akihiro MOTOKI 2013-09-20 04:01:09 +09:00
parent 2ed62bb2e5
commit 8071baff97
2 changed files with 21 additions and 3 deletions

View File

@ -48,9 +48,16 @@ class UserSettingsForm(forms.SelfHandlingForm):
super(UserSettingsForm, self).__init__(*args, **kwargs)
# Languages
languages = [(k, "%s (%s)"
% (translation.get_language_info(k)['name_local'], k))
for k, v in settings.LANGUAGES]
def get_language_display_name(code, desc):
try:
desc = translation.get_language_info(code)['name_local']
except KeyError:
# If a language is not defined in django.conf.locale.LANG_INFO
# get_language_info raises KeyError
pass
return "%s (%s)" % (desc, code)
languages = [(k, get_language_display_name(k, v))
for k, v in settings.LANGUAGES]
self.fields['language'].choices = languages
# Timezones

View File

@ -14,6 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from django.conf import settings # noqa
from django.core.urlresolvers import reverse # noqa
from openstack_dashboard.test import helpers as test
@ -31,3 +32,13 @@ class UserSettingsTest(test.TestCase):
self.assertContains(res, "Europe/Moscow (UTC +04:00)")
self.assertContains(res, "Atlantic/Stanley (UTC -03:00)")
self.assertContains(res, "Pacific/Honolulu (UTC -10:00)")
def test_display_language(self):
# Add an unknown language to LANGUAGES list
settings.LANGUAGES += (('unknown', 'Unknown Language'),)
res = self.client.get(INDEX_URL)
# Known language
self.assertContains(res, 'English')
# Unknown language
self.assertContains(res, 'Unknown Language')