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)
评论列表
文章目录