1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! # amethyst_locale
//!
//! Localisation binding a `Fluent` file to an Asset<Locale> via the use of amethyst_assets.
#![warn(missing_docs)]

extern crate amethyst_assets;
extern crate amethyst_core;
extern crate fluent;

#[macro_use]
#[cfg(feature = "profiler")]
extern crate thread_profiler;

#[cfg(feature = "profiler")]
use thread_profiler::{register_thread_with_profiler, write_profile};

use amethyst_assets::{Asset, Handle, ProcessingState, Result, SimpleFormat};
use amethyst_core::specs::prelude::VecStorage;
use fluent::MessageContext;

/// Loads the strings from localisation files.
#[derive(Clone)]
pub struct LocaleFormat;

impl SimpleFormat<Locale> for LocaleFormat {
    const NAME: &'static str = "FTL";

    type Options = ();

    fn import(&self, bytes: Vec<u8>, _: ()) -> Result<Locale> {
        let s = String::from_utf8(bytes)?;
        let mut ctx = MessageContext::new(&[]);
        ctx.add_messages(&s);
        Ok(Locale { context: ctx })
    }
}

impl Into<Result<ProcessingState<Locale>>> for Locale {
    fn into(self) -> Result<ProcessingState<Locale>> {
        Ok(ProcessingState::Loaded(self))
    }
}

/// A handle to a locale.
pub type LocaleHandle = Handle<Locale>;

/// A loaded locale.
pub struct Locale {
    /// The message context.
    pub context: MessageContext<'static>,
}

impl Asset for Locale {
    const NAME: &'static str = "locale::Locale";
    type Data = Locale;
    type HandleStorage = VecStorage<LocaleHandle>;
}