-
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
37 changed files
with
574 additions
and
368 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
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,3 @@ | ||
en-US: [en-US/main.ftl] | ||
ru-BY: [ru/ru-BY/main.ftl] | ||
ru-RU: [ru/ru-RU/main.ftl] |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 @@ | ||
{ | ||
"*de-DE": ["de-DE.ftl"], | ||
"en-US": ["en-US.ftl"], | ||
"ru-RU": ["ru-RU.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 |
---|---|---|
@@ -0,0 +1 @@ | ||
hello-world = hallo welt |
File renamed without changes.
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 @@ | ||
hello-world = привет мир |
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,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(), "привет мир"); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
hello-world = hello world |
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 @@ | ||
hello-world = привет мир |
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,4 @@ | ||
{ | ||
"*en-US": ["en-US.ftl"], | ||
"ru-RU": ["ru-RU.ftl"], | ||
} |
1 change: 0 additions & 1 deletion
1
...ples/multilingual/assets/locales/.ftl.ron → ...al/assets/locales/without_default.ftl.ron
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,5 +1,4 @@ | ||
{ | ||
"de-DE": ["de-DE.ftl"], | ||
"en-US": ["en-US.ftl"], | ||
"ru-RU": ["ru-RU.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 |
---|---|---|
@@ -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(), "привет мир"); | ||
} | ||
} |
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
Oops, something went wrong.