bevy_fluent

repository·main·Indexed 41 days ago

https://github.com/kgv/bevy_fluent

A 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.

Tokens
4.9K
Snippets
18
Records
25
Agent score
50%

What's inside bevy_fluent

  1. Core concepts of Bevy Fluent localization

    main

    Understanding 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.ron extension.
    • Resource asset: The atomic unit of disk storage for Fluent, using the .ftl extension. 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.
  2. Core concepts of Bevy Fluent

    main

    Understanding the hierarchy of localization in bevy_fluent:

    • Localization: A fallback chain of FluentBundles.
    • BundleAsset: An abstraction for Fluent bundles, represented by .ftl.ron files.
    • ResourceAsset: An abstraction for a single FluentResource, represented by .ftl files. 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).
  3. Load Fluent assets in Bevy

    main

    You can load Fluent assets using the standard Bevy AssetServer or the AssetServerExt trait for pattern matching.

    • Use asset_server.load to load a specific .ftl.ron file.
    • Use asset_server.load_glob (from bevy_fluent::exts::bevy::AssetServerExt) to load multiple assets matching a glob pattern.

    Note that .ftl.ron files represent BundleAssets, 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")?;
  4. Load Fluent assets using AssetServer

    main

    You 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 the load_glob method provided by the AssetServerExt trait.

    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")?;
  5. Check Fluent asset load state

    main

    Before 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
    }
  6. Check Bevy and Bevy fluent version compatibility

    main

    When using bevy_fluent, you must ensure that your version of the bevy engine is compatible with the version of bevy_fluent you are using. Refer to the compatibility matrix below to select the correct versions for your project:

    bevybevy_fluent
    0.190.15
    0.180.14
    0.170.13
    0.160.12
    0.150.11
    0.140.10
    0.130.9
    0.120.8
    0.110.7
    0.100.6
    0.90.5
  7. Add Fluent support to Bevy using FluentPlugin

    main

    To enable Fluent file loading in your Bevy application, add the FluentPlugin to your app. This plugin registers the necessary asset loaders and initializes the core asset types: ResourceAsset and BundleAsset.

    use bevy::prelude::*;
    use bevy_fluent::FluentPlugin;
    
    fn main() {
        App::new()
            .add_plugins(DefaultPlugins)
            .add_plugins(FluentPlugin)
            .run();
    }
  8. Use the bevy_fluent prelude

    main

    To simplify imports, you can use the prelude module which exports the most commonly used types and plugins for localization. This includes BundleAsset, FluentPlugin, Locale, Localization, and LocalizationBuilder.

    use bevy_fluent::prelude::*;
  9. Check asset loading status

    main

    To ensure your localization assets are ready before use, you can check their loading state using AssetServer methods:

    • 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) {
        ...
    }
  10. Request localized content

    main

    Once assets are loaded, you can retrieve the actual translated text (content) using the .content() method. This can be called on either a single BundleAsset or the entire Localization fallback 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")?;
  11. Create a Localization fallback chain

    main

    Use LocalizationBuilder to create a Localization object from a set of loaded asset handles. This Localization object represents a fallback chain of Fluent bundles, allowing you to manage multiple locales and their fallback logic.

    let localization = localization_builder.build(handles);