From b72cd414320d68401392ed9a94c11f76f698d695 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Tue, 21 Jan 2025 08:26:26 +0100 Subject: [PATCH] feat: use translate-toolkit to convert PO to MO This makes the conversion work without external dependencies. Fixes #103 --- pyproject.toml | 6 +++++- setuptools_gettext/__init__.py | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 38b3216..4462b4f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,11 @@ classifiers = [ "Operating System :: Microsoft :: Windows", ] requires-python = ">=3.9" -dependencies = ["setuptools>=61.0", 'tomli>=1.2.1; python_version<"3.11"'] +dependencies = [ + "setuptools>=61.0", + 'tomli>=1.2.1; python_version<"3.11"', + "translate-toolkit>=3.14.0" +] dynamic = ["version"] [project.readme] diff --git a/setuptools_gettext/__init__.py b/setuptools_gettext/__init__.py index b5bebf9..d95dcc3 100644 --- a/setuptools_gettext/__init__.py +++ b/setuptools_gettext/__init__.py @@ -28,6 +28,7 @@ from setuptools import Command from setuptools.dist import Distribution +from translate.tools.pocompile import convertmo __version__ = (0, 1, 14) DEFAULT_SOURCE_DIR = "po" @@ -90,6 +91,7 @@ class build_mo(Command): ("build-dir=", "d", "Directory to build locale files"), ("output-base=", "o", "mo-files base name"), ("force", "f", "Force creation of mo files"), + ("msgfmt", "m", "Use msgfmt program"), ("lang=", None, "Comma-separated list of languages to process"), ] @@ -99,6 +101,7 @@ def initialize_options(self): self.build_dir = None self.output_base = None self.force = None + self.msgfmt = None self.lang = None self.outfiles = [] @@ -132,7 +135,7 @@ def run(self): if not self.lang: return - if find_executable("msgfmt") is None: + if self.msgfmt and find_executable("msgfmt") is None: logging.warning("GNU gettext msgfmt utility not found!") logging.warning("Skip compiling po files.") return @@ -173,9 +176,16 @@ def run(self): mo = os.path.join(dir_, basename) if self.force or newer(po, mo): logging.info(f"Compile: {po} -> {mo}") - self.spawn(["msgfmt", "-o", mo, po]) + self.compile_mo(po, mo) self.outfiles.append(mo) + def compile_mo(self, po: str, mo: str): + if self.msgfmt: + self.spawn(["msgfmt", "-o", mo, po]) + else: + with open(po, "rb") as pofile, open(mo, "wb") as mofile: + convertmo(pofile, mofile, None) + def get_outputs(self): return self.outfiles