本文整理汇总了Python中django.utils.translation.check_for_language方法的典型用法代码示例。如果您正苦于以下问题:Python translation.check_for_language方法的具体用法?Python translation.check_for_language怎么用?Python translation.check_for_language使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.translation
的用法示例。
在下文中一共展示了translation.check_for_language方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_user_language
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import check_for_language [as 别名]
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
示例2: notify_message_push_subscribers_with_language
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import check_for_language [as 别名]
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),
}
)
示例3: set_language
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import check_for_language [as 别名]
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
示例4: iter_format_modules
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import check_for_language [as 别名]
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
示例5: setlang
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import check_for_language [as 别名]
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
示例6: handle
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import check_for_language [as 别名]
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
示例7: user_profile_save_preferences
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import check_for_language [as 别名]
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
示例8: set_language
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import check_for_language [as 别名]
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
示例9: prepare_email_content
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import check_for_language [as 别名]
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
示例10: language_for_user
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import check_for_language [as 别名]
def language_for_user(user):
language = user.language
if not translation.check_for_language(language):
language = 'en'
return language
示例11: hijack_translation
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import check_for_language [as 别名]
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
示例12: setlang
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import check_for_language [as 别名]
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
示例13: set_session_language_if_necessary
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import check_for_language [as 别名]
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