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.
Metadata
Release files for django-base-locale 4.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_base_locale-4.2.0.tar.gz | 8.8 kB | Without provenance | Details for django_base_locale-4.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_base_locale-4.2.0-py3-none-any.whl | Python 3 | none | any | Details for django_base_locale-4.2.0-py3-none-any.whl |
Total release size: 20.5 kB
Release files / django_base_locale-4.2.0.tar.gz
| Download URL | django_base_locale-4.2.0.tar.gz |
|---|---|
| Size | 8.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c595e1caaa3b3efba8d5916c09465ff1783445b5133b6ca68e0742aa69c94bbd
|
|
BLAKE2b-256 checksum How to use checksums |
79cf3f462c7da35d953caf84b7f231a976244b4011df367f19ed810131119f33
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / django_base_locale-4.2.0-py3-none-any.whl
| Download URL | django_base_locale-4.2.0-py3-none-any.whl |
|---|---|
| Size | 11.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
70b6d3da13f6ddeff3c2a0b5a1eddcb8a76aeb4ef21e27670f36b257e0fed8c9
|
|
BLAKE2b-256 checksum How to use checksums |
763244320a4ee9845d57f43e7acf1a5ad589959b2ad341e8874f84d3be9a958e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Security for django-base-locale 4.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.