Repository: spiral-project/ihatemoney (language: python)

Issue:
Do not version the compiled catalogs
I just noticed `*.mo` files are versioned in the git repository.
While they must be included in python packages, they do not need to be in the git repository. They make the repository bigger, and as git retrieves all the commit when cloning, with enough time this can become noticeable. They can also induce unnecessary conflicts.

Catalog can be compiled just in time when tests are launched (via a pytest fixture [for example](https://github.com/yaal-coop/canaille/blob/main/tests/conftest.py#L14-L21)) or when the python packages are built (via setuptools/hatch/poetry/etc. hooks).

What do you think? If this OK with you I can submit a PR.


Discussion / hints:
I agree it's not ideal, but it's intended. Several years ago we converged on this behaviour, but I couldn't find the discussion. I believe it was because we sometimes forgot to rebuild translations for releases, and maybe there was an issue with weblate.

I'm completely open to revisit this, your help would be appreciated! Things to keep in mind:

- pypi release (semi-automated, see contributing doc)
- docker publication (automated in CI)
- local development
- tests (but I think we don't use translations in tests)
- possible interactions with weblate?
Hi @azmeuk, and thanks for your feedback on this.

I'm also open to revisit this, as the current behavior sometimes leads to malfunctions. Don't hesitate to submit a pull request, I'm +1 for it.
As I wrote in #1243 it might be better to move away from setuptools someday, and replacement tools would allow us to run some custom code at build time (such as catalog compilation). I have not seen this with setuptools+setup.cfg but I think we could do this with setuptools+setup.py

Anyways, I know that at least [hatch](https://github.com/pypa/hatch) and [poetry](https://python-poetry.org/) would allow this. We [moved to hatch in wtforms recently](https://github.com/wtforms/wtforms/pull/798) and [catalog compilation at build time works](https://github.com/wtforms/wtforms/blob/master/hatch_build.py), and there is no issue with weblate so far. As tox builds the packages, unit tests run with tox will have compiled catalogs. For people who would like to use pytest outside a tox environment, we can write an auto-used pytest fixture as [we did in canaille](https://github.com/yaal-coop/canaille/blob/main/tests/conftest.py#L14-L21).

I would suggest if/when #1243 is merged to move to hatch. What do you think?
I really like the idea to move away from setuptools. Hatch seems less magic than poetry to me, which makes it more palatable. I've also used it in the past without trouble.

Thanks for bringing this, let's do it :-)

Fix patch:
diff --git a/.dockerignore b/.dockerignore
index c36a60b55..3e33847da 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -14,9 +14,7 @@ CONTRIBUTORS
 docker-compose.*
 Dockerfile
 docs
-LICENSE
 Makefile
 MANIFEST.in
-README.md
 SECURITY.md
 tox.ini
diff --git a/.gitignore b/.gitignore
index 1ec6964a7..ef4cc7718 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
 *.pyc
 *.egg-info
+*.mo
 dist
 .venv
 docs/_build/
diff --git a/MANIFEST.in b/MANIFEST.in
deleted file mode 100644
index d39072416..000000000
--- a/MANIFEST.in
+++ /dev/null
@@ -1,3 +0,0 @@
-include *.md
-recursive-include ihatemoney *.py *.yaml *.po *.mo *.html *.css *.js *.eot *.svg *.woff *.txt *.png *.webp *.ini *.cfg *.j2 *.jpg *.gif *.ico *.xml
-include LICENSE CONTRIBUTORS
diff --git a/Makefile b/Makefile
index 47bbdc02d..bf917b601 100644
--- a/Makefile
+++ b/Makefile
@@ -36,7 +36,7 @@ remove-install-stamp:
 update: remove-install-stamp install ## Update the dependencies
 
 .PHONY: serve
-serve: install ## Run the ihatemoney server
+serve: install build-translations ## Run the ihatemoney server
 	@echo 'Running ihatemoney on http://localhost:5000'
 	FLASK_DEBUG=1 FLASK_APP=ihatemoney.wsgi $(VENV)/bin/flask run --host=0.0.0.0
 
@@ -74,8 +74,8 @@ compress-assets: compress-showcase ## Compress static assets
 build-translations: ## Build the translations
 	$(VENV)/bin/pybabel compile -d ihatemoney/translations
 
-.PHONY: update-translations
-update-translations: ## Extract new translations from source code
+.PHONY: extract-translations
+extract-translations: ## Extract new translations from source code
 	$(VENV)/bin/pybabel extract --add-comments "I18N:" --strip-comments --omit-header --no-location --mapping-file ihatemoney/babel.cfg -o ihatemoney/messages.pot ihatemoney
 	$(VENV)/bin/pybabel update -i ihatemoney/messages.pot -d ihatemoney/translations/
 
diff --git a/docs/contributing.md b/docs/contributing.md
index a86f73211..78d5a8063 100644
--- a/docs/contributing.md
+++ b/docs/contributing.md
@@ -274,10 +274,9 @@ In order to issue a new release, follow the following steps:
 
         make compress-assets
 
--   Build the translations:
+-   Extract the translations:
 
-        make update-translations
-        make build-translations
+        make extract-translations
 
 -   If you're not completely sure of yourself at this point, you can
     optionally: create a new branch, push it, open a pull request, check
diff --git a/hatch_build.py b/hatch_build.py
new file mode 100644
index 000000000..d536daed0
--- /dev/null
+++ b/hatch_build.py
@@ -0,0 +1,11 @@
+import sys
+
+from hatchling.builders.hooks.plugin.interface import BuildHookInterface
+
+
+class CustomBuildHook(BuildHookInterface):
+    def initialize(self, version, build_data):
+        sys.path.insert(0, "./ihatemoney")
+        from babel_utils import compile_catalogs
+
+        compile_catalogs()
diff --git a/ihatemoney/babel_utils.py b/ihatemoney/babel_utils.py
new file mode 100644
index 000000000..a498bce3e
--- /dev/null
+++ b/ihatemoney/babel_utils.py
@@ -0,0 +1,11 @@
+from pathlib import Path
+
+from babel.messages.frontend import compile_catalog
+
+
+def compile_catalogs():
+    cmd = compile_catalog()
+    cmd.directory = Path(__file__).parent / "translations"
+    cmd.statistics = True
+    cmd.finalize_options()
+    cmd.run()
diff --git a/ihatemoney/translations/bn/LC_MESSAGES/messages.mo b/ihatemoney/translations/bn/LC_MESSAGES/messages.mo
deleted file mode 100644
index 7b4bd7178..000000000
Binary files a/ihatemoney/translations/bn/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/bn_BD/LC_MESSAGES/messages.mo b/ihatemoney/translations/bn_BD/LC_MESSAGES/messages.mo
deleted file mode 100644
index 6cc61e8f1..000000000
Binary files a/ihatemoney/translations/bn_BD/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/ca/LC_MESSAGES/messages.mo b/ihatemoney/translations/ca/LC_MESSAGES/messages.mo
deleted file mode 100644
index a6b269a54..000000000
Binary files a/ihatemoney/translations/ca/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/cs/LC_MESSAGES/messages.mo b/ihatemoney/translations/cs/LC_MESSAGES/messages.mo
deleted file mode 100644
index 1b4b1b83f..000000000
Binary files a/ihatemoney/translations/cs/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/de/LC_MESSAGES/messages.mo b/ihatemoney/translations/de/LC_MESSAGES/messages.mo
deleted file mode 100644
index 9b03f6ca8..000000000
Binary files a/ihatemoney/translations/de/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/el/LC_MESSAGES/messages.mo b/ihatemoney/translations/el/LC_MESSAGES/messages.mo
deleted file mode 100644
index e7746e1fc..000000000
Binary files a/ihatemoney/translations/el/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/eo/LC_MESSAGES/messages.mo b/ihatemoney/translations/eo/LC_MESSAGES/messages.mo
deleted file mode 100644
index d46cd40a3..000000000
Binary files a/ihatemoney/translations/eo/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/es/LC_MESSAGES/messages.mo b/ihatemoney/translations/es/LC_MESSAGES/messages.mo
deleted file mode 100644
index a8f107778..000000000
Binary files a/ihatemoney/translations/es/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/es_419/LC_MESSAGES/messages.mo b/ihatemoney/translations/es_419/LC_MESSAGES/messages.mo
deleted file mode 100644
index 81fec7957..000000000
Binary files a/ihatemoney/translations/es_419/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/fa/LC_MESSAGES/messages.mo b/ihatemoney/translations/fa/LC_MESSAGES/messages.mo
deleted file mode 100644
index 43a3a59c2..000000000
Binary files a/ihatemoney/translations/fa/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/fr/LC_MESSAGES/messages.mo b/ihatemoney/translations/fr/LC_MESSAGES/messages.mo
deleted file mode 100644
index 0ead04b00..000000000
Binary files a/ihatemoney/translations/fr/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/he/LC_MESSAGES/messages.mo b/ihatemoney/translations/he/LC_MESSAGES/messages.mo
deleted file mode 100644
index a9ddfde3e..000000000
Binary files a/ihatemoney/translations/he/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/hi/LC_MESSAGES/messages.mo b/ihatemoney/translations/hi/LC_MESSAGES/messages.mo
deleted file mode 100644
index 74ae7cf0c..000000000
Binary files a/ihatemoney/translations/hi/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/hu/LC_MESSAGES/messages.mo b/ihatemoney/translations/hu/LC_MESSAGES/messages.mo
deleted file mode 100644
index fe90ccbc1..000000000
Binary files a/ihatemoney/translations/hu/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/id/LC_MESSAGES/messages.mo b/ihatemoney/translations/id/LC_MESSAGES/messages.mo
deleted file mode 100644
index 3901d26fb..000000000
Binary files a/ihatemoney/translations/id/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/it/LC_MESSAGES/messages.mo b/ihatemoney/translations/it/LC_MESSAGES/messages.mo
deleted file mode 100644
index df64c7437..000000000
Binary files a/ihatemoney/translations/it/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/ja/LC_MESSAGES/messages.mo b/ihatemoney/translations/ja/LC_MESSAGES/messages.mo
deleted file mode 100644
index e0a0e5212..000000000
Binary files a/ihatemoney/translations/ja/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/kn/LC_MESSAGES/messages.mo b/ihatemoney/translations/kn/LC_MESSAGES/messages.mo
deleted file mode 100644
index 4a9277e15..000000000
Binary files a/ihatemoney/translations/kn/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/ms/LC_MESSAGES/messages.mo b/ihatemoney/translations/ms/LC_MESSAGES/messages.mo
deleted file mode 100644
index 9c58d4dee..000000000
Binary files a/ihatemoney/translations/ms/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/nb_NO/LC_MESSAGES/messages.mo b/ihatemoney/translations/nb_NO/LC_MESSAGES/messages.mo
deleted file mode 100644
index 52ea457f8..000000000
Binary files a/ihatemoney/translations/nb_NO/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/nl/LC_MESSAGES/messages.mo b/ihatemoney/translations/nl/LC_MESSAGES/messages.mo
deleted file mode 100644
index 5a0e9c1c8..000000000
Binary files a/ihatemoney/translations/nl/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/pl/LC_MESSAGES/messages.mo b/ihatemoney/translations/pl/LC_MESSAGES/messages.mo
deleted file mode 100644
index 9787b92f2..000000000
Binary files a/ihatemoney/translations/pl/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/pt/LC_MESSAGES/messages.mo b/ihatemoney/translations/pt/LC_MESSAGES/messages.mo
deleted file mode 100644
index 0e7ed0e8f..000000000
Binary files a/ihatemoney/translations/pt/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/pt_BR/LC_MESSAGES/messages.mo b/ihatemoney/translations/pt_BR/LC_MESSAGES/messages.mo
deleted file mode 100644
index 3d649d6b9..000000000
Binary files a/ihatemoney/translations/pt_BR/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/ru/LC_MESSAGES/messages.mo b/ihatemoney/translations/ru/LC_MESSAGES/messages.mo
deleted file mode 100644
index 2987fec02..000000000
Binary files a/ihatemoney/translations/ru/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/sr/LC_MESSAGES/messages.mo b/ihatemoney/translations/sr/LC_MESSAGES/messages.mo
deleted file mode 100644
index 1100d08cf..000000000
Binary files a/ihatemoney/translations/sr/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/sv/LC_MESSAGES/messages.mo b/ihatemoney/translations/sv/LC_MESSAGES/messages.mo
deleted file mode 100644
index 75799e817..000000000
Binary files a/ihatemoney/translations/sv/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/ta/LC_MESSAGES/messages.mo b/ihatemoney/translations/ta/LC_MESSAGES/messages.mo
deleted file mode 100644
index 25e6adb95..000000000
Binary files a/ihatemoney/translations/ta/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/te/LC_MESSAGES/messages.mo b/ihatemoney/translations/te/LC_MESSAGES/messages.mo
deleted file mode 100644
index 9949f84af..000000000
Binary files a/ihatemoney/translations/te/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/th/LC_MESSAGES/messages.mo b/ihatemoney/translations/th/LC_MESSAGES/messages.mo
deleted file mode 100644
index 413741026..000000000
Binary files a/ihatemoney/translations/th/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/tr/LC_MESSAGES/messages.mo b/ihatemoney/translations/tr/LC_MESSAGES/messages.mo
deleted file mode 100644
index 7c027fe47..000000000
Binary files a/ihatemoney/translations/tr/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/uk/LC_MESSAGES/messages.mo b/ihatemoney/translations/uk/LC_MESSAGES/messages.mo
deleted file mode 100644
index dfb9b4751..000000000
Binary files a/ihatemoney/translations/uk/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/ur/LC_MESSAGES/messages.mo b/ihatemoney/translations/ur/LC_MESSAGES/messages.mo
deleted file mode 100644
index e9bb034d5..000000000
Binary files a/ihatemoney/translations/ur/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/vi/LC_MESSAGES/messages.mo b/ihatemoney/translations/vi/LC_MESSAGES/messages.mo
deleted file mode 100644
index c26a756a7..000000000
Binary files a/ihatemoney/translations/vi/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/ihatemoney/translations/zh_Hans/LC_MESSAGES/messages.mo b/ihatemoney/translations/zh_Hans/LC_MESSAGES/messages.mo
deleted file mode 100644
index 0dcc89dd9..000000000
Binary files a/ihatemoney/translations/zh_Hans/LC_MESSAGES/messages.mo and /dev/null differ
diff --git a/pyproject.toml b/pyproject.toml
index d14e8288e..bf2de70b1 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
 [build-system]
-requires = ["setuptools", "setuptools-scm"]
-build-backend = "setuptools.build_meta"
+requires = ["hatchling"]
+build-backend = "hatchling.build"
 
 [project]
 name = "ihatemoney"
@@ -85,5 +85,19 @@ doc = [
 [project.scripts]
 ihatemoney = "ihatemoney.manage:cli"
 
-[tool.setuptools]
-packages = ["ihatemoney"]
+[tool.hatch.build.hooks.custom]
+dependencies = [
+    # Babel is needed to compile translations catalogs at package build time
+    "Babel>=2.13.1"
+]
+
+[tool.hatch.build]
+artifacts = ["ihatemoney/translations/**/*.mo"]
+include = [
+    "ihatemoney/",
+    "LICENSE",
+    "CONTRIBUTORS",
+    "CHANGELOG.md",
+    "README.md",
+    "SECURITY.md",
+]
diff --git a/tox.ini b/tox.ini
index 695d550c8..fe2c9ad99 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,4 +1,5 @@
 [tox]
+isolated_build = true
 envlist = py312,py311,py310,py39,py38,py37,lint_docs
 skip_missing_interpreters = True
 


Test patch:
diff --git a/ihatemoney/tests/conftest.py b/ihatemoney/tests/conftest.py
index 08d6e8912..35f99e27f 100644
--- a/ihatemoney/tests/conftest.py
+++ b/ihatemoney/tests/conftest.py
@@ -3,10 +3,16 @@
 from flask import Flask
 import pytest
 
+from ihatemoney.babel_utils import compile_catalogs
 from ihatemoney.currency_convertor import CurrencyConverter
 from ihatemoney.run import create_app, db
 
 
+@pytest.fixture(autouse=True, scope="session")
+def babel_catalogs():
+    compile_catalogs()
+
+
 @pytest.fixture
 def app(request: pytest.FixtureRequest):
     """Create the Flask app with database"""