本文整理汇总了Python中django.utils.translation.get_language_from_request方法的典型用法代码示例。如果您正苦于以下问题:Python translation.get_language_from_request方法的具体用法?Python translation.get_language_from_request怎么用?Python translation.get_language_from_request使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.translation
的用法示例。
在下文中一共展示了translation.get_language_from_request方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: tracker_context
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import get_language_from_request [as 别名]
def tracker_context(request, qdict=None):
starttime = datetime.datetime.now()
language = translation.get_language_from_request(request)
translation.activate(language)
request.LANGUAGE_CODE = translation.get_language()
profile = None
qdict = qdict or {}
qdict.update(
{
'djangoversion': dv(),
'pythonversion': pv(),
'user': request.user,
'profile': profile,
'next': request.POST.get('next', request.GET.get('next', request.path)),
'starttime': starttime,
'events': tracker.models.Event.objects.all(),
'settings': settings,
}
)
qdict.setdefault('event', viewutil.get_event(None))
qdict.setdefault('user', request.user)
return qdict
示例2: redirect_homepage
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import get_language_from_request [as 别名]
def redirect_homepage(self, request):
if request.path not in settings.REDIRECTED_PATHS:
return
language = get_language_from_request(request)
if language not in self.LANGUAGES:
language = settings.DEFAULT_LANGUAGE
querystring = request.META.get('QUERY_STRING', '')
if querystring:
querystring = '?' + querystring
return HttpResponseRedirect(
''.join([
'http://', language, '.', settings.BASE_DOMAIN,
request.path, querystring
])
)
示例3: get_language_from_request
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import get_language_from_request [as 别名]
def get_language_from_request(request, check_path=False):
"""Try to get the user's preferred language by first checking the
cookie and then by checking the HTTP language headers.
If all fails, try fall back to default language.
"""
supported = dict(supported_langs())
for lang_getter in (
get_lang_from_session,
get_lang_from_cookie,
get_lang_from_http_header,
):
lang = lang_getter(request, supported)
if lang is not None:
return lang
from django.conf import settings
return settings.LANGUAGE_CODE
示例4: retrieve
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import get_language_from_request [as 别名]
def retrieve(self, request, pk, version, kind):
"""
Return a single item by ID
"""
# Wrap the ES get in a try/catch to we control the exception we emit — it would
# raise and end up in a 500 error otherwise
try:
query_response = ES_CLIENT.get(
index=self._meta.indexer.index_name,
doc_type=self._meta.indexer.document_type,
id=pk,
)
except NotFoundError:
raise NotFound
# Format a clean category object as a response
return Response(
self._meta.indexer.format_es_object_for_api(
query_response,
# Get the best language we can return multilingual fields in
get_language_from_request(request),
)
)
示例5: process_request
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import get_language_from_request [as 别名]
def process_request(self, request):
check_path = self.is_language_prefix_patterns_used()
language = translation.get_language_from_request(
request, check_path=check_path)
translation.activate(language)
request.LANGUAGE_CODE = translation.get_language()
示例6: process_request
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import get_language_from_request [as 别名]
def process_request(self, request):
urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
i18n_patterns_used, prefixed_default_language = is_language_prefix_patterns_used(urlconf)
language = translation.get_language_from_request(request, check_path=i18n_patterns_used)
language_from_path = translation.get_language_from_path(request.path_info)
if not language_from_path and i18n_patterns_used and not prefixed_default_language:
language = settings.LANGUAGE_CODE
translation.activate(language)
request.LANGUAGE_CODE = translation.get_language()
示例7: hijack_translation
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import get_language_from_request [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
示例8: autocomplete
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import get_language_from_request [as 别名]
def autocomplete(self, request, version, kind):
"""
Use the "completion" field on the categories mapping & objects to provide autocomplete
functionality through an API endpoint.
We cannot reuse the `AutocompleteMixin` as it does not handle "kinds" as categories need.
"""
# Get a hold of the relevant indexer
indexer = self._meta.indexer
# Query our specific ES completion field
autocomplete_query_response = ES_CLIENT.search(
index=indexer.index_name,
doc_type=indexer.document_type,
body={
"suggest": {
"categories": {
"prefix": request.query_params["query"],
"completion": {
"contexts": {"kind": [kind]},
"field": "complete.{:s}".format(
get_language_from_request(request)
),
},
}
}
},
)
# Build a response array from the list of completion options
return Response(
[
indexer.format_es_document_for_autocomplete(
option, get_language_from_request(request)
)
for option in autocomplete_query_response["suggest"]["categories"][0][
"options"
]
]
)
示例9: autocomplete
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import get_language_from_request [as 别名]
def autocomplete(self, request, version):
"""
Use the "completion" field on the organization mapping & objects to provide autocomplete
functionality through an API endpoint.
"""
# This mixin is intended to be used on ViewSets. It requires a _meta attribute holding
# the relevant indexer
indexer = self._meta.indexer
# Query our specific ES completion field
autocomplete_query_response = ES_CLIENT.search(
index=indexer.index_name,
doc_type=indexer.document_type,
body={
"suggest": {
"organizations": {
"prefix": request.query_params["query"],
"completion": {
"field": "complete.{:s}".format(
get_language_from_request(request)
)
},
}
}
},
)
# Build a response array from the list of completion options
return Response(
[
indexer.format_es_document_for_autocomplete(
option, get_language_from_request(request)
)
for option in autocomplete_query_response["suggest"]["organizations"][
0
]["options"]
]
)
示例10: process_request
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import get_language_from_request [as 别名]
def process_request(self, request):
language = translation.get_language_from_request(
request, check_path=self.is_language_prefix_patterns_used)
translation.activate(language)
request.LANGUAGE_CODE = translation.get_language()
示例11: get_language_for_user
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import get_language_from_request [as 别名]
def get_language_for_user(self, request):
if request.user.is_authenticated():
try:
account = Account.objects.get(user=request.user)
return account.language
except Account.DoesNotExist:
pass
return translation.get_language_from_request(request)
示例12: create
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import get_language_from_request [as 别名]
def create(cls, request=None, **kwargs):
create_email = kwargs.pop("create_email", True)
confirm_email = kwargs.pop("confirm_email", None)
account = cls(**kwargs)
if "language" not in kwargs:
if request is None:
account.language = settings.LANGUAGE_CODE
else:
account.language = translation.get_language_from_request(request, check_path=True)
account.save()
if create_email and account.user.email:
kwargs = {"primary": True}
if confirm_email is not None:
kwargs["confirm"] = confirm_email
return account
示例13: __init__
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import get_language_from_request [as 别名]
def __init__(self, request=None):
self.user = AnonymousUser()
self.timezone = settings.TIME_ZONE
if request is None:
self.language = settings.LANGUAGE_CODE
else:
self.language = translation.get_language_from_request(request, check_path=True)
示例14: get_global_context
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import get_language_from_request [as 别名]
def get_global_context(request, enterprise_customer=None):
"""
Get the set of variables that are needed by default across views.
"""
platform_name = get_configuration_value("PLATFORM_NAME", settings.PLATFORM_NAME)
# pylint: disable=no-member
context = {
'enterprise_customer': enterprise_customer,
'LMS_SEGMENT_KEY': settings.LMS_SEGMENT_KEY,
'LANGUAGE_CODE': get_language_from_request(request),
'tagline': get_configuration_value("ENTERPRISE_TAGLINE", settings.ENTERPRISE_TAGLINE),
'platform_description': get_configuration_value(
"PLATFORM_DESCRIPTION",
settings.PLATFORM_DESCRIPTION,
),
'LMS_ROOT_URL': settings.LMS_ROOT_URL,
'platform_name': platform_name,
'header_logo_alt_text': _('{platform_name} home page').format(platform_name=platform_name),
'welcome_text': constants.WELCOME_TEXT.format(platform_name=platform_name),
}
if enterprise_customer is not None:
context.update({
'enterprise_welcome_text': constants.ENTERPRISE_WELCOME_TEXT.format(
enterprise_customer_name=enterprise_customer.name,
platform_name=platform_name,
strong_start='<strong>',
strong_end='</strong>',
line_break='<br/>',
privacy_policy_link_start="<a href='{pp_url}' target='_blank'>".format(
pp_url=get_configuration_value('PRIVACY', 'https://www.edx.org/edx-privacy-policy', type='url'),
),
privacy_policy_link_end="</a>",
),
})
return context
示例15: language
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import get_language_from_request [as 别名]
def language(self):
return get_language_from_request(self.request, check_path=True)