fluent_i18n/
error.rs

1//! Error handling.
2
3use unic_langid::LanguageIdentifierError;
4
5use crate::t;
6
7/// The error that may occur while using the crate.
8#[derive(Debug, thiserror::Error)]
9pub enum Error {
10    /// An error occurred while parsing a locale.
11    #[error("{msg}\n{source}", msg = t!("error-locale-parse", { "locale" => locale }))]
12    LocaleParseError {
13        /// The locale string that could not be parsed.
14        locale: String,
15        /// The source error.
16        source: LanguageIdentifierError,
17    },
18}
19
20#[cfg(test)]
21mod tests {
22    use rstest::*;
23
24    use super::*;
25
26    /// Make sure that the message of the error is properly translated
27    #[rstest]
28    #[case("en-US", r#"Could not parse locale "en-US""#)]
29    #[case("fr-FR", r#"Impossible d’analyser la langue « fr-FR »"#)]
30    #[case("de-DE", r#"Konnte die Spracheinstellung "de-DE" nicht analysieren"#)]
31    #[case("tr-TR", r#""tr-TR" yerel ayarı tanınmadı"#)]
32    #[case("ja-JP", r#"ロケール "ja-JP" を解析できませんでした"#)]
33    #[case("ar-SA", r#"تعذر تحليل اللغة "ar-SA""#)]
34    fn test_error_message_localization(
35        #[case] locale: &str,
36        #[case] expected: &str,
37    ) -> testresult::TestResult {
38        // Set the locale for the test
39        crate::locale::set_locale(Some(locale))?;
40
41        // Construct the error
42        let err = Error::LocaleParseError {
43            locale: locale.to_string(),
44            source: LanguageIdentifierError::Unknown,
45        };
46        let msg = err.to_string();
47
48        // Check translation was applied
49        assert_eq!(
50            msg.lines().next(),
51            Some(expected),
52            "Error message did not match expected translation"
53        );
54
55        Ok(())
56    }
57}