Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 0 additions & 6 deletions {{ cookiecutter.name }}/.github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ jobs:
id: setup-project
uses: ./.github/actions/setup-project

- name: install locale stuff
uses: awalsh128/cache-apt-pkgs-action@v1
with:
packages: locales-all gettext
version: 1

- name: test
env:
DATABASE_URL: postgres://postgres:secret@localhost:5432/postgres
Expand Down
2 changes: 1 addition & 1 deletion {{ cookiecutter.name }}/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ ENV STATIC_ROOT=/code/static
ENV UID=999 GID=999

RUN apt-get update \
&& apt-get --no-install-recommends install -y gettext locales-all tzdata git wait-for-it wget tini \
&& apt-get --no-install-recommends install -y locales-all tzdata git wait-for-it wget tini \
&& rm -rf /var/lib/apt/lists/*

RUN groupadd --system --gid=${GID} "web" \
Expand Down
2 changes: 2 additions & 0 deletions {{ cookiecutter.name }}/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies = [
"drf-spectacular[sidecar]>=0.30.0",
"orjson>=3.11.9",
"pillow>=12.3.0",
"polib>=1.2.0",
"psycopg[binary]>=3.3.4",
"redis>=8.0.1",
"sentry-sdk>=2.66.0",
Expand Down Expand Up @@ -48,6 +49,7 @@ dev = [
"toml-sort>=0.24.4",
"types-freezegun>=1.1.10",
"types-pillow>=10.2.0.20240822",
"types-polib>=1.2.0.20260518",
]

[tool.uv]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from pathlib import Path
from typing import Any

import polib
from django.conf import settings
from django.core.management.base import BaseCommand, CommandParser


class Command(BaseCommand):
"""Compile .po catalogues in LOCALE_PATHS with polib, so GNU gettext is not needed"""

help = "Compiles .po files to .mo files for use with builtin gettext support."

def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument("--locale", "-l", action="append", default=[], help="Locale(s) to process. Default is to process all.")

def handle(self, *args: Any, **options: Any) -> None:
for locale_path in settings.LOCALE_PATHS:
for po_file in sorted(Path(locale_path).glob("*/LC_MESSAGES/*.po")):
if options["locale"] and po_file.parent.parent.name not in options["locale"]:
continue

if options["verbosity"] > 0:
self.stdout.write(f"processing file {po_file.name} in {po_file.parent}")

polib.pofile(str(po_file)).save_as_mofile(str(po_file.with_suffix(".mo")))
60 changes: 60 additions & 0 deletions {{ cookiecutter.name }}/src/app/tests/test_compilemessages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import gettext

import pytest
from django.core.management import call_command


@pytest.fixture
def messages_dir(tmp_path, settings):
settings.LOCALE_PATHS = [tmp_path]
messages_dir = tmp_path / "ru" / "LC_MESSAGES"
messages_dir.mkdir(parents=True)
(messages_dir / "django.po").write_text(
r"""#, fuzzy
msgid ""
msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n"

msgid "Active"
msgstr "Активна"

msgid "day"
msgid_plural "days"
msgstr[0] "день"
msgstr[1] "дня"
msgstr[2] "дней"

#, fuzzy
msgid "Paused"
msgstr "Приостановлена"
"""
)
return messages_dir


@pytest.fixture
def translations(messages_dir):
call_command("compilemessages", verbosity=0)

with (messages_dir / "django.mo").open("rb") as mo_file:
return gettext.GNUTranslations(mo_file)


def test_translates(translations):
assert translations.gettext("Active") == "Активна"


@pytest.mark.parametrize(("count", "expected"), [(1, "день"), (3, "дня"), (5, "дней"), (11, "дней"), (21, "день")])
def test_keeps_plural_forms(translations, count, expected):
assert translations.ngettext("day", "days", count) == expected


def test_skips_fuzzy_messages(translations):
assert translations.gettext("Paused") == "Paused"


def test_skips_other_locales(messages_dir):
call_command("compilemessages", locale=["en"], verbosity=0)

assert not (messages_dir / "django.mo").exists()
26 changes: 22 additions & 4 deletions {{ cookiecutter.name }}/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading