-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
47 changed files
with
374 additions
and
595 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "bevy_fluent" | ||
version = "0.8.0" | ||
version = "0.9.0" | ||
authors = ["g <[email protected]>"] | ||
edition = "2021" | ||
description = "Bevy plugin for localization using Fluent" | ||
|
@@ -18,23 +18,23 @@ categories = [ | |
exclude = [".github/**/*"] | ||
|
||
[dependencies] | ||
bevy = { version = "0.12", default-features = false, features = ["bevy_asset"] } | ||
bevy = { version = "0.12.0", default-features = false, features = [ | ||
"bevy_asset", | ||
] } | ||
fluent = "0.16.0" | ||
fluent_content = "0.0.5" | ||
fluent-langneg = "0.13.0" | ||
futures-lite = "2.0.0" | ||
futures-lite = "2.0.1" | ||
indexmap = { version = "2.1.0", features = ["serde"] } | ||
intl-memoizer = "0.5.1" | ||
ron = "0.8.1" | ||
serde = { version = "1.0.188", features = ["derive"] } | ||
serde = { version = "1.0.192", features = ["derive"] } | ||
serde_yaml = "0.9.27" | ||
thiserror = "1.0.50" | ||
tracing = "0.1.40" | ||
unic-langid = { version = "0.9.1", features = ["serde"] } | ||
uuid = { version = "1.5.0", features = ["serde", "v4", "v5"] } | ||
# fluent-syntax = { git = "https://github.com/projectfluent/fluent-rs" } | ||
# globset = "0.4.13" | ||
|
||
[dev-dependencies] | ||
bevy = "0.12" | ||
bevy = "0.12.0" | ||
unic-langid = { version = "0.9.1", features = ["macros"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"en-US": ["en-US/main.ftl"], | ||
"ru-BY": ["ru/ru-BY/main.ftl"], | ||
"ru-RU": ["ru/ru-RU/main.ftl"], | ||
} |
2 changes: 1 addition & 1 deletion
2
...hain/assets/locales/en-US/hello_world.ftl → ...lback_chain/assets/locales/en-US/main.ftl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
hello = hello | ||
world = world | ||
hello-world = hello world | ||
bevy = bevy |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,52 @@ | ||
use bevy::{ | ||
asset::{LoadState, LoadedFolder}, | ||
prelude::*, | ||
}; | ||
use bevy_fluent::prelude::*; | ||
use bevy::{asset::LoadState, prelude::*}; | ||
use bevy_fluent::{prelude::*, resources::Locales}; | ||
use fluent_content::Content; | ||
use unic_langid::langid; | ||
|
||
pub fn main() { | ||
App::new() | ||
.insert_resource(Locale::new(langid!("ru-RU")).with_default(langid!("en-US"))) | ||
.insert_resource( | ||
Locales::new([langid!("en-US"), langid!("ru-RU"), langid!("ru-BY")]) | ||
.with_default(langid!("en-US")), | ||
) | ||
.add_plugins(( | ||
DefaultPlugins.set(AssetPlugin { | ||
file_path: "examples/fallback_chain/assets".to_string(), | ||
..default() | ||
}), | ||
FluentPlugin, | ||
)) | ||
.add_systems(Update, localized_hello_world) | ||
.add_systems(Update, localize) | ||
.run(); | ||
} | ||
|
||
fn localized_hello_world( | ||
localization_builder: LocalizationBuilder, | ||
fn localize( | ||
locales: Res<Locales>, | ||
asset_server: Res<AssetServer>, | ||
mut handle: Local<Option<Handle<LoadedFolder>>>, | ||
mut localization: Local<Option<Localization>>, | ||
assets: Res<Assets<BundleAsset>>, | ||
mut handles: Local<Option<Vec<Handle<BundleAsset>>>>, | ||
mut bundles: Local<Bundles>, | ||
) { | ||
let handle = &*handle.get_or_insert_with(|| asset_server.load_folder("locales")); | ||
if let Some(LoadState::Loaded) = asset_server.get_load_state(handle) { | ||
let localization = localization.get_or_insert_with(|| localization_builder.build(handle)); | ||
let handles = handles.get_or_insert_with(|| { | ||
locales | ||
.request(Some(langid!("ru-RU"))) | ||
.iter() | ||
.map(|locale| asset_server.load(format!("locales/.ftl.ron#{locale}"))) | ||
.collect() | ||
}); | ||
if handles | ||
.iter() | ||
.all(|handle| asset_server.get_load_state(handle) == Some(LoadState::Loaded)) | ||
{ | ||
*bundles = handles | ||
.iter() | ||
.map(|handle| (handle.clone(), assets.get(handle).unwrap().clone())) | ||
.collect(); | ||
// From ru-RU bundle, the first in fallback chain. | ||
assert!(matches!(localization.content("hello"), Some(content) if content == "привет")); | ||
assert!(matches!(bundles.content("hello"), Some(content) if content == "привет")); | ||
// From ru-BY bundle, the second in fallback chain. | ||
assert!(matches!(localization.content("world"), Some(content) if content == "свету")); | ||
assert!(matches!(bundles.content("world"), Some(content) if content == "свету")); | ||
// From en-US bundle, the last in fallback chain, default locale. | ||
assert!( | ||
matches!(localization.content("hello-world"), Some(content) if content == "hello world") | ||
); | ||
assert!(matches!(bundles.content("bevy"), Some(content) if content == "bevy")); | ||
} | ||
} |
4 changes: 1 addition & 3 deletions
4
...minimal/assets/locales/en-US/main.ftl.yml → examples/minimal/assets/locales/.ftl.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,2 @@ | ||
# Locale files may be in YAML as in this example, or in RON | ||
locale: en-US | ||
resources: | ||
- hello_world.ftl | ||
en-US: [en-US/main.ftl] |
File renamed without changes.
Oops, something went wrong.