Merge lp:~daker/loco-team-portal/fix.648296-648293 into lp:loco-team-portal

Proposed by Adnane Belmadiaf
Status: Merged
Approved by: Adnane Belmadiaf
Approved revision: 559
Merged at revision: 557
Proposed branch: lp:~daker/loco-team-portal/fix.648296-648293
Merge into: lp:loco-team-portal
Diff against target: 179 lines (+69/-21)
5 files modified
loco_directory/common/context_processors.py (+11/-3)
loco_directory/common/utils.py (+36/-3)
loco_directory/common/views.py (+18/-11)
loco_directory/settings.py (+2/-2)
loco_directory/templates/base.html (+2/-2)
To merge this branch: bzr merge lp:~daker/loco-team-portal/fix.648296-648293
Reviewer Review Type Date Requested Status
LoCo Team Portal Developers Pending
Review via email: mp+133769@code.launchpad.net

Commit message

Fixed the Languages switcher

To post a comment you must log in.
557. By Adnane Belmadiaf

* Remove print statement

558. By Adnane Belmadiaf

* Removed another print statement

559. By Adnane Belmadiaf

* Fixed a typo

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'loco_directory/common/context_processors.py'
2--- loco_directory/common/context_processors.py 2012-06-01 23:45:18 +0000
3+++ loco_directory/common/context_processors.py 2012-11-09 23:49:20 +0000
4@@ -1,8 +1,9 @@
5-# context processors for loco-directory
6-# see: http://docs.djangoproject.com/en/dev/ref/settings/#setting-TEMPLATE_CONTEXT_PROCESSORS
7-# to use this processor, add "common.context_processors.loco_version" to your variable TEMPLATE_CONTEXT_PROCESSORS in your settings file
8+# -*- coding: utf-8 -*-
9
10 from django.conf import settings
11+from django.utils.translation import to_locale, get_language
12+
13+from utils import get_languages
14
15
16 def loco_version(request):
17@@ -54,3 +55,10 @@
18 from common.forms import SiteSearchForm
19 search_form = SiteSearchForm(data=request.GET)
20 return {'search_form': search_form}
21+
22+
23+def languages_menu(request):
24+ """
25+ Return the languages list.
26+ """
27+ return {'languages': get_languages(), 'LOCALE': to_locale(get_language())}
28\ No newline at end of file
29
30=== modified file 'loco_directory/common/utils.py'
31--- loco_directory/common/utils.py 2012-06-01 23:45:18 +0000
32+++ loco_directory/common/utils.py 2012-11-09 23:49:20 +0000
33@@ -1,11 +1,14 @@
34+# -*- coding: utf-8 -*-
35+
36+import subprocess
37+import datetime
38 import email
39 import os
40 from copy import deepcopy
41-import subprocess
42-import datetime
43-
44 from django.conf import settings
45
46+languages = None
47+
48
49 def write_log(job_name, log):
50 stamp_dir = os.path.join(settings.PROJECT_PATH, 'data')
51@@ -106,3 +109,33 @@
52 def reset(self):
53 self.index = -1
54 return ''
55+
56+
57+def check_for_language(lang_code):
58+ """
59+ Checks whether there is a global language file for the given language code.
60+ """
61+ globalpath = os.path.join(settings.PROJECT_PATH, 'locale')
62+ for dirnames in os.listdir(globalpath):
63+ if dirnames == lang_code:
64+ for dirpath_local, dirnames_local, filenames in os.walk(os.path.join(globalpath, dirnames, 'LC_MESSAGES')):
65+ if 'django.mo' in filenames:
66+ return True
67+ else:
68+ return False
69+
70+def get_languages():
71+ """
72+ Return a language list.
73+ """
74+ global languages
75+ if languages is None:
76+ from teams.models import Language
77+ languages = {}
78+ Languages = Language.objects.order_by('name')
79+ for language in Languages:
80+ if check_for_language(language.code):
81+ languages[language.code] = language.name
82+ languages['en_US'] = 'English'
83+ languages = sorted([(value,key) for (key,value) in languages.items()])
84+ return languages
85
86=== modified file 'loco_directory/common/views.py'
87--- loco_directory/common/views.py 2012-06-01 23:45:18 +0000
88+++ loco_directory/common/views.py 2012-11-09 23:49:20 +0000
89@@ -4,8 +4,9 @@
90 from django.template import RequestContext
91 from django.template.loader import render_to_string
92 from django.contrib.auth import logout
93+from django.conf import settings
94+from django.utils import translation
95 from django.utils.translation import check_for_language
96-from django.conf import settings
97
98 try:
99 from django_openid_auth.exceptions import (
100@@ -78,19 +79,25 @@
101 return HttpResponseRedirect('/')
102
103
104-def set_language(request):
105+def set_language(request, next=None):
106 """
107 Change the language for a user
108 """
109- next_url = request.META.get('HTTP_REFERER', None)
110- if not next_url:
111- next_url = '/'
112- response = HttpResponseRedirect(next_url)
113- lang = request.GET.get('lang', '')
114- if check_for_language(lang):
115- if hasattr(request, 'session'):
116- request.session['django_language'] = lang
117- response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang)
118+ if not next:
119+ next = request.REQUEST.get('next', None)
120+ if not next:
121+ next = request.META.get('HTTP_REFERER', None)
122+ if not next:
123+ next = '/'
124+ response = HttpResponseRedirect(next)
125+ if request.method == 'GET':
126+ lang_code = request.GET.get('lang', '')
127+ if lang_code and check_for_language(lang_code):
128+ if hasattr(request, 'session'):
129+ request.session['django_language'] = lang_code
130+ else:
131+ response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code)
132+ translation.activate(lang_code)
133 return response
134
135
136
137=== modified file 'loco_directory/settings.py'
138--- loco_directory/settings.py 2012-11-05 21:16:15 +0000
139+++ loco_directory/settings.py 2012-11-09 23:49:20 +0000
140@@ -29,7 +29,7 @@
141
142 # Check for necessary modules
143 modules = {
144- 'pytz': 'python-tz',
145+ 'pytz': 'python-tz',
146 'vobject': 'python-vobject',
147 }
148
149@@ -106,7 +106,6 @@
150
151 MIDDLEWARE_CLASSES = (
152 'django.middleware.common.CommonMiddleware',
153- 'django.middleware.locale.LocaleMiddleware',
154 'django.contrib.sessions.middleware.SessionMiddleware',
155 'django.contrib.auth.middleware.AuthenticationMiddleware',
156 )
157@@ -124,6 +123,7 @@
158 "common.context_processors.login_redirect",
159 "common.context_processors.url_base",
160 "common.context_processors.site_search",
161+ "common.context_processors.languages_menu",
162 )
163
164 ROOT_URLCONF = 'loco_directory.urls'
165
166=== modified file 'loco_directory/templates/base.html'
167--- loco_directory/templates/base.html 2012-09-25 21:01:12 +0000
168+++ loco_directory/templates/base.html 2012-11-09 23:49:20 +0000
169@@ -81,8 +81,8 @@
170 <form name="lang-switcher" style="text-align:right;" action="/language/" method="get">
171 <input name="next" type="hidden" value="{{ request.path }}" />
172 <select name="lang" onchange="document.forms['lang-switcher'].submit()">
173- {% for lang in LANGUAGES %}
174- <option value="{{ lang.0 }}"{% ifequal LANGUAGE_CODE lang.0 %} selected="selected"{% endifequal %}>{{ lang.1 }}</option>
175+ {% for lang in languages %}
176+ <option value="{{ lang.1 }}"{% ifequal LOCALE lang.1 %} selected="selected"{% endifequal %}>{{ lang.0 }}</option>
177 {% endfor %}
178 </select>
179 </form>

Subscribers

People subscribed via source and target branches