hatch-gettext(3)
β¨ NAME β¨
hatch-gettext β it's the Hatch build hook plugin for GNU gettext, and it's literally here to make your translations compile themselves π
π SYNOPSIS π
Okay bestie, first things first, you gotta go touch your pyproject.toml. Like this:
[build-system]
requires = ["hatchling", "hatch-gettext"]
build-backend = "hatchling.build"
[tool.hatch.build.hooks.gettext]
locale-directory = "RELATIVE MO FILE DIRECTORY"
i18n-name = "GETTEXT OUTPUT NAME"
po-directory = "RELATIVE PO FILE DIRECTORY"
identify-left-out = BOOLEAN
regenerate-template = BOOLEAN
show-report = BOOLEAN
[tool.hatch.build.hooks.gettext.files]
"RELATIVE DIRECTORY" = ["RELATIVE FILE PATH", ...]
That's the whole vibe check right there, no cap.
π¦ DESCRIPTION π¦
So hatch-gettext gives you a build hook plugin for Hatch that compiles all your multi-lingual messages using GNU gettext's tool msgfmt. Slay. π
And optionally (like, only if you want it), it can also pull in intltool to:
- search for left out files π
- regenerate the
.pottemplate π± - display a status report for all translations π
- translate
.xmland.desktopfiles π§
π Directory layout
Heads up, this plugin has requirements, and it's giving very specific:
The directory storing the generated mo files must be within the project's base directory. And it must not be equal to the project's base directory, and it must not be the directory in which po files are sourced. Three rules, all mandatory, periodt. π
Example time: for a project named myproject, with a src layout of src/myproject, an acceptable directory to store the LC_MESSAGES/myproject.mo files would be src/myproject/locale. π«Ά
π Required key value
locale-directoryβ the destination directory where the mo files and all their associated subdirectories get generated into. You need this one, it's not optional, bestie.
π Optional key values
i18n-nameβ the gettext output name. The default isnamein[project]in thepyproject.toml.po-directoryβ the source directory where the po files live. The default ispo.identify-left-outβ a boolean saying whether to useintltool-updateto search for left out files that should have been listed inPOTFILES.inorPOTFILES.skip. The default isfalse.regenerate-templateβ a boolean saying whether to useintltool-updateto regenerate the.pottemplate with every single sdist build. The default isfalse.show-reportβ a boolean saying whether to useintltool-updateto display a status report for all translations. The default isfalse.
πΈ Translating files using intltool-merge
This plugin allows for translating .xml and .desktop files using intltool-merge, but it does not mandate it. Your call, your rules. β¨
So using [tool.hatch.build.hooks.gettext.files] in pyproject.toml, you specify the destination directories for the translated files using keys, and arrays of source files as values. Keys and values, iconic behaviour honestly.
π§Ή Cleaning output files
The plugin has logic built in to remove the files it outputs, using hatch's clean hook. Obsessed. π
And it's not just individual files β any output directories it created will also get removed, as long as those directories don't contain files created by something other than this plugin. So don't go putting your own stuff in there and expecting it to survive, iykyk.
π EXAMPLES π
Compiling messages with msgfmt
[tool.hatch.build.hooks.gettext]
locale-directory = "src/myproject/locale"
Identifying left out files using intltool-update
[tool.hatch.build.hooks.gettext]
locale-directory = "src/myproject/locale"
identify-left-out = true
Regenerating the .pot template using intltool-update
[tool.hatch.build.hooks.gettext]
locale-directory = "src/myproject/locale"
regenerate-template = true
Displaying a status report using intltool-update
[tool.hatch.build.hooks.gettext]
locale-directory = "src/myproject/locale"
show-report = true
Translating files using intltool-merge
[tool.hatch.build.hooks.gettext.files]
"share/applications" = ["data/net.myproject.desktop.in"]
"share/solid/actions" = ["data/kde/net.myproject.desktop.in"]
"share/metainfo" = ["data/net.myproject.metainfo.xml.in"]
Copy. Paste. Ship. π
π AUTHOR π
Damon Lynch β damonlynch@gmail.com β¨
π COPYRIGHT π
Copyright 2024 Damon Lynch. π
Okay that's the whole page bestie, we ate and left no crumbs π β this was page 3 of the manuals, the hatch-gettext one, sourced from Hatch build hook plugin for GNU gettext. Go compile something gorgeous, mwah ππ
π¦ where this page comes from
- ubuntu resolute: python3-hatch-gettext 1.1.1-0ubuntu1 Β· GPL-3.0-or-later
- ubuntu stonking: python3-hatch-gettext 1.1.1-0ubuntu1 Β· GPL-3.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms