Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/web/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,32 @@ impl Tr {
crate::web::i18n::LOCALES.lookup(&self.lang, key)
}

/// Translate a pluralized message. Passes `$count` as a number so Fluent
/// plural selectors (`[one]` / `[other]`, etc.) resolve correctly.
pub fn tn(&self, key: &str, count: &usize) -> String {
let args = std::collections::HashMap::from([("count", fluent::FluentValue::from(count))]);
crate::web::i18n::LOCALES.lookup_with_args(&self.lang, key, &args)
}

#[cfg(feature = "server")]
pub fn lang_string(&self) -> String {
self.lang.to_string()
}
}

#[cfg(test)]
mod tr_tests {
use super::*;
use unic_langid::langid;

#[test]
fn recipes_count_pluralizes() {
let tr = Tr::new(langid!("en-US"));
assert_eq!(tr.tn("recipes-count", &1), "1 recipe");
assert_eq!(tr.tn("recipes-count", &3), "3 recipes");
}
}

mod filters {
use askama::Result;
use url::Url;
Expand Down
2 changes: 1 addition & 1 deletion templates/recipes.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ <h2 class="text-xl font-bold">{{ tr.t("todays-menu-title") }}</h2>
<h3 class="font-bold text-lg mb-2">{{ item.name }}</h3>
{% match item.count %}
{% when Some with (count) %}
<span class="text-sm text-orange-600 font-medium">{{ count }} recipes</span>
<span class="text-sm text-orange-600 font-medium">{{ tr.tn("recipes-count", count) }}</span>
{% when None %}
{% endmatch %}
</div>
Expand Down
Loading