Commits

Mikhail Korobov committed 405ed7a

Vkontakte user language is used as django i18n language. Thanks Nakvasiuk Vasyl (aka vaXa).

Comments (0)

Files changed (4)

 Optional:
 
 * vkontakte >= 0.9.4.1 for populating cities and countries info via admin action
-* django-webtest >= 1.2.2 for tests
 
 Usage
 =====
 
 
 5. Put 'vk_iframe.middleware.AuthenticationMiddleware' and
-   'vk_iframe.middleware.LoginRequiredMiddleware' to the end of MIDDLEWARE_CLASSES::
+   'vk_iframe.middleware.LoginRequiredMiddleware' to MIDDLEWARE_CLASSES::
 
         MIDDLEWARE_CLASSES = [
             # ...
+            'django.contrib.auth.middleware.AuthenticationMiddleware',
+            # ...
             'vk_iframe.middleware.AuthenticationMiddleware',
+            # ...
+            'django.middleware.locale.LocaleMiddleware',
+            # ...
             'vk_iframe.middleware.LoginRequiredMiddleware',
         ]
 
+   Please note that 'vk_iframe.middleware.AuthenticationMiddleware' must be
+   after 'django.contrib.auth.middleware.AuthenticationMiddleware' but before
+   'django.middleware.locale.LocaleMiddleware'.
+
+   'vk_iframe.middleware.LoginRequiredMiddleware' must be after
+   'vk_iframe.middleware.AuthenticationMiddleware'.
+
    Vkontakte visitors will be automatically registered and authorized as django
    users (username == vkontakte user id).
 
             '^my-callback/',
         ]
 
+   If i18n is in use then vkontakte user's language will be used as django's
+   user language.
+
 
 6. Run ``python ./manage.py syncdb`` (or ``python ./manage.py migrate vk_iframe`` if
    South is used)

vk_iframe/forms.py

 #coding: utf-8
 from hashlib import md5
+
 from django import forms
 from django.conf import settings
 from django.utils import simplejson as json
+from django.utils.translation import check_for_language
+
 from vk_iframe.languages import LANGUAGES
 
 VIEWER_TYPES_GROUP = (
 )
 
 class VkontakteIframeForm(forms.Form):
+    LANGUAGE_CHOICES = [(i[0], i[1][0]) for i in LANGUAGES]
 
     # адрес сервиса API, по которому необходимо осуществлять запросы
     api_url = forms.CharField()
     auth_key = forms.CharField()
 
     # id языка пользователя, просматривающего приложение
-    language = forms.ChoiceField(LANGUAGES)
+    language = forms.ChoiceField(LANGUAGE_CHOICES)
 
     # результат первого API-запроса, который выполняется при загрузке приложения
     api_result = forms.CharField(required = False)
         if api_result:
             return json.loads(api_result)['response'][0]
         return {}
+
+    def language_code(self):
+        """
+        Возвращает код языка или None, если язык не поддерживается/не известен
+        """
+        lang_code = None
+        for language in self.LANGUAGE_CHOICES:
+            if language[0] == int(self.cleaned_data['language']):
+                lang_code = language[1]
+                break
+        if check_for_language(lang_code):
+            return lang_code
+        return None

