Fluent Runtime
Fluent Runtime is the Python implementation of Project Fluent's runtime component, providing localization capabilities for expressive translations. It handles parsing and formatting of Fluent Localization (FTL) files. The current version is 0.4.0, with new releases occurring infrequently, often in conjunction with updates to the `fluent.syntax` library.
Warnings
- breaking Support for Python 2.7 and 3.5 was dropped in version 0.4.0. Users on older Python versions must remain on `fluent-runtime<0.4.0`.
- breaking Major API changes were introduced in version 0.3.0. `FluentBundle.add_messages` was removed in favor of `FluentBundle.add_resource`. Additionally, `bundle.format()` was replaced by `bundle.format_pattern(bundle.get_message().value)` for direct formatting.
- gotcha `fluent-runtime` is tightly coupled with `fluent.syntax`. Ensure your `fluent.syntax` version is compatible with your `fluent-runtime` version. For `fluent-runtime 0.4.x`, `fluent.syntax 0.19.x` is required.
- gotcha For applications requiring locale management, fallback mechanisms, or dynamic resource loading, `FluentLocalization` is the recommended high-level entry point over direct `FluentBundle` usage.
Install
-
pip install fluent-runtime
Imports
- FluentResource
from fluent.runtime import FluentResource
- FluentBundle
from fluent.runtime import FluentBundle
- FluentLocalization
from fluent.runtime import FluentLocalization
Quickstart
from fluent.runtime import FluentResource, FluentBundle, FluentLocalization
from datetime import datetime
# 1. Define your FTL resources
resource_en = FluentResource("""
hello = Hello, { $name }!
welcome = Welcome, today is {DATETIME($date)}
""")
resource_fr = FluentResource("""
hello = Bonjour, { $name }!
welcome = Bienvenue, aujourd'hui c'est le {DATETIME($date)}
""")
# 2. Create FluentBundles for each locale
bundle_en = FluentBundle(["en-US"], use_isolating=False)
bundle_en.add_resource(resource_en)
bundle_fr = FluentBundle(["fr"], use_isolating=False)
bundle_fr.add_resource(resource_fr)
# 3. Use FluentLocalization as the main entrypoint
loc = FluentLocalization(
["en-US", "fr"],
[
lambda locales: bundle_en, # Provider function for 'en-US'
lambda locales: bundle_fr # Provider function for 'fr'
]
)
# 4. Format messages
message_en_hello, errors_en = loc.format_value("hello", {"name": "World"})
print(f"EN Hello: {message_en_hello}")
message_fr_welcome, errors_fr = loc.format_value("welcome", {"date": datetime.now()})
print(f"FR Welcome: {message_fr_welcome}")
# Example Output:
# EN Hello: Hello, World!
# FR Welcome: Bienvenue, aujourd'hui c'est le 2024-04-11 12:34:56