Django app to wrap models fields to gettext calling for the given apps
Project description
Django-translate
This is a django app that allows you to wrap django models fields with a translation field (not for Pycharm and docker-compose python interpretator).
It uses the django model translation and gettext package to achieve this.
Installation
poetry add django-translate-gettext
# or
poetry add git+https://github.com/alpden550/django-translate.git
Usage
Add django_translate
to your INSTALLED_APPS
setting like this:
INSTALLED_APPS = [
...,
'django_translate_gettext',
...
]
Call django commands to create the translation fields for your apps, use --format
flag to call ruff format tool after files changed.
Example:
python manage.py translate app1 app2 app3 app4 --format
Model file before:
import csv
from pathlib import Path
from django.db import models
class UserTypes(models.TextChoices):
ADMIN = "admin", "Admin"
USER = "user", "User"
STAFF = "staff", "Staff"
class CustomUser(models.Model):
class _InnerTypes(models.TextChoices):
NEW = "new", "New"
OLD = "old", "Old"
username = models.CharField("User Name", max_length=100, help_text="This is the help text")
email = models.EmailField()
password = models.CharField(max_length=100)
created_at = models.DateTimeField("Date Created", auto_now_add=True)
updated_at = models.DateTimeField(verbose_name="Date Updated", auto_now=True)
first_name = models.CharField(verbose_name="First_Name", max_length=25)
last_name = models.CharField("Last_Name", max_length=25)
is_active = models.BooleanField(default=True)
choices = models.CharField(max_length=5, choices=UserTypes.choices, default=UserTypes.USER)
groups = models.ManyToManyField("auth.Group", verbose_name="Custom Groups", blank=True)
owner = models.ForeignKey("auth.User", related_name="users", on_delete=models.CASCADE, blank=True, null=True)
class Meta:
verbose_name = "Own Custom User"
verbose_name_plural = "Own Custom Users"
ordering = ("-created_at",)
get_latest_by = ("-created_at", "is_active")
Model file after:
import csv
from pathlib import Path
from django.db import models
from django.utils.translation import gettext_lazy as _
class UserTypes(models.TextChoices):
ADMIN = ("admin", _("Admin"))
USER = ("user", _("User"))
STAFF = ("staff", _("Staff"))
class CustomUser(models.Model):
class _InnerTypes(models.TextChoices):
NEW = ("new", _("New"))
OLD = ("old", _("Old"))
username = models.CharField(_("User Name"), max_length=100, help_text=_("This is the help text"))
email = models.EmailField(_("Email"))
password = models.CharField(verbose_name=_("Password"), max_length=100)
created_at = models.DateTimeField(_("Date Created"), auto_now_add=True)
updated_at = models.DateTimeField(verbose_name=_("Date Updated"), auto_now=True)
first_name = models.CharField(verbose_name=_("First_Name"), max_length=25)
last_name = models.CharField(_("Last_Name"), max_length=25)
is_active = models.BooleanField(verbose_name=_("Is_Active"), default=True)
choices = models.CharField(
verbose_name=_("Choices"), max_length=5, choices=UserTypes.choices, default=UserTypes.USER
)
groups = models.ManyToManyField("auth.Group", verbose_name=_("Custom Groups"), blank=True)
owner = models.ForeignKey(
"auth.User", verbose_name=_("Owner"), related_name="users", on_delete=models.CASCADE, blank=True, null=True
)
class Meta:
verbose_name = _("Own Custom User")
verbose_name_plural = _("Own Custom Users")
ordering = ("-created_at",)
get_latest_by = ("-created_at", "is_active")
Don't forget to check the changes before call makemigrations
and migrate
commands.
Don't forget to fill the settings.py like example file with the recommended Django settings before calling the makemessaging
command.
# settings.py
LANGUAGE_CODE = "en"
USE_I18N = True
USE_L10N = True
USE_TZ = True
LOCALE_PATHS = [Path(BASE_DIR, "locale")]
LANGUAGES = [
("en", "English"),
("ru", "Russian"),
]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_translate_gettext-0.1.7.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bdbb22f7349d1abcc353dd14ff8fd368012868eeb4e59fc57f05a414c3cbad8 |
|
MD5 | 100c0207882e777994ccadb03ee4be13 |
|
BLAKE2b-256 | b2213bcf779752ce6eda42140b05da45ceba4061873844973a0b92b92ae1572a |
Hashes for django_translate_gettext-0.1.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0647bc4d6b62706977399c78c5acbd11ab133930694a7581e47369d5c927e9d2 |
|
MD5 | afafdbd11704d93d8453d967f1fedf9a |
|
BLAKE2b-256 | b2f7120f91c4ca3f4d1b934bf8170d5883f81ad0c984bc45dad9eea9af2673fc |