Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
kgv committed Nov 15, 2023
1 parent 99454c8 commit c63a7b0
Show file tree
Hide file tree
Showing 37 changed files with 574 additions and 368 deletions.
5 changes: 5 additions & 0 deletions doc/en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ Request content:
let hello_world = bundle_asset.content("hello-world")?;
```

## File formats

- `ResourceAsset` have to be `FTL`.
- `BundlesAsset` files may be `YAML` or `RON`

## Definitions
[`BundleAsset`][bundle-asset] - is an abstraction for presentation
[`FluentBundle`][fluent-bundle]. A *bundles* file has the extension `.ftl.ron`
Expand Down
3 changes: 3 additions & 0 deletions examples/complex/assets/locales/monolingual/.ftl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
en-US: [en-US/main.ftl]
ru-BY: [ru/ru-BY/main.ftl]
ru-RU: [ru/ru-RU/main.ftl]
5 changes: 5 additions & 0 deletions examples/complex/assets/locales/multilingual/.ftl.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"*de-DE": ["de-DE.ftl"],
"en-US": ["en-US.ftl"],
"ru-RU": ["ru-RU.ftl"],
}
1 change: 1 addition & 0 deletions examples/complex/assets/locales/multilingual/de-DE.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello-world = hallo welt
1 change: 1 addition & 0 deletions examples/complex/assets/locales/multilingual/ru-RU.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello-world = привет мир
98 changes: 98 additions & 0 deletions examples/complex/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use bevy::{asset::LoadState, prelude::*};
use bevy_fluent::{
assets::bundles::{Locale, Settings},
prelude::*,
};
use fluent_content::Content;
use unic_langid::langid;

// Locale fallback chain.
pub fn main() {
App::new()
.add_plugins((
DefaultPlugins.set(AssetPlugin {
file_path: "examples/complex/assets".to_string(),
..default()
}),
FluentPlugin,
))
.add_systems(Update, (monolingual, multilingual))
.run();
}

// Loads locales fallback chain bundle (monolingual) by settings.
// - Note: the `BundlesAsset` file uses the yaml format.
fn monolingual(
asset_server: Res<AssetServer>,
assets: Res<Assets<BundlesAsset>>,
mut handle: Local<Option<Handle<BundlesAsset>>>,
) {
let handle = &*handle.get_or_insert_with(|| {
asset_server.load_with_settings(
"locales/monolingual/.ftl.yml",
|settings: &mut Settings| {
settings.locales.push(Locale {
requested: vec![langid!("ru")],
default: Some(langid!("en-US")),
..default()
});
},
)
});
if let Some(LoadState::Loaded) = asset_server.get_load_state(handle) {
let bundles = assets.get(handle).unwrap();
assert_eq!(bundles.len(), 1);
assert_eq!(bundles["ru"].locales, ["ru-RU", "ru-BY", "en-US"]);
// From ru-RU bundle, the first in fallback chain.
assert_eq!(bundles.content("hello").unwrap(), "привет");
// From ru-BY bundle, the second in fallback chain.
assert_eq!(bundles.content("world").unwrap(), "свету");
// From en-US bundle, the last in fallback chain, default locale.
assert_eq!(bundles.content("bevy").unwrap(), "bevy");
}
}

// Loads locales fallback chain bundles (multilingual) by settings.
// - Note: the `BundlesAsset` file contains default locale selector ("de-DE").
// But the locale "ru" overrides it to "en-US" via settings.
fn multilingual(
asset_server: Res<AssetServer>,
assets: Res<Assets<BundlesAsset>>,
mut handle: Local<Option<Handle<BundlesAsset>>>,
) {
let handle = &*handle.get_or_insert_with(|| {
asset_server.load_with_settings(
"locales/multilingual/.ftl.ron",
|settings: &mut Settings| {
settings.locales = vec![
Locale {
requested: vec![langid!("de")],
..default()
},
Locale {
requested: vec![langid!("en")],
..default()
},
Locale {
requested: vec![langid!("ru")],
default: Some(langid!("en-US")),
..default()
},
];
},
)
});
if let Some(LoadState::Loaded) = asset_server.get_load_state(handle) {
let bundles = assets.get(handle).unwrap();
assert_eq!(bundles.len(), 3);
assert_eq!(bundles["de"].locales, ["de-DE"]);
assert_eq!(bundles["en"].locales, ["en-US", "de-DE"]);
assert_eq!(bundles["ru"].locales, ["ru-RU", "en-US"]);
// From de-DE bundle.
assert_eq!(bundles["de"].content("hello-world").unwrap(), "hallo welt");
// From en-US bundle.
assert_eq!(bundles["en"].content("hello-world").unwrap(), "hello world");
// From ru-RU bundle.
assert_eq!(bundles["ru"].content("hello-world").unwrap(), "привет мир");
}
}
5 changes: 0 additions & 5 deletions examples/fallback_chain/assets/locales/.ftl.ron

This file was deleted.

52 changes: 0 additions & 52 deletions examples/fallback_chain/main.rs

This file was deleted.

2 changes: 0 additions & 2 deletions examples/minimal/assets/locales/.ftl.yml

This file was deleted.

28 changes: 0 additions & 28 deletions examples/minimal/main.rs

This file was deleted.

3 changes: 0 additions & 3 deletions examples/multilingual/assets/locales/de-DE.ftl

This file was deleted.

3 changes: 0 additions & 3 deletions examples/multilingual/assets/locales/en-US.ftl

This file was deleted.

3 changes: 0 additions & 3 deletions examples/multilingual/assets/locales/ru-RU.ftl

This file was deleted.

43 changes: 0 additions & 43 deletions examples/multilingual/main.rs

This file was deleted.

1 change: 1 addition & 0 deletions examples/trivial/assets/locales/en-US.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello-world = hello world
1 change: 1 addition & 0 deletions examples/trivial/assets/locales/ru-RU.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello-world = привет мир
4 changes: 4 additions & 0 deletions examples/trivial/assets/locales/with_default.ftl.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"*en-US": ["en-US.ftl"],
"ru-RU": ["ru-RU.ftl"],
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"de-DE": ["de-DE.ftl"],
"en-US": ["en-US.ftl"],
"ru-RU": ["ru-RU.ftl"],
}
76 changes: 76 additions & 0 deletions examples/trivial/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use bevy::{asset::LoadState, prelude::*};
use bevy_fluent::prelude::*;
use fluent_content::Content;

// Single locale.
pub fn main() {
App::new()
.add_plugins((
DefaultPlugins.set(AssetPlugin {
file_path: "examples/trivial/assets".to_string(),
..default()
}),
FluentPlugin,
))
.add_systems(Update, (empty, selector, label))
.run();
}

// Loads a bundle without everything.
// - Note: no default locale selector is set, no label is set, no settings are
// set. In this case `BundlesAsset` will be empty.
fn empty(
asset_server: Res<AssetServer>,
assets: Res<Assets<BundlesAsset>>,
mut handle: Local<Option<Handle<BundlesAsset>>>,
) {
let handle =
&*handle.get_or_insert_with(|| asset_server.load("locales/without_default.ftl.ron"));
if let Some(LoadState::Loaded) = asset_server.get_load_state(handle) {
let bundles = assets.get(handle).unwrap();
assert_eq!(bundles.len(), 0);
}
}

// Loads a bundle using a default locale selector.
// - Note: the default locale selector is set ("en-US").
fn selector(
asset_server: Res<AssetServer>,
assets: Res<Assets<BundlesAsset>>,
mut handle: Local<Option<Handle<BundlesAsset>>>,
) {
let handle = &*handle.get_or_insert_with(|| asset_server.load("locales/with_default.ftl.ron"));
if let Some(LoadState::Loaded) = asset_server.get_load_state(handle) {
let bundles = assets.get(handle).unwrap();
assert_eq!(bundles.len(), 1);
assert_eq!(bundles[0].locales, ["en-US"]);
assert_eq!(bundles.content("hello-world").unwrap(), "hello world");
}
}

// Loads a bundle using a label.
// - Note: whether a default locale selector is set or not, the bandle will be
// loaded by label.
fn label(
asset_server: Res<AssetServer>,
assets: Res<Assets<BundlesAsset>>,
mut ru: Local<Option<Handle<BundlesAsset>>>,
mut en: Local<Option<Handle<BundlesAsset>>>,
) {
let handle =
&*en.get_or_insert_with(|| asset_server.load("locales/without_default.ftl.ron#en-US"));
if let Some(LoadState::Loaded) = asset_server.get_load_state(handle) {
let bundles = assets.get(handle).unwrap();
assert_eq!(bundles.len(), 1);
assert_eq!(bundles[0].locales, ["en-US"]);
assert_eq!(bundles.content("hello-world").unwrap(), "hello world");
}
let handle =
&*ru.get_or_insert_with(|| asset_server.load("locales/with_default.ftl.ron#ru-RU"));
if let Some(LoadState::Loaded) = asset_server.get_load_state(handle) {
let bundles = assets.get(handle).unwrap();
assert_eq!(bundles.len(), 1);
assert_eq!(bundles[0].locales, ["ru-RU"]);
assert_eq!(bundles.content("hello-world").unwrap(), "привет мир");
}
}
2 changes: 1 addition & 1 deletion examples/ui/assets/locales/.ftl.ron
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"en-US": ["en-US/menu/ui.ftl"],
"ru-BY": ["ru/ru-BY/menu/ui.ftl"],
"ru-RU": ["ru/ru-RU/menu/ui.ftl"],
"und": ["und/menu/locales.ftl"],
"*und": ["und/menu/locales.ftl"],
}
Loading

0 comments on commit c63a7b0

Please sign in to comment.