Pslink/app/src/i18n.rs

99 lines
2.8 KiB
Rust
Raw Normal View History

use std::sync::Arc;
use fluent::{FluentArgs, FluentBundle, FluentResource};
2021-05-31 06:55:56 +02:00
use seed::log;
2021-06-13 12:58:55 +02:00
use shared::datatypes::Lang;
use unic_langid::LanguageIdentifier;
2021-05-24 14:29:14 +02:00
// A struct containing the functions and the current language to query the localized strings.
#[derive(Clone)]
pub struct I18n {
lang: Lang,
ftl_bundle: Arc<FluentBundle<FluentResource>>,
}
impl std::fmt::Debug for I18n {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.lang)
}
}
impl I18n {
2021-05-24 14:29:14 +02:00
/// Create a new translator struct
#[must_use]
pub fn new(lang: Lang) -> Self {
2021-06-13 12:58:55 +02:00
let ftl_bundle = Arc::new(Self::create_ftl_bundle(lang));
Self { lang, ftl_bundle }
}
2021-05-24 14:29:14 +02:00
/// Get the current language
#[must_use]
pub const fn lang(&self) -> &Lang {
&self.lang
}
2021-05-24 14:29:14 +02:00
/// Set the current language
2021-06-13 12:58:55 +02:00
pub fn set_lang(&mut self, lang: Lang) {
self.lang = lang;
2021-06-13 12:58:55 +02:00
self.ftl_bundle = Arc::new(Self::create_ftl_bundle(lang));
}
2021-05-24 14:29:14 +02:00
/// Get a localized string. Optionally with parameters provided in `args`.
pub fn translate(&self, key: impl AsRef<str>, args: Option<&FluentArgs>) -> String {
2021-05-31 06:55:56 +02:00
log!(key.as_ref());
let msg = self
.ftl_bundle
.get_message(key.as_ref())
2021-05-24 14:29:14 +02:00
.expect("Failed to get fluent message for key {}");
2021-05-24 14:29:14 +02:00
let pattern = msg.value().expect("Failed to parse pattern");
self.ftl_bundle
.format_pattern(pattern, args, &mut vec![])
.to_string()
}
}
2021-06-13 12:58:55 +02:00
impl I18n {
2021-05-24 14:29:14 +02:00
/// Prettyprint the language name
#[must_use]
2021-06-13 12:58:55 +02:00
pub const fn label(&self) -> &'static str {
match self.lang {
Lang::EnUS => "English (US)",
Lang::DeDE => "Deutsch (Deutschland)",
}
}
2021-05-24 14:29:14 +02:00
/// include the fluent messages into the binary
#[must_use]
2021-06-13 12:58:55 +02:00
pub const fn ftl_messages(lang: Lang) -> &'static str {
macro_rules! include_ftl_messages {
( $lang_id:literal ) => {
include_str!(concat!("../../locales/", $lang_id, "/main.ftl"))
};
}
2021-06-13 12:58:55 +02:00
match lang {
Lang::EnUS => include_ftl_messages!("en"),
Lang::DeDE => include_ftl_messages!("de"),
}
}
#[must_use]
2021-06-13 12:58:55 +02:00
pub fn language_identifier(lang: Lang) -> LanguageIdentifier {
lang.as_ref()
.parse()
.expect("parse Lang to LanguageIdentifier")
}
2021-05-24 14:29:14 +02:00
/// Create and initialize a fluent bundle.
#[must_use]
2021-06-13 12:58:55 +02:00
pub fn create_ftl_bundle(lang: Lang) -> FluentBundle<FluentResource> {
let ftl_resource = FluentResource::try_new(Self::ftl_messages(lang).to_owned())
.expect("parse FTL messages");
2021-06-13 12:58:55 +02:00
let mut bundle = FluentBundle::new(vec![Self::language_identifier(lang)]);
bundle.add_resource(ftl_resource).expect("add FTL resource");
bundle
}
}