Python django.utils.translation.check_for_language() Examples
The following are 13
code examples of django.utils.translation.check_for_language().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.utils.translation
, or try the search function
.
Example #1
Source File: views.py From anytask with MIT License | 6 votes |
def set_user_language(request): next = request.REQUEST.get('next') if not is_safe_url(url=next, host=request.get_host()): next = request.META.get('HTTP_REFERER') if not is_safe_url(url=next, host=request.get_host()): next = '/' response = HttpResponseRedirect(next) if request.method == 'POST': lang_code = request.POST.get('language', None) if 'ref' not in next: ref = urlparse(request.POST.get('referrer', next)) response = HttpResponseRedirect('?ref='.join([next, ref.path])) if lang_code and check_for_language(lang_code): if hasattr(request, 'session'): request.session['django_language'] = lang_code else: response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code) user = request.user if user.is_authenticated(): user_profile = user.profile user_profile.language = lang_code user_profile.save() return response
Example #2
Source File: tasks.py From karrot-backend with GNU Affero General Public License v3.0 | 6 votes |
def notify_message_push_subscribers_with_language(message, subscriptions, language): conversation = message.conversation if not translation.check_for_language(language): language = 'en' with translation.override(language): message_title = get_message_title(message, language) if message.is_thread_reply(): click_action = frontend_urls.thread_url(message.thread) else: click_action = frontend_urls.conversation_url(conversation, message.author) notify_subscribers_by_device( subscriptions, click_action=click_action, fcm_options={ 'message_title': message_title, 'message_body': Truncator(message.content).chars(num=1000), # this causes each notification for a given conversation to replace previous notifications # fancier would be to make the new notifications show a summary not just the latest message 'tag': 'conversation:{}'.format(conversation.id), } )
Example #3
Source File: i18n.py From luscan-devel with GNU General Public License v2.0 | 6 votes |
def set_language(request): """ Redirect to a given url while setting the chosen language in the session or cookie. The url and the language code need to be specified in the request parameters. Since this view changes how the user will see the rest of the site, it must only be accessed as a POST request. If called as a GET request, it will redirect to the page in the request (the 'next' parameter) without changing any state. """ next = request.REQUEST.get('next') if not is_safe_url(url=next, host=request.get_host()): next = request.META.get('HTTP_REFERER') if not is_safe_url(url=next, host=request.get_host()): next = '/' response = http.HttpResponseRedirect(next) if request.method == 'POST': lang_code = request.POST.get('language', None) if lang_code and check_for_language(lang_code): if hasattr(request, 'session'): request.session['django_language'] = lang_code else: response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code) return response
Example #4
Source File: formats.py From luscan-devel with GNU General Public License v2.0 | 6 votes |
def iter_format_modules(lang): """ Does the heavy lifting of finding format modules. """ if check_for_language(lang): format_locations = ['django.conf.locale.%s'] if settings.FORMAT_MODULE_PATH: format_locations.append(settings.FORMAT_MODULE_PATH + '.%s') format_locations.reverse() locale = to_locale(lang) locales = [locale] if '_' in locale: locales.append(locale.split('_')[0]) for location in format_locations: for loc in locales: try: yield import_module('.formats', location % loc) except ImportError: pass
Example #5
Source File: views.py From esdc-ce with Apache License 2.0 | 6 votes |
def setlang(request): """ Sets a user's language preference and redirects to a given URL or, by default, back to the previous page. """ next = request.GET.get('next', None) if not is_safe_url(url=next, host=request.get_host()): next = request.META.get('HTTP_REFERER') if not is_safe_url(url=next, host=request.get_host()): next = '/' response = redirect(next) lang_code = request.GET.get('language', None) if lang_code and check_for_language(lang_code): if hasattr(request, 'session'): request.session[LANGUAGE_SESSION_KEY] = lang_code else: response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code, max_age=settings.LANGUAGE_COOKIE_AGE, path=settings.LANGUAGE_COOKIE_PATH, domain=settings.LANGUAGE_COOKIE_DOMAIN) return response
Example #6
Source File: forms.py From avos with Apache License 2.0 | 6 votes |
def handle(self, request, data): response = shortcuts.redirect(request.build_absolute_uri()) # Language lang_code = data['language'] if lang_code and translation.check_for_language(lang_code): if hasattr(request, 'session'): request.session['django_language'] = lang_code response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code, expires=_one_year()) # Timezone request.session['django_timezone'] = pytz.timezone( data['timezone']).zone response.set_cookie('django_timezone', data['timezone'], expires=_one_year()) request.session['horizon_pagesize'] = data['pagesize'] response.set_cookie('horizon_pagesize', data['pagesize'], expires=_one_year()) with translation.override(lang_code): messages.success(request, encoding.force_text(_("Settings saved."))) return response
Example #7
Source File: __init__.py From astrobin with GNU Affero General Public License v3.0 | 6 votes |
def user_profile_save_preferences(request): """Saves the form""" profile = request.user.userprofile form = UserProfileEditPreferencesForm(data=request.POST, instance=profile) response_dict = {'form': form} response = HttpResponseRedirect("/profile/edit/preferences/") if form.is_valid(): form.save() # Activate the chosen language from django.utils.translation import check_for_language, activate lang = form.cleaned_data['language'] if lang and check_for_language(lang): if hasattr(request, 'session'): request.session['django_language'] = lang response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang) activate(lang) else: return render(request, "user/profile/edit/preferences.html", response_dict) messages.success(request, _("Form saved. Thank you!")) return response
Example #8
Source File: __init__.py From astrobin with GNU Affero General Public License v3.0 | 6 votes |
def set_language(request, lang): from django.utils.translation import check_for_language, activate next = request.GET.get('next', None) if not next: next = request.META.get('HTTP_REFERER', None) if not next: next = '/' response = HttpResponseRedirect(next) if lang and check_for_language(lang): if hasattr(request, 'session'): request.session['django_language'] = lang response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang) activate(lang) if request.user.is_authenticated(): profile = request.user.userprofile profile.language = lang profile.save(keep_deleted=True) return response
Example #9
Source File: email_utils.py From karrot-backend with GNU Affero General Public License v3.0 | 5 votes |
def prepare_email_content(template, context, tz, language='en'): if not translation.check_for_language(language): language = 'en' with timezone.override(tz), translation.override(language): html_content = None try: html_template = get_template('{}.html.jinja2'.format(template)) html_content = html_template.render(context) except TemplateDoesNotExist: pass try: text_template = get_template('{}.text.jinja2'.format(template)) text_content = text_template.render(context) except TemplateDoesNotExist: if html_content: text_content = generate_plaintext_from_html(html_content) else: raise Exception('Nothing to use for text content, no text or html templates available.') subject = render_to_string('{}.subject.jinja2'.format(template), context).replace('\n', '') return subject, text_content, html_content
Example #10
Source File: emails.py From karrot-backend with GNU Affero General Public License v3.0 | 5 votes |
def language_for_user(user): language = user.language if not translation.check_for_language(language): language = 'en' return language
Example #11
Source File: override.py From zing with GNU General Public License v3.0 | 5 votes |
def hijack_translation(): """Sabotage Django's fascist linguistical regime.""" # Override functions that check if language is known to Django translation.check_for_language = lambda lang_code: True trans_real.check_for_language = lambda lang_code: True translation.get_language_from_request = get_language_from_request # Override django's inadequate bidi detection translation.get_language_bidi = get_language_bidi
Example #12
Source File: views.py From pyconkr-2014 with MIT License | 5 votes |
def setlang(request, lang_code): # Copied from django.views.i18n.set_language next = request.REQUEST.get('next') if not is_safe_url(url=next, host=request.get_host()): next = request.META.get('HTTP_REFERER') if not is_safe_url(url=next, host=request.get_host()): next = '/' response = HttpResponseRedirect(next) if lang_code and check_for_language(lang_code): if hasattr(request, 'session'): request.session['django_language'] = lang_code else: response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code) return response
Example #13
Source File: utils.py From feedsubs with MIT License | 5 votes |
def set_session_language_if_necessary(request, user): preferred_language = user.um_profile.language if request.LANGUAGE_CODE == preferred_language: return if not translation.check_for_language(preferred_language): logger.warning('Invalid language %s for user %s', preferred_language, user) return logger.info('Set language %s for user session %s', preferred_language, user) translation.activate(preferred_language) request.LANGUAGE_CODE = translation.get_language() request.session[translation.LANGUAGE_SESSION_KEY] = preferred_language