Skip to main content

No project description provided

Project description

rustfluent

A Python interface to the Rust Fluent Library.

This project is a small shim around fluent-rs, so it can be used from Python.

[!WARNING] This package is under active development, and breaking changes may be released at any time. Be sure to pin to specific versions if you're using this package in a production environment.

Prerequisites

This package supports:

  • Python 3.11
  • Python 3.12

Installation

pip install rustfluent

Usage

import rustfluent

# First load a bundle
bundle = rustfluent.Bundle(
    "en",
    [
        # Multiple FTL files can be specified. Entries in later
        # files overwrite earlier ones.
        "en.ftl",
    ],
)

# Fetch a translation
assert bundle.get_translation("hello-world") == "Hello World"

# Fetch a translation that includes variables
assert bundle.get_translation("hello-user", variables={"user": "Bob"}) == "Hello, \u2068Bob\u2069"

The Unicode characters around "Bob" in the above example are for Unicode bidirectional handling.

API reference

Bundle class

A set of translations for a specific language.

import rustfluent

bundle = rustfluent.Bundle(
    language="en-US",
    ftl_files=[
        "/path/to/messages.ftl",
        "/path/to/more/messages.ftl",
    ],
)

Parameters

Name Type Description
language str Unicode Language Identifier for the language.
ftl_files list[str] Full paths to the FTL files containing the translations. Entries in later files overwrite earlier ones.
strict bool, optional In strict mode, a ParserError will be raised if there are any errors in the file. In non-strict mode, invalid Fluent messages will be excluded from the Bundle.

Raises

  • FileNotFoundError if any of the FTL files could not be found.
  • rustfluent.ParserError if any of the FTL files contain errors (strict mode only).

Bundle.get_translation

>>> bundle.get_translation(identifier="hello-world")
"Hello, world!"
>>> bundle.get_translation(identifier="hello-user", variables={"user": "Bob"})
"Hello, Bob!"

Parameters

Name Type Description
identifier str The identifier for the Fluent message.
variables dict[str, str], optional Any variables to be passed to the Fluent message.

Return value

str: the translated message.

Raises

  • ValueError if the message could not be found or has no translation available.

Contributing

See Contributing.

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

rustfluent-0.1.0a2.tar.gz (15.7 kB view hashes)

Uploaded Source

Built Distributions

rustfluent-0.1.0a2-cp312-none-win_amd64.whl (223.6 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

rustfluent-0.1.0a2-cp312-none-win32.whl (212.8 kB view hashes)

Uploaded CPython 3.12 Windows x86

rustfluent-0.1.0a2-cp312-cp312-musllinux_1_2_x86_64.whl (537.3 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ x86-64

rustfluent-0.1.0a2-cp312-cp312-musllinux_1_2_i686.whl (559.4 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ i686

rustfluent-0.1.0a2-cp312-cp312-musllinux_1_2_armv7l.whl (634.2 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ ARMv7l

rustfluent-0.1.0a2-cp312-cp312-musllinux_1_2_aarch64.whl (553.4 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ ARM64

rustfluent-0.1.0a2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (374.2 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

rustfluent-0.1.0a2-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl (438.5 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ s390x

rustfluent-0.1.0a2-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (419.5 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ppc64le

rustfluent-0.1.0a2-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (385.0 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARMv7l

rustfluent-0.1.0a2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (381.9 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

rustfluent-0.1.0a2-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl (390.3 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.5+ i686

rustfluent-0.1.0a2-cp312-cp312-macosx_11_0_arm64.whl (328.1 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

rustfluent-0.1.0a2-cp312-cp312-macosx_10_12_x86_64.whl (330.1 kB view hashes)

Uploaded CPython 3.12 macOS 10.12+ x86-64

rustfluent-0.1.0a2-cp311-none-win_amd64.whl (224.0 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

rustfluent-0.1.0a2-cp311-none-win32.whl (213.5 kB view hashes)

Uploaded CPython 3.11 Windows x86

rustfluent-0.1.0a2-cp311-cp311-musllinux_1_2_x86_64.whl (538.7 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ x86-64

rustfluent-0.1.0a2-cp311-cp311-musllinux_1_2_i686.whl (559.7 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ i686

rustfluent-0.1.0a2-cp311-cp311-musllinux_1_2_armv7l.whl (634.3 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ ARMv7l

rustfluent-0.1.0a2-cp311-cp311-musllinux_1_2_aarch64.whl (554.7 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ ARM64

rustfluent-0.1.0a2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (374.4 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

rustfluent-0.1.0a2-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl (439.9 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ s390x

rustfluent-0.1.0a2-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (421.4 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ppc64le

rustfluent-0.1.0a2-cp311-cp311-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (385.6 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARMv7l

rustfluent-0.1.0a2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (381.5 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

rustfluent-0.1.0a2-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl (391.4 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.5+ i686

rustfluent-0.1.0a2-cp311-cp311-macosx_11_0_arm64.whl (328.5 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

rustfluent-0.1.0a2-cp311-cp311-macosx_10_12_x86_64.whl (331.0 kB view hashes)

Uploaded CPython 3.11 macOS 10.12+ x86-64

Supported by

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