Metadata-Version: 2.5
Name: hatch-gettext
Version: 1.1.5
Summary: Hatch build hook plugin for GNU gettext
Project-URL: Documentation, https://github.com/damonlynch/hatch-gettext#readme
Project-URL: Issues, https://github.com/damonlynch/hatch-gettext/issues
Project-URL: Homepage, https://github.com/damonlynch/hatch-gettext
Author-email: Damon Lynch <damonlynch@gmail.com>
License-Expression: GPL-3.0-or-later
License-File: LICENSE.txt
Keywords: build,gettext,hatch,i18n,plugin,typing
Classifier: Development Status :: 5 - Production/Stable
Classifier: Framework :: Hatch
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: hatchling
Requires-Dist: rich
Description-Content-Type: text/markdown

# Hatch Gettext

| |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| --- |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Package | [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/hatch-gettext.svg)](https://pypi.org/project/hatch-gettext) [![PyPI - Version](https://img.shields.io/pypi/v/hatch-gettext.svg)](https://pypi.org/project/hatch-gettext)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| Meta | [![Hatch project](https://img.shields.io/badge/%F0%9F%A5%9A-Hatch-4051b5.svg)](https://github.com/pypa/hatch) [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff) [![GitButler](https://img.shields.io/badge/GitButler-%23B9F4F2?logo=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPHN2ZyB3aWR0aD0iMzkiIGhlaWdodD0iMjgiIHZpZXdCb3g9IjAgMCAzOSAyOCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTI1LjIxNDUgMTIuMTk5N0wyLjg3MTA3IDEuMzg5MTJDMS41NDI5NSAwLjc0NjUzMiAwIDEuNzE0MDYgMCAzLjE4OTQ3VjI0LjgxMDVDMCAyNi4yODU5IDEuNTQyOTUgMjcuMjUzNSAyLjg3MTA3IDI2LjYxMDlMMjUuMjE0NSAxNS44MDAzQzI2LjcxOTcgMTUuMDcyMSAyNi43MTk3IDEyLjkyNzkgMjUuMjE0NSAxMi4xOTk3WiIgZmlsbD0iYmxhY2siLz4KPHBhdGggZD0iTTEzLjc4NTUgMTIuMTk5N0wzNi4xMjg5IDEuMzg5MTJDMzcuNDU3MSAwLjc0NjUzMiAzOSAxLjcxNDA2IDM5IDMuMTg5NDdWMjQuODEwNUMzOSAyNi4yODU5IDM3LjQ1NzEgMjcuMjUzNSAzNi4xMjg5IDI2LjYxMDlMMTMuNzg1NSAxNS44MDAzQzEyLjI4MDMgMTUuMDcyMSAxMi4yODAzIDEyLjkyNzkgMTMuNzg1NSAxMi4xOTk3WiIgZmlsbD0idXJsKCNwYWludDBfcmFkaWFsXzMxMF8xMjkpIi8%2BCjxkZWZzPgo8cmFkaWFsR3JhZGllbnQgaWQ9InBhaW50MF9yYWRpYWxfMzEwXzEyOSIgY3g9IjAiIGN5PSIwIiByPSIxIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgZ3JhZGllbnRUcmFuc2Zvcm09InRyYW5zbGF0ZSgxNi41NzAxIDE0KSBzY2FsZSgxOS44NjQxIDE5LjgzODMpIj4KPHN0b3Agb2Zmc2V0PSIwLjMwMTA1NiIgc3RvcC1vcGFjaXR5PSIwIi8%2BCjxzdG9wIG9mZnNldD0iMSIvPgo8L3JhZGlhbEdyYWRpZW50Pgo8L2RlZnM%2BCjwvc3ZnPgo%3D)](https://gitbutler.com/) [![pyrefly](https://img.shields.io/endpoint?url=https://pyrefly.org/badge.json)](https://github.com/pyrefly-org/pyrefly) [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) [![GitHub Sponsors](https://img.shields.io/github/sponsors/damonlynch?logo=GitHub%20Sponsors&style=social)](https://github.com/sponsors/damonlynch)  |

______________________________________________________________________

This provides a [build hook](https://hatch.pypa.io/latest/config/build/#build-hooks) plugin for [Hatch](https://github.com/pypa/hatch) that compiles multi-lingual messages with GNU gettext's tools `msgfmt`. It can also (optionally) use `intltool` to:

- translate .xml and .desktop files
- search for left out files
- regenerate the .pot template
- display a status report for all translations

**Table of Contents**

<!-- TOC -->
* [Hatch Gettext](#hatch-gettext)
  * [Configuration](#configuration)
    * [Calling the plugin](#calling-the-plugin)
    * [Skipping Platforms](#skipping-platforms)
    * [Compiling messages with msgfmt](#compiling-messages-with-msgfmt)
    * [Identifying left out files using intltool-update](#identifying-left-out-files-using-intltool-update)
    * [Regenerating the .pot template using intltool-update](#regenerating-the-pot-template-using-intltool-update)
    * [Displaying a status report using intltool-update](#displaying-a-status-report-using-intltool-update)
    * [Why those three options apply only to source distribution builds](#why-those-three-options-apply-only-to-source-distribution-builds)
    * [Translating files using intltool-merge](#translating-files-using-intltool-merge)
    * [Progress in a verbose build](#progress-in-a-verbose-build)
  * [Cleaning output files](#cleaning-output-files)
  * [Running tests](#running-tests)
  * [Related Hatch plugin](#related-hatch-plugin)
  * [License](#license)
<!-- TOC -->

## Configuration

The [build hook plugin](https://hatch.pypa.io/latest/plugins/build-hook/) name is `gettext`.

### Calling the plugin

Modify `pyproject.toml` to include the plugin as a build dependency:

```toml
[build-system]
requires = ["hatchling", "hatch-gettext"]
build-backend = "hatchling.build"
```

### Skipping Platforms

This plugin may make less sense on some platforms, e.g. Windows. To skip running this plugin on specific platforms use values from `sys.platform` in the configuration option `skip-platforms`, e.g.:

```toml
[tool.hatch.build.hooks.gettext]
skip-platforms = ["win32", "cygwin"]
```

### Compiling messages with msgfmt

This plugin requires `.mo` files be created; it also requires that the directory storing them is within the project's base directory, and is not equal to the project's base directory or the directory in which `po` files are sourced. The mo file directory is set using `locale-directory`, which is required.

For example, for a project named `myproject`, and a src layout `src/myproject`, an acceptable directory in which to store the `LC_MESSAGES/myproject.mo` files would be `src/myproject/locale`

```toml
[tool.hatch.build.hooks.gettext]
locale-directory = "src/myproject/locale"
```

Optionally, specify values for the gexttext output name and the source directory where po files are found:

```toml
[tool.hatch.build.hooks.gettext]
locale-directory = "src/myproject/locale"
i18n-name = "myproject" 
po-directory = "po-files"
```

If `i18n-name` is not specified, the `name` in `[project]` in the `pyproject.toml` is used. If `po-directory` is not specified, the directory `po` is used.

### Identifying left out files using intltool-update

With every source distribution (sdist) build, to search for left out files — those that should have been listed in `POTFILES.in` or `POTFILES.skip` — set `identify-left-out` to true (the default value is false):

```toml
[tool.hatch.build.hooks.gettext]
locale-directory = "src/myproject/locale"
identify-left-out = true
```

The utility writes two lists into the po directory: `missing` holds the files that contain translatable strings but are absent from `POTFILES.in`, and `notexist` holds the entries in `POTFILES.in` whose file has gone. When either one is written the build stops, and the error names every file, says whether to add it to `POTFILES.in` or remove it, and gives the absolute path of the report it came from, because `intltool-update` itself only says the report was written "in the current directory". Both reports are removed before the check runs, so a build that passes leaves nothing behind from an earlier one.

Note that `intltool-update -m` exits with status 0 whether or not it found anything, and writes its findings to standard error. That is why the two report files, and not the exit status or the output, determine the outcome of running `intltool-update`.

When either report is written the build stops there, so `regenerate-template` (see next section) does not run in that build. That is deliberate: `xgettext` assembles the template from `POTFILES.in`, so a template regenerated from a list that disagrees with the tree is incomplete, and an incomplete template looks current to whoever translates against it. Fix `POTFILES.in` and rebuild.

### Regenerating the .pot template using intltool-update

To regenerate the .pot template with every sdist build, set `regenerate-template` to true (the default value is false):

```toml
[tool.hatch.build.hooks.gettext]
locale-directory = "src/myproject/locale"
regenerate-template = true
```

### Displaying a status report using intltool-update

To display a status report for all translations, set `show-report` to true (the default value is false):

```toml
[tool.hatch.build.hooks.gettext]
locale-directory = "src/myproject/locale"
show-report = true
```

### Why those three options apply only to source distribution builds

`identify-left-out`, `regenerate-template` and `show-report` change or report on the files
in the po directory instead of contributing part of an artifact, which is why they are acted
on only while a source distribution is built. A wheel is built by continuous integration, by
distribution packagers and by anyone installing the project from source, and none of them
should find a translation template rewritten in their tree, or their build stopped over a
`POTFILES.in` they had no hand in.

Hatch instantiates a build hook only for the targets whose tables name it, so listing this
plugin under `[tool.hatch.build.targets.wheel.hooks]` leaves those three options with nothing
to act on. That is now reported as an error rather than ignored. Name the plugin in
`[tool.hatch.build.hooks]` for every target to see it, as the examples above do, or in
`[tool.hatch.build.targets.sdist.hooks]` for the source distribution alone. Note that a
per-target table replaces the global one rather than adding to it, so the options have to be
listed in whichever table the sdist build uses.

A failed `intltool-update` run deletes the template it was regenerating. This plugin keeps the
previous contents and restores them when the utility fails, so the po directory is left exactly
as it was found.

### Translating files using intltool-merge

This plugin allows for but does not mandate translating `.xml` and `.desktop` files using `intltool-merge`. Using `[tool.hatch.build.hooks.gettext.files]`, specify the destination directories for the translated files using keys, and arrays of source files as values. For example:

```toml
[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"]
```

### Progress in a verbose build

A build run with `hatch -vv` asks each gettext utility for what it knows and passes it on unchanged, so the output shows what the utility did rather than only the plugin's line for it.

For `intltool-update` that means lines such as `Building myproject.pot...`, the complete `Running xgettext ...` command, and `Wrote myproject.pot`. The status report needs nothing passed on, because its output goes straight to the build and is never captured.

`msgfmt` is asked for `--statistics`, which reports per catalogue how many messages are translated, fuzzy and untranslated, as `164 translated messages, 649 untranslated messages.` That is the only place you see it without running the utility yourself, and it is how a language that has fallen behind shows up.

`intltool-merge` has no option to ask for more, only `-q` to say less, so what it already writes — `Merging translations into net.myproject.desktop.` — is passed on. Note that the utilities do not agree on the stream they use: `msgfmt` reports on standard error while the other two use standard output, so both are collected.

All of it appears at `-vv`, not at `-v`, because that is the level at which the plugin reports its own progress. A build without `-vv` stays as quiet as it was.

## Cleaning output files

The plugin includes logic to remove the files it outputs using hatch's `clean` hook. As well as individual files, any output directories created will also be removed, as long as these directories do not contain files created by something other than this plugin.

## Running tests

To run the full test suite for all supported Python versions, run:

```
hatch test -a
```

## Related Hatch plugin

To automatically generate a manual page from an `ArgumentParser` object, see [hatch-argparse-manpage](https://github.com/damonlynch/hatch-argparse-manpage).

## License

`hatch-gettext` is distributed under the terms of the [GPL-3.0-or-later](https://spdx.org/licenses/GPL-3.0-or-later.html) license.
