utils.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:pyconjp-website 作者: pyconjp 项目源码 文件源码
def locale_path(path, locale=''):
    """
    Generate the localeurl-enabled path from a path without locale prefix. If
    the locale is empty settings.LANGUAGE_CODE is used.
    """

    locale = supported_language(locale)
    if not locale:
        locale = supported_language(settings.LANGUAGE_CODE)
    if is_locale_independent(path):
        return path
    elif is_default_locale(locale) and not localeurl_settings.PREFIX_DEFAULT_LOCALE:
        return path
    else:
        tmp = path.split("/")

        if len(tmp) < 3:
            if tmp[1] == URL_PREFIX:
                tmp.append("")
            else:
                raise Http404()
        elif len(tmp) < 2:
            tmp.insert(1, URL_PREFIX)
        elif tmp[1] == locale:
            tmp[1] = URL_PREFIX
        tmp_locale = tmp[2]
        if tmp_locale == locale:
            return path
        if tmp_locale in localeurl_settings.SUPPORTED_LOCALES:
            tmp[2] = locale
        else:
            tmp.insert(2, locale)
        return "/".join(tmp)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号