Skip to content

Commit

Permalink
feat: use translate-toolkit to convert PO to MO
Browse files Browse the repository at this point in the history
This makes the conversion work without external dependencies.

Fixes breezy-team#103
  • Loading branch information
nijel committed Jan 21, 2025
1 parent cef0b7f commit b72cd41
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
14 changes: 12 additions & 2 deletions setuptools_gettext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"),
]

Expand All @@ -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 = []

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit b72cd41

Please sign in to comment.