Skip to main content

Simple way to create multi language Django app.

Project description

Django Base Locale

Simple way to create multi language Django app.

1. Install package:

pip install django-base-locale

2. Register app in INSTALLED_APPS is your settings.py:

INSTALLED_APPS = [
    ...
    'base_locale.apps.BaseLocaleConfig',
    ...
]

3. Create your model with model locale:

from base_locale.models import BaseModel, BaseModelLocale


class Example(BaseModel):
    pass


class ExampleLocale(BaseModelLocale):
    pass

Your base urls.py file:

from base_locale.urls import include_locale_urls

from example.urls import urlpatterns as example_urls


app_urls = [
    *example_urls,
]

urlpatterns = [
    ...,
    path('', include_locale_urls(app_urls)),
    """
    # In base_locale/urls.py
    def include_locale_urls(urls):
        return include([ 
            re_path(r'^(?P<language>[a-z]{2})/', include(urls)),
            path('', include(urls)),
        ])
    """
]

How to use

views.py

We create class BaseLocaleContext(ContextMixin).

from django.views.generic import DetailView

from base_locale.views import BaseLocaleContext


class ExampleView(BaseLocaleContext, DetailView):
    model = Example
    template_name = 'template.html'
    context_object_name = 'example'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        """
        Now context have instance language, all languages and path with out current language slug.
            context['language']
            context['languages']
            context['path'](form request path '/ru/...' replace language slug)

        context['example'] have methods:
            property locales
                Return all locales
            property locale_default
                Use in admin panel, for display locale instance
            get_locale
                Need argument language
                Return Locale instance by language
                context['example_locale'] = context['example'].get_locale(context['language'])
        """
        return context
template.html

For getting current locale from instance load base_locale_extras

{% load base_locale_extras %}
{% bl_locale example as example_locale %}
    Now we use the locale object.
    {{ example_locale.language.code }}
    Two ways for use example instance:
    1. {{ example.pk }}
    2. {{ example_locale.base.pk }}
{% end_bl_locale %}
How to create page translation links:
{% for lang in languages %}
    <a href="{% bl_trans_url lang %}">lang.code</a>
{% endfor %}
{# or #}
{% for lang in languages %}
    <a href="{% bl_trans_url lang path %}">lang.code</a>
{% endfor %}

path is not require. path is request.path with out language code.

How to create page locale links:
{% load base_locale_extras %}
<a href="{% bl_url '...' %}"></a>
{# or #}
<a href="{% bl_url '...' *args **kwargs %}"></a>

Register tags in settings.py

TEMPLATES = [
    {
        ...
        'OPTIONS': {
            ...
            'builtins': [
                'base_locale.templatetags.base_locale_extras',
            ]  
        },
    },
]

Models reference

base_locale app has modifier django metaclass, and use new metaclass for BaseModelLocale. New metaclass was auto generation foreign key for BaseModel. See How to use. For access from BaseModelLocale to BaseModel use attribute 'base'.

BaseModel have attributes:

  • property locales: return all locales.
  • property locale_default: return locale by language__is_default is True.
  • get_locale(language): return locale by language.

BaseModelLocale have attributes:

  • property base: return instance ForeignKey for BaseModel.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_base_locale-4.2.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_base_locale-4.2.0-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file django_base_locale-4.2.0.tar.gz.

File metadata

  • Download URL: django_base_locale-4.2.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for django_base_locale-4.2.0.tar.gz
Algorithm Hash digest
SHA256 c595e1caaa3b3efba8d5916c09465ff1783445b5133b6ca68e0742aa69c94bbd
MD5 a37ba4e1e388093e8c7133c80acb0260
BLAKE2b-256 79cf3f462c7da35d953caf84b7f231a976244b4011df367f19ed810131119f33

See more details on using hashes here.

File details

Details for the file django_base_locale-4.2.0-py3-none-any.whl.

File metadata

  • Download URL: django_base_locale-4.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for django_base_locale-4.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 70b6d3da13f6ddeff3c2a0b5a1eddcb8a76aeb4ef21e27670f36b257e0fed8c9
MD5 73f89182f9097ffa9c257d79e5bc07a8
BLAKE2b-256 763244320a4ee9845d57f43e7acf1a5ad589959b2ad341e8874f84d3be9a958e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page