Skip to main content

Mozilla tools for localization

Project description

moz.l10n

This is a library of Python tools and utilities for working with localization files, primarily built for internal use at Mozilla.

The core idea here is to establish Message and Resource as format-independent representations of localizable and localized messages and resources, so that operations like linting and transforms can be applied to them.

The Message and Resource representations are drawn from work done for the Unicode MessageFormat 2 specification and the Message resource specification.

Command-line Tools

For usage details, use each command's --help argument.

l10n-build

Build localization files for release.

Iterates source files as defined by --config, reads localization sources from --base, and writes to --target. Trims out all comments and messages not in the source files for each of the --locales. Adds empty files for any missing from the target locale.

l10n-fix

Fix the formatting for localization resources.

If paths is a single directory, it is iterated with L10nConfigPaths if --config is set, or L10nDiscoverPaths otherwise. If paths is not a single directory, its values are treated as glob expressions, with ** support.

moz.l10n.paths

L10nConfigPaths

Wrapper for localization config files.

Supports a subset of the format specified at: https://moz-l10n-config.readthedocs.io/en/latest/fileformat.html

Differences:

  • [build] is ignored
  • [[excludes]] are not supported
  • [[filters]] are ignored
  • [[paths]] must always include both reference and l10n

Does not consider .l10n-ignore files.

L10nDiscoverPaths

Automagical localization resource discovery.

Given a root directory, finds the likeliest reference and target directories.

The reference directory has a name like templates, en-US, or en, and contains files with extensions that appear localizable.

The localization target root is a directory with subdirectories named as BCP 47 locale identifiers, i.e. like aa, aa-AA, aa-Aaaa, or aa-Aaaa-AA.

An underscore may also be used as a separator, as in en_US.

moz.l10n.resources

Parsers and serializers are provided for a number of formats, using common and well-established libraries to take care of the details. A unified API for these is provided, such that FORMAT_parse(text) will always accept str input, and FORMAT_serialize(resource) will always provide a str iterator. All the serializers accept a trim_comments argument which leaves out comments from the serialized result, but additional input types and options vary by format.

The library currently supports the following resource formats:

  • android: Android string resources (strings.xml)
  • dtd: .dtd
  • fluent: Fluent (.ftl)
  • inc: .inc
  • ini: .ini
  • plain_json: Plain JSON (.json)
  • po: Gettext (.po, .pot)
  • properties: .properties
  • webext: WebExtensions (messages.json)
  • xliff: XLIFF 1.2, including XCode customizations (.xlf, .xliff)

add_entries

def add_entries(target: Resource, source: Resource) -> int

Modifies target by adding entries from source that are not already present in target. Standalone comments are not added.

Entries are not copied, so further changes will be reflected in both resources.

Returns a count of added entries.

detect_format

def detect_format(name: str | None, source: bytes | str) -> Format | None

Detect the format of the input based on its file extension and/or contents.

Returns a Format enum value, or None if the input is not recognized.

iter_resources

def iter_resources(
    root: str,
    dirs: list[str] | None = None,
    ignorepath: str = ".l10n-ignore"
) -> Iterator[tuple[str, Resource[Message, str] | None]]

Iterate through localizable resources under the root directory. Use dirs to limit the search to only some subdirectories under root.

Yields (str, Resource | None) tuples, with the file path and the corresponding Resource, or None for files that could not be parsed as localization resources.

To ignore files, include a .l10n-ignore file in root, or some other location passed in as ignorepath. This file uses a git-ignore syntax, and is always based in the root directory.

l10n_equal

def l10n_equal(a: Resource, b: Resource) -> bool

Compares the localization-relevant content (id, comment, metadata, message values) of two resources.

Sections with no message entries are ignored, and the order of sections, entries, and metadata is ignored.

parse_resource

def parse_resource(
    input: Format | str | None,
    source: str | bytes | None = None
) -> Resource[Message, str]

Parse a Resource from its string representation.

The first argument may be an explicit Format, the file path as a string, or None. For the latter two types, an attempt is made to detect the appropriate format.

If the first argument is a string path, the source argument is optional, as the file will be opened and read.

serialize_resource

def serialize_resource(
    resource: Resource[str, str] | Resource[Message, str],
    format: Format | None = None,
    trim_comments: bool = False
) -> Iterator[str]

Serialize a Resource as its string representation.

If format is set, it overrides the resource.format value.

With trim_comments, all standalone and attached comments are left out of the serialization.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

moz_l10n-0.2.2.tar.gz (61.7 kB view details)

Uploaded Source

Built Distribution

moz.l10n-0.2.2-py3-none-any.whl (85.8 kB view details)

Uploaded Python 3

File details

Details for the file moz_l10n-0.2.2.tar.gz.

File metadata

  • Download URL: moz_l10n-0.2.2.tar.gz
  • Upload date:
  • Size: 61.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.18

File hashes

Hashes for moz_l10n-0.2.2.tar.gz
Algorithm Hash digest
SHA256 945acfc61b9c44a12b2dec45e21492f1d532da7faef6a00815ad92cd8f63b6a1
MD5 05acb0b09dcbc7277e7868e7cf8ea506
BLAKE2b-256 ed42ae8e223ff1abedbeca7c5ab0ba26968bb9e00df6750a0b066c4514769fed

See more details on using hashes here.

File details

Details for the file moz.l10n-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: moz.l10n-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 85.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.18

File hashes

Hashes for moz.l10n-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7da71c6d5096ce060d969cc4a0a62088fba1f08c78c30beeea1d85265cf3ff23
MD5 99dd0972807cbcd7ca608816f34d6029
BLAKE2b-256 d396d3dd9edb3c2797ebabbd4ad8fc8b3f09afa371f87da18c4493a032b07cd4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page