vk_iframe/languages.py

 # -*- coding: utf-8 -*-
 
 LANGUAGES = (
-    (0, u"Русский"),
-    (1, u"Українська"),
-    (2, u"Беларуская (тарашкевiца)"),
-    (3, u"English"),
-    (4, u"Español"),
-    (5, u"Suomi"),
-    (6, u"Deutsch"),
-    (7, u"Italiano"),
-    (8, u"Български"),
-    (9, u"Hrvatski"),
-    (10, u"Magyar"),
-    (11, u"Српски"),
-    (12, u"Português"),
-    (14, u"Ελληνική"),
-    (15, u"Polski"),
-    (16, u"Français"),
-    (17, u"한국어"),
-    (18, u"汉语"),
-    (19, u"Lietuvių"),
-    (20, u"日本語"),
-    (21, u"Čeština"),
-    (22, u"Eesti"),
-    (53, u"Slovenčina"),
-    (54, u"Română"),
-    (55, u"Norsk"),
-    (56, u"Latviešu"),
-    (57, u"Azərbaycan"),
-    (58, u"Հայերեն"),
-    (59, u"Shqip"),
-    (60, u"Svenska"),
-    (61, u"Nederlands"),
-    (62, u"Türkmen"),
-    (63, u"ქართული"),
-    (64, u"Dansk"),
-    (65, u"O‘zbek"),
-    (66, u"Moldovenească"),
-    (68, u"ภาษาไทย"),
-    (69, u"Bahasa Indonesia"),
-    (70, u"Тоҷикӣ"),
-    (71, u"Slovenščina"),
-    (72, u"Bosanski"),
-    (73, u"Português brasileiro"),
-    (74, u"پارسی"),
-    (75, u"Việt Nam"),
-    (76, u"हिन्दी"),
-    (77, u"සිංහල"),
-    (78, u"বাংলা Bāṇlā"),
-    (79, u"Tagalog"),
-    (80, u"Монгол"),
-    (81, u"ဗမာစာ"),
-    (82, u"Türkçe"),
-    (83, u"नेपाली"),
-    (85, u"اردو"),
-    (86, u"Bahasa Melayu"),
-    (87, u"Кыргыз тили"),
-    (90, u"پنجابی"),
-    (91, u"Иронау"),
-    (94, u"ಕನ್ನಡ"),
-    (95, u"Kiswahili"),
-    (97, u"Қазақша"),
-    (98, u"العربية"),
-    (99, u"עברית"),
-    (100, u"Дореволюцiонный"),
-    (102, u"Адыгэбзэ"),
-    (777, u"В Союзе"),
+    (0, ('ru', u"Русский")),
+    (1, ('uk', u"Українська")),
+    (2, ('be', u"Беларуская (тарашкевiца)")),
+    (3, ('en', u"English")),
+    (4, ('es', u"Español")),
+    (5, (None, u"Suomi")),
+    (6, ('de', u"Deutsch")),
+    (7, ('it', u"Italiano")),
+    (8, ('bg', u"Български")),
+    (9, ('hr', u"Hrvatski")),
+    (10, ('sl', u"Magyar")),
+    (11, ('sr', u"Српски")),
+    (12, ('pt', u"Português")),
+    (14, ('el', u"Ελληνική")),
+    (15, ('pl', u"Polski")),
+    (16, ('fr', u"Français")),
+    (17, (None, u"한국어")),
+    (18, (None, u"汉语")),
+    (19, (None, u"Lietuvių")),
+    (20, (None, u"日本語")),
+    (21, (None, u"Čeština")),
+    (22, (None, u"Eesti")),
+    (53, ('sk', u"Slovenčina")),
+    (54, (None, u"Română")),
+    (55, ('no', u"Norsk")),
+    (56, (None, u"Latviešu")),
+    (57, (None, u"Azərbaycan")),
+    (58, (None, u"Հայերեն")),
+    (59, (None, u"Shqip")),
+    (60, ('sv', u"Svenska")),
+    (61, ('nl', u"Nederlands")),
+    (62, (None, u"Türkmen")),
+    (63, (None, u"ქართული")),
+    (64, ('da', u"Dansk")),
+    (65, (None, u"O‘zbek")),
+    (66, ('mo', u"Moldovenească")),
+    (68, (None, u"ภาษาไทย")),
+    (69, (None, u"Bahasa Indonesia")),
+    (70, (None, u"Тоҷикӣ")),
+    (71, (None, u"Slovenščina")),
+    (72, (None, u"Bosanski")),
+    (73, (None, u"Português brasileiro")),
+    (74, (None, u"پارسی")),
+    (75, (None, u"Việt Nam")),
+    (76, (None, u"हिन्दी")),
+    (77, (None, u"සිංහල")),
+    (78, (None, u"বাংলা Bāṇlā")),
+    (79, (None, u"Tagalog")),
+    (80, (None, u"Монгол")),
+    (81, (None, u"ဗမာစာ")),
+    (82, (None, u"Türkçe")),
+    (83, (None, u"नेपाली")),
+    (85, (None, u"اردو")),
+    (86, (None, u"Bahasa Melayu")),
+    (87, (None, u"Кыргыз тили")),
+    (90, (None, u"پنجابی")),
+    (91, (None, u"Иронау")),
+    (94, (None, u"ಕನ್ನಡ")),
+    (95, (None, u"Kiswahili")),
+    (97, (None, u"Қазақша")),
+    (98, (None, u"العربية")),
+    (99, (None, u"עברית")),
+    (100, (None, u"Дореволюцiонный")),
+    (102, (None, u"Адыгэбзэ")),
+    (777, (None, u"В Союзе")),
 )

vk_iframe/middleware.py

 
         # пользователь не залогинен или залогинен под другим именем
         vk_form = VkontakteIframeForm(request.GET)
+
         user = auth.authenticate(vk_form = vk_form)
         if user:
             request.user = user
             auth.login(request, user)
+
+            # устанавливаем язык пользователя
+            lang_code = vk_form.language_code()
+            if hasattr(request, 'session') and lang_code:
+                request.session['django_language'] = lang_code
+
         else:
             request.META['VKONTAKTE_LOGIN_ERRORS'] = vk_form.errors
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.