from django.urls import reverse
from .get_handler import get_handler
[docs]def i18n_reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None, languagecode=None):
"""
Serves kind of the same use case as the ``django.urls.reverse`` function, but with i18n URL support, AND
this function returns an absolute URL.
The reason why it returns absolute URL is because i18n URLs may be based on domains, not just URL paths.
NOTE: Session based `ievv_i18n_url` handlers will ignore the languagecode argument and just return
the URL for the default translation. This is because all their translations live at the same URL.
See the *A warning about session based translations* in the docs for more details.
Args:
viewname: See the docs for ``django.urls.reverse``.
urlconf: See the docs for ``django.urls.reverse``. Defaults to None.
args: See the docs for ``django.urls.reverse``. Defaults to None.
kwargs: See the docs for ``django.urls.reverse``. Defaults to None.
current_app: See the docs for ``django.urls.reverse``. Defaults to None.
languagecode (str, optional): The languagecode to reverse the URL in. Defaults to None, which means
we reverse the URL in the current languagecode.
Returns:
str: An URL.
"""
path = reverse(viewname=viewname, urlconf=urlconf, args=args, kwargs=kwargs, current_app=current_app)
return get_handler().build_absolute_url(path=path, languagecode=languagecode)