Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc: how to deal with i18n_patterns #148

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,34 @@ You can use it like::
{% with menu=menus.main %}{% include "bootstrap-navbar.html" %}{% endwith %}


Internacionalization
--------------------

If your project uses ``i18n_patterns`` for handling localized URLs, you should define the ``url``
argument in ``MenuItem`` as a callable instead of a static string. This ensures that the correct
localized URL is resolved based on the active language.

For example, consider the following implementation inside a ``utils/menus.py`` file:

``utils/menus.py``::

from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from simple_menu import Menu, MenuItem

Menu.add_item(
"main",
MenuItem(
_("Awesome App"), # Title translated using gettext_lazy
lambda request: reverse("portal:index"), # URL resolved dynamically
icon="fa-solid fa-circle",
check=lambda request: request.user.is_authenticated,
),
)

When using ``i18n_patterns``, Django assigns language-specific prefixes to URLs (e.g., ``/en/portal/index/`` for English and ``/fr/portal/index/`` for French). By defining the ``url`` as a callable (``lambda request: reverse("portal:index")``), Django ensures that it resolves the correct localized URL dynamically at runtime, rather than hardcoding it.


Check generalizations
---------------------

Expand Down