| 1 |
Metadata-Version: 2.2 |
| 2 |
Name: moz.l10n |
| 3 |
Version: 0.5.7 |
| 4 |
Summary: Mozilla tools for localization |
| 5 |
Author-email: Mozilla <[email protected]>, Eemeli Aro <[email protected]> |
| 6 |
License: Apache-2.0 |
| 7 |
Project-URL: repository, https://github.com/mozilla/moz-l10n |
| 8 |
Platform: any |
| 9 |
Classifier: Development Status :: 4 - Beta |
| 10 |
Classifier: Intended Audience :: Developers |
| 11 |
Classifier: License :: OSI Approved :: Apache Software License |
| 12 |
Classifier: Operating System :: OS Independent |
| 13 |
Classifier: Programming Language :: Python |
| 14 |
Classifier: Programming Language :: Python :: 3 |
| 15 |
Classifier: Programming Language :: Python :: 3.8 |
| 16 |
Classifier: Programming Language :: Python :: 3.9 |
| 17 |
Classifier: Programming Language :: Python :: 3.10 |
| 18 |
Classifier: Programming Language :: Python :: 3.11 |
| 19 |
Classifier: Programming Language :: Python :: 3.12 |
| 20 |
Classifier: Topic :: Software Development :: Libraries :: Python Modules |
| 21 |
Classifier: Topic :: Software Development :: Localization |
| 22 |
Classifier: Topic :: Software Development :: Testing |
| 23 |
Requires-Python: ~=3.8 |
| 24 |
Description-Content-Type: text/markdown |
| 25 |
Requires-Dist: fluent.syntax~=0.19.0 |
| 26 |
Requires-Dist: gitignorant~=0.3.1 |
| 27 |
Requires-Dist: iniparse~=0.5 |
| 28 |
Requires-Dist: polib~=1.2 |
| 29 |
Requires-Dist: tomli>=1.1.0; python_version < "3.11" |
| 30 |
Provides-Extra: xml |
| 31 |
Requires-Dist: lxml~=5.0; extra == "xml" |
| 32 |
|
| 33 |
# moz.l10n |
| 34 |
|
| 35 |
This is a library of Python tools and utilities for working with localization files, |
| 36 |
primarily built for internal use at Mozilla. |
| 37 |
|
| 38 |
The core idea here is to establish [Message](./moz/l10n/message.py) and [Resource](./moz/l10n/resource.py) |
| 39 |
as format-independent representations of localizable and localized messages and resources, |
| 40 |
so that operations like linting and transforms can be applied to them. |
| 41 |
|
| 42 |
The Message and Resource representations are drawn from work done for the |
| 43 |
Unicode [MessageFormat 2 specification](https://github.com/unicode-org/message-format-wg/tree/main/spec) |
| 44 |
and the [Message resource specification](https://github.com/eemeli/message-resource-wg/). |
| 45 |
|
| 46 |
Support for XML formats (`android`, `xliff`) is an optional extra; |
| 47 |
to support them, install as `moz.l10n[xml]`. |
| 48 |
|
| 49 |
## Command-line Tools |
| 50 |
|
| 51 |
For usage details, use each command's `--help` argument. |
| 52 |
|
| 53 |
### `l10n-build` |
| 54 |
|
| 55 |
Build localization files for release. |
| 56 |
|
| 57 |
Iterates source files as defined by `--config`, reads localization sources from `--base`, and writes to `--target`. |
| 58 |
Trims out all comments and messages not in the source files for each of the `--locales`. |
| 59 |
Adds empty files for any missing from the target locale. |
| 60 |
|
| 61 |
### `l10n-build-file` |
| 62 |
|
| 63 |
Build one localization file for release. |
| 64 |
|
| 65 |
Uses the `--source` file as a baseline, applying `--l10n` localizations to build `--target`. |
| 66 |
Trims out all comments and messages not in the source file. |
| 67 |
|
| 68 |
### `l10n-compare` |
| 69 |
|
| 70 |
Compare localizations to their `source`, which may be |
| 71 |
|
| 72 |
- a directory (using `L10nDiscoverPaths`), |
| 73 |
- a TOML config file (using `L10nConfigPaths`), or |
| 74 |
- a JSON file containing a mapping of file paths to arrays of messages. |
| 75 |
|
| 76 |
### `l10n-fix` |
| 77 |
|
| 78 |
Fix the formatting for localization resources. |
| 79 |
|
| 80 |
If `paths` is a single directory, it is iterated with `L10nConfigPaths` if `--config` is set, or `L10nDiscoverPaths` otherwise. |
| 81 |
If `paths` is not a single directory, its values are treated as glob expressions, with `**` support. |
| 82 |
|
| 83 |
## moz.l10n.paths |
| 84 |
|
| 85 |
### L10nConfigPaths |
| 86 |
|
| 87 |
Wrapper for localization config files. |
| 88 |
|
| 89 |
Supports a subset of the format specified at: |
| 90 |
https://moz-l10n-config.readthedocs.io/en/latest/fileformat.html |
| 91 |
|
| 92 |
Differences: |
| 93 |
|
| 94 |
- `[build]` is ignored |
| 95 |
- `[[excludes]]` are not supported |
| 96 |
- `[[filters]]` are ignored |
| 97 |
- `[[paths]]` must always include both `reference` and `l10n` |
| 98 |
|
| 99 |
Does not consider `.l10n-ignore` files. |
| 100 |
|
| 101 |
### L10nDiscoverPaths |
| 102 |
|
| 103 |
Automagical localization resource discovery. |
| 104 |
|
| 105 |
Given a root directory, finds the likeliest reference and target directories. |
| 106 |
|
| 107 |
The reference directory has a name like `templates`, `en-US`, or `en`, |
| 108 |
and contains files with extensions that appear localizable. |
| 109 |
|
| 110 |
The localization target root is a directory with subdirectories named as |
| 111 |
BCP 47 locale identifiers, i.e. like `aa`, `aa-AA`, `aa-Aaaa`, or `aa-Aaaa-AA`. |
| 112 |
|
| 113 |
An underscore may also be used as a separator, as in `en_US`. |
| 114 |
|
| 115 |
## moz.l10n.resources |
| 116 |
|
| 117 |
Parsers and serializers are provided for a number of formats, |
| 118 |
using common and well-established libraries to take care of the details. |
| 119 |
A unified API for these is provided, |
| 120 |
such that `FORMAT_parse(text)` will always accept `str` input, |
| 121 |
and `FORMAT_serialize(resource)` will always provide a `str` iterator. |
| 122 |
All the serializers accept a `trim_comments` argument |
| 123 |
which leaves out comments from the serialized result, |
| 124 |
but additional input types and options vary by format. |
| 125 |
|
| 126 |
The library currently supports the following resource formats: |
| 127 |
|
| 128 |
- `android`: Android string resources (strings.xml) |
| 129 |
- `dtd`: .dtd |
| 130 |
- `fluent`: Fluent (.ftl) |
| 131 |
- `inc`: .inc |
| 132 |
- `ini`: .ini |
| 133 |
- `plain_json`: Plain JSON (.json) |
| 134 |
- `po`: Gettext (.po, .pot) |
| 135 |
- `properties`: .properties |
| 136 |
- `webext`: WebExtensions (messages.json) |
| 137 |
- `xliff`: XLIFF 1.2, including XCode customizations (.xlf, .xliff) |
| 138 |
|
| 139 |
### add_entries |
| 140 |
|
| 141 |
```python |
| 142 |
def add_entries( |
| 143 |
target: Resource, |
| 144 |
source: Resource, |
| 145 |
*, |
| 146 |
use_source_entries: bool = False |
| 147 |
) -> int |
| 148 |
``` |
| 149 |
|
| 150 |
Modifies `target` by adding entries from `source` that are not already present in `target`. |
| 151 |
Standalone comments are not added. |
| 152 |
|
| 153 |
If `use_source_entries` is set, |
| 154 |
entries from `source` override those in `target` when they differ, |
| 155 |
as well as updating section comments and metadata from `source`. |
| 156 |
|
| 157 |
Entries are not copied, so further changes will be reflected in both resources. |
| 158 |
|
| 159 |
Returns a count of added or changed entries and sections. |
| 160 |
|
| 161 |
### detect_format |
| 162 |
|
| 163 |
```python |
| 164 |
def detect_format(name: str | None, source: bytes | str) -> Format | None |
| 165 |
``` |
| 166 |
|
| 167 |
Detect the format of the input based on its file extension |
| 168 |
and/or contents. |
| 169 |
|
| 170 |
Returns a `Format` enum value, or `None` if the input is not recognized. |
| 171 |
|
| 172 |
### iter_resources |
| 173 |
|
| 174 |
```python |
| 175 |
def iter_resources( |
| 176 |
root: str, |
| 177 |
dirs: list[str] | None = None, |
| 178 |
ignorepath: str = ".l10n-ignore" |
| 179 |
) -> Iterator[tuple[str, Resource[Message, str] | None]] |
| 180 |
``` |
| 181 |
|
| 182 |
Iterate through localizable resources under the `root` directory. |
| 183 |
Use `dirs` to limit the search to only some subdirectories under `root`. |
| 184 |
|
| 185 |
Yields `(str, Resource | None)` tuples, |
| 186 |
with the file path and the corresponding `Resource`, |
| 187 |
or `None` for files that could not be parsed as localization resources. |
| 188 |
|
| 189 |
To ignore files, include a `.l10n-ignore` file in `root`, |
| 190 |
or some other location passed in as `ignorepath`. |
| 191 |
This file uses a git-ignore syntax, |
| 192 |
and is always based in the `root` directory. |
| 193 |
|
| 194 |
### l10n_equal |
| 195 |
|
| 196 |
```python |
| 197 |
def l10n_equal(a: Resource, b: Resource) -> bool |
| 198 |
``` |
| 199 |
|
| 200 |
Compares the localization-relevant content |
| 201 |
(id, comment, metadata, message values) of two resources. |
| 202 |
|
| 203 |
Sections with no message entries are ignored, |
| 204 |
and the order of sections, entries, and metadata is ignored. |
| 205 |
|
| 206 |
### parse_resource |
| 207 |
|
| 208 |
```python |
| 209 |
def parse_resource( |
| 210 |
input: Format | str | None, |
| 211 |
source: str | bytes | None = None |
| 212 |
) -> Resource[Message, str] |
| 213 |
``` |
| 214 |
|
| 215 |
Parse a Resource from its string representation. |
| 216 |
|
| 217 |
The first argument may be an explicit Format, |
| 218 |
the file path as a string, or None. |
| 219 |
For the latter two types, |
| 220 |
an attempt is made to detect the appropriate format. |
| 221 |
|
| 222 |
If the first argument is a string path, |
| 223 |
the `source` argument is optional, |
| 224 |
as the file will be opened and read. |
| 225 |
|
| 226 |
### serialize_resource |
| 227 |
|
| 228 |
```python |
| 229 |
def serialize_resource( |
| 230 |
resource: Resource[str, str] | Resource[Message, str], |
| 231 |
format: Format | None = None, |
| 232 |
trim_comments: bool = False |
| 233 |
) -> Iterator[str] |
| 234 |
``` |
| 235 |
|
| 236 |
Serialize a Resource as its string representation. |
| 237 |
|
| 238 |
If `format` is set, it overrides the `resource.format` value. |
| 239 |
|
| 240 |
With `trim_comments`, |
| 241 |
all standalone and attached comments are left out of the serialization. |