bevy_fluent
repository·main·Indexed 41 days ago
https://github.com/kgv/bevy_fluentA localization plugin for the Bevy game engine that leverages the Fluent syntax for managing translations and internationalization. It provides support for loading Fluent assets via the Bevy AssetServer, managing locale fallback chains, and resolving translated content using BundleAsset and ResourceAsset types. Version 0.15.0 is compatible with Bevy 0.19.
What's inside bevy_fluent
- Bevy fluent is a plugin for the Bevy game engine that provides localization capabilities using the Fluent system. It allows developers to manage multi-language support within Bevy applications.
Core concepts of Bevy Fluent localization
mainUnderstanding the hierarchy of Fluent assets in
bevy_fluent:- Localization: A fallback chain of Fluent bundles.
- Bundle asset: An abstraction for presentation Fluent bundles, using the
.ftl.ronextension. - Resource asset: The atomic unit of disk storage for Fluent, using the
.ftlextension. It contains a set of messages. - Message: The basic atomic translation unit, identified by a unique Identifier.
- Content: The formatted Pattern resulting from a Request for a message, term, variant, or attribute.
Core concepts of Bevy Fluent
mainUnderstanding the hierarchy of localization in
bevy_fluent:- Localization: A fallback chain of
FluentBundles. - BundleAsset: An abstraction for Fluent bundles, represented by
.ftl.ronfiles. - ResourceAsset: An abstraction for a single
FluentResource, represented by.ftlfiles. This is the atomic unit of storage on disk. - Message: An atomic unit of translation within a resource, identified by a unique Identifier.
- Content: The final formatted string resulting from a Request applied to a message's pattern (which may contain terms, variants, or attributes).
- Localization: A fallback chain of
Load Fluent assets in Bevy
mainYou can load Fluent assets using the standard Bevy
AssetServeror theAssetServerExttrait for pattern matching.- Use
asset_server.loadto load a specific.ftl.ronfile. - Use
asset_server.load_glob(frombevy_fluent::exts::bevy::AssetServerExt) to load multiple assets matching a glob pattern.
Note that
.ftl.ronfiles representBundleAssets, which are abstractions for Fluent bundles.// Load a single bundle asset let handle = asset_server.load("locales/ru-RU/main.ftl.ron"); // Load all bundle assets matching a pattern use bevy_fluent::exts::bevy::AssetServerExt; let handles = asset_server.load_glob("locales/**/main.ftl.ron")?;- Use
Load Fluent assets using AssetServer
mainYou can load Fluent assets into your Bevy project using the standard
AssetServer.To load a specific bundle asset, use the
.load()method with a path ending in.ftl.ron. To load multiple assets at once (for example, all locales), use theload_globmethod provided by theAssetServerExttrait.File Extensions
- Bundle assets: Use
.ftl.ron(represents a Fluent bundle fallback chain). - Resource assets: Use
.ftl(represents an atomic Fluent resource).
// Load a single bundle asset let handle = asset_server.load("locales/en-US/main.ftl.ron"); // Load all assets matching a glob pattern using AssetServerExt use bevy_fluent::exts::bevy::AssetServerExt; let handles = asset_server.load_glob("locales/**/main.ftl.ron")?;- Bundle assets: Use
Check Fluent asset load state
mainBefore attempting to use localization assets, ensure they have finished loading by checking their load state via the
AssetServer.You can check the state of a single handle or a group of handles (e.g., from a glob load).
// Check a single handle if let LoadState::Loaded = asset_server.get_load_state(handle) { // Assets are ready } // Check a group of handles if let LoadState::Loaded = asset_server.get_group_load_state(handles) { // All assets in the group are ready }Check Bevy and Bevy fluent version compatibility
mainWhen using
bevy_fluent, you must ensure that your version of thebevyengine is compatible with the version ofbevy_fluentyou are using. Refer to the compatibility matrix below to select the correct versions for your project:bevy bevy_fluent 0.19 0.15 0.18 0.14 0.17 0.13 0.16 0.12 0.15 0.11 0.14 0.10 0.13 0.9 0.12 0.8 0.11 0.7 0.10 0.6 0.9 0.5 Add Fluent support to Bevy using FluentPlugin
mainTo enable Fluent file loading in your Bevy application, add the
FluentPluginto your app. This plugin registers the necessary asset loaders and initializes the core asset types:ResourceAssetandBundleAsset.use bevy::prelude::*; use bevy_fluent::FluentPlugin; fn main() { App::new() .add_plugins(DefaultPlugins) .add_plugins(FluentPlugin) .run(); }Use the bevy_fluent prelude
mainTo simplify imports, you can use the
preludemodule which exports the most commonly used types and plugins for localization. This includesBundleAsset,FluentPlugin,Locale,Localization, andLocalizationBuilder.use bevy_fluent::prelude::*;Check asset loading status
mainTo ensure your localization assets are ready before use, you can check their loading state using
AssetServermethods:get_load_state(handle): Checks the status of a single asset handle.get_group_load_state(handles): Checks the status of a collection of asset handles.
// Check a single asset if let LoadState::Loaded = asset_server.get_load_state(handle) { ... } // Check multiple assets if let LoadState::Loaded = asset_server.get_group_load_state(handles) { ... }Request localized content
mainOnce assets are loaded, you can retrieve the actual translated text (content) using the
.content()method. This can be called on either a singleBundleAssetor the entireLocalizationfallback chain.Both methods take an identifier (the message ID) and return the formatted content.
// Request from a specific bundle asset let hello_world = bundle_asset.content("hello-world")?; // Request from the localization fallback chain let hello_world = localization.content("hello-world")?;Create a Localization fallback chain
mainUse
LocalizationBuilderto create aLocalizationobject from a set of loaded asset handles. ThisLocalizationobject represents a fallback chain of Fluent bundles, allowing you to manage multiple locales and their fallback logic.let localization = localization_builder.build(handles);