Add l10n
This commit is contained in:
parent
a78632be08
commit
f174efc649
22
src/i18n.rs
22
src/i18n.rs
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Reference in New Issue