1
0
Fork 0
This commit is contained in:
Alex Kotov 2020-10-21 09:37:26 +05:00
parent a78632be08
commit f174efc649
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
2 changed files with 20 additions and 8 deletions

View File

@ -6,6 +6,8 @@ use unic_langid::LanguageIdentifier;
pub struct I18n(HashMap<String, FluentBundle<FluentResource>>);
pub struct L10n<'a>(&'a FluentBundle<FluentResource>);
impl I18n {
pub fn new(path: &str, locales: &[&str]) -> Result<Self, ()> {
let lang_ids: Vec<Result<LanguageIdentifier, _>> =
@ -51,11 +53,19 @@ impl I18n {
Ok(Self(hash_map))
}
pub fn dummy_translate(&self, locale: &str, key: &str) -> String {
let bundle = self.0.get(locale).unwrap();
let msg = bundle.get_message(key).unwrap();
let val = msg.value.unwrap();
let mut errors = vec![];
bundle.format_pattern(val, None, &mut errors).to_string()
pub fn l10n<'a>(&'a self, locale: &'a str) -> Option<L10n<'a>> {
match self.0.get(locale) {
None => None,
Some(bundle) => Some(L10n(bundle)),
}
}
}
impl L10n<'_> {
pub fn dummy_translate(&self, key: &str) -> String {
let msg = self.0.get_message(key).unwrap();
let val = msg.value.unwrap();
let mut errors = vec![];
self.0.format_pattern(val, None, &mut errors).to_string()
}
}

View File

@ -14,9 +14,11 @@ pub fn index(
csrf_token: CsrfToken,
current_user: states::MaybeCurrentUser,
) -> Result<Template, CommonResponse> {
let l10n = i18n.l10n("en").unwrap();
let page_context = views::home::Index {
i18n_fedihub: i18n.dummy_translate("en", "fedihub"),
i18n_federated_services_without_censorship: i18n.dummy_translate("en", "federated-services-without-censorship"),
i18n_fedihub: l10n.dummy_translate("fedihub"),
i18n_federated_services_without_censorship: l10n.dummy_translate("federated-services-without-censorship"),
};
let context = views::Site {