Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rustfluent

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rustfluent

  • 0.1.0a6
  • PyPI
  • Socket score

Maintainers
1

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
NameTypeDescription
languagestrUnicode Language Identifier for the language.
ftl_fileslist[str]Full paths to the FTL files containing the translations. Entries in later files overwrite earlier ones.
strictbool, optionalIn 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, \u2068Bob\u2069!"
>>> bundle.get_translation(identifier="hello-user", variables={"user": "Bob"}, use_isolating=False)
"Hello, Bob!"
Parameters
NameTypeDescription
identifierstrThe identifier for the Fluent message.
variablesdict[str, str | int | datetime.date], optionalAny variables to be passed to the Fluent message.
use_isolatingbool, optionalWhether to insert Unicode Directionality Isolation Marks around placeables, to indicate that their direction may differ from the surrounding message. Defaults to True.
Supported variable types:
  • str: Rendered as-is.
  • int: Must be in the range -2,147,483,648 to 2,147,483,647. Rendered as-is.
  • datetime.date: Rendered in the form YYYY-MM-DD.
Return value

str: the translated message.

If there is a problem with a passed variable (e.g. it is of the wrong type or an integer that is larger than a signed long integer), then the name of the variable will be used instead.

Raises
  • ValueError if the message could not be found or has no translation available.
  • TypeError if a passed variable name (i.e. a key in the variables dict) is not a string.

Contributing

See Contributing.

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc