amethyst_locale/
lib.rs

1//! # amethyst_locale
2//!
3//! Localisation binding a `Fluent` file to an Asset<Locale> via the use of amethyst_assets.
4
5#![warn(
6    missing_debug_implementations,
7    missing_docs,
8    rust_2018_idioms,
9    rust_2018_compatibility
10)]
11#![warn(clippy::all)]
12
13use amethyst_assets::{Asset, Format, Handle};
14use amethyst_core::ecs::prelude::VecStorage;
15use amethyst_error::Error;
16pub use fluent::{concurrent::FluentBundle, FluentResource};
17use serde::{Deserialize, Serialize};
18use unic_langid::langid;
19
20/// Loads the strings from localisation files.
21#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
22pub struct LocaleFormat;
23
24amethyst_assets::register_format_type!(Locale);
25
26amethyst_assets::register_format!("FTL", LocaleFormat as Locale);
27impl Format<Locale> for LocaleFormat {
28    fn name(&self) -> &'static str {
29        "FTL"
30    }
31
32    fn import_simple(&self, bytes: Vec<u8>) -> Result<Locale, Error> {
33        let s = String::from_utf8(bytes)?;
34
35        let resource = FluentResource::try_new(s).expect("Failed to parse locale data");
36        let lang_en = langid!("en");
37        let mut bundle = FluentBundle::new(&[lang_en]);
38
39        bundle
40            .add_resource(resource)
41            .expect("Failed to add resource");
42
43        Ok(Locale { bundle })
44    }
45}
46
47/// A handle to a locale.
48pub type LocaleHandle = Handle<Locale>;
49
50/// A loaded locale.
51#[allow(missing_debug_implementations)]
52pub struct Locale {
53    /// The bundle stores its resources for now.
54    pub bundle: FluentBundle<FluentResource>,
55}
56
57impl Asset for Locale {
58    const NAME: &'static str = "locale::Locale";
59    type Data = Locale;
60    type HandleStorage = VecStorage<LocaleHandle>;
61}