当前位置: 首页>>代码示例>>Python>>正文


Python translation.check_for_language方法代码示例

本文整理汇总了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 
开发者ID:znick,项目名称:anytask,代码行数:26,代码来源:views.py

示例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),
        }
    ) 
开发者ID:yunity,项目名称:karrot-backend,代码行数:27,代码来源:tasks.py

示例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 
开发者ID:blackye,项目名称:luscan-devel,代码行数:27,代码来源:i18n.py

示例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 
开发者ID:blackye,项目名称:luscan-devel,代码行数:21,代码来源:formats.py

示例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 
开发者ID:erigones,项目名称:esdc-ce,代码行数:24,代码来源:views.py

示例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 
开发者ID:CiscoSystems,项目名称:avos,代码行数:27,代码来源:forms.py

示例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 
开发者ID:astrobin,项目名称:astrobin,代码行数:26,代码来源:__init__.py

示例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 
开发者ID:astrobin,项目名称:astrobin,代码行数:24,代码来源:__init__.py

示例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 
开发者ID:yunity,项目名称:karrot-backend,代码行数:28,代码来源:email_utils.py

示例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 
开发者ID:yunity,项目名称:karrot-backend,代码行数:9,代码来源:emails.py

示例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 
开发者ID:evernote,项目名称:zing,代码行数:11,代码来源:override.py

示例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 
开发者ID:pythonkr,项目名称:pyconkr-2014,代码行数:16,代码来源:views.py

示例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 
开发者ID:NicolasLM,项目名称:feedsubs,代码行数:16,代码来源:utils.py


注:本文中的django.utils.translation.check_for_language方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。