當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。