use std::env;
use log::debug;
use super::{
Arguments, DiagnosticMessageSet, Localizer, MessageKey, resolve_localizer, resolve_message_set,
};
#[must_use]
pub fn get_localizer_for_lint(lint_name: &str, configuration_locale: Option<&str>) -> Localizer {
let environment_locale =
env::var_os("DYLINT_LOCALE").and_then(|value| value.into_string().ok());
let selection = resolve_localizer(None, environment_locale, configuration_locale);
selection.log_outcome(lint_name);
selection.into_localizer()
}
#[must_use]
pub fn branch_phrase(locale: &str, branches: usize) -> String {
match locale
.split_once('-')
.map(|(lang, _)| lang)
.unwrap_or(locale)
{
"cy" => welsh_branch_phrase(branches),
"gd" => gaelic_branch_phrase(branches),
_ => english_branch_phrase(branches),
}
}
fn english_branch_phrase(branches: usize) -> String {
match branches {
1 => String::from("1 branch"),
_ => format!("{branches} branches"),
}
}
fn gaelic_branch_phrase(branches: usize) -> String {
match branches {
1 => String::from("1 meur"),
_ => format!("{branches} meuran"),
}
}
fn welsh_branch_phrase(branches: usize) -> String {
match branches {
0 => String::from("dim canghennau"),
1 => String::from("un gangen"),
2 => String::from("dwy gangen"),
3 => String::from("tair cangen"),
6 => String::from("chwe changen"),
4 | 5 => format!("{branches} cangen"),
_ => format!("{branches} canghennau"),
}
}
pub fn noop_reporter(_message: String) {}
#[must_use]
pub fn safe_resolve_message_set(
localizer: &Localizer,
resolution: MessageResolution<'_>,
report_bug: impl FnOnce(String),
fallback: impl FnOnce() -> DiagnosticMessageSet,
) -> DiagnosticMessageSet {
match resolve_message_set(localizer, resolution.key, resolution.args) {
Ok(messages) => messages.strip_isolating_marks(),
Err(error) => {
debug!(
target: resolution.lint_name,
"localization error for key `{}` in locale `{}`: {error}; using fallback",
resolution.key,
localizer.locale(),
);
report_bug(format!(
"Localization error for `{}` key `{}` in locale `{}`: {error}",
resolution.lint_name,
resolution.key,
localizer.locale(),
));
fallback().strip_isolating_marks()
}
}
}
#[derive(Clone, Copy)]
pub struct MessageResolution<'a> {
pub lint_name: &'a str,
pub key: MessageKey<'a>,
pub args: &'a Arguments<'a>,
}
#[cfg(test)]
mod tests {
use super::branch_phrase;
use crate::i18n::FALLBACK_LOCALE;
#[test]
fn renders_english_branch_phrase() {
assert_eq!(branch_phrase(FALLBACK_LOCALE, 0), "0 branches");
assert_eq!(branch_phrase(FALLBACK_LOCALE, 1), "1 branch");
assert_eq!(branch_phrase(FALLBACK_LOCALE, 4), "4 branches");
}
#[test]
fn renders_gaelic_branch_phrase() {
assert_eq!(branch_phrase("gd", 1), "1 meur");
assert_eq!(branch_phrase("gd", 2), "2 meuran");
assert_eq!(branch_phrase("gd", 3), "3 meuran");
}
#[test]
fn renders_welsh_branch_phrase() {
assert_eq!(branch_phrase("cy", 0), "dim canghennau");
assert_eq!(branch_phrase("cy", 1), "un gangen");
assert_eq!(branch_phrase("cy", 2), "dwy gangen");
assert_eq!(branch_phrase("cy", 3), "tair cangen");
assert_eq!(branch_phrase("cy", 6), "chwe changen");
assert_eq!(branch_phrase("cy", 11), "11 canghennau");
}
}