modo-rs 0.11.0

Rust web framework for small monolithic apps
Documentation
use serde::Deserialize;

/// Configuration for the i18n module.
///
/// All fields have sensible defaults and can be overridden via YAML config.
/// Paths are relative to the working directory of the running process.
///
/// # Defaults
///
/// | Field                | Default     |
/// |----------------------|-------------|
/// | `locales_path`       | `"locales"` |
/// | `default_locale`     | `"en"`      |
/// | `locale_cookie`      | `"lang"`    |
/// | `locale_query_param` | `"lang"`    |
#[non_exhaustive]
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct I18nConfig {
    /// Directory that contains locale subdirectories with YAML translation files.
    pub locales_path: String,
    /// BCP 47 language tag used when no locale can be resolved from the request.
    pub default_locale: String,
    /// Cookie name read by [`CookieResolver`](super::CookieResolver) to determine the active locale.
    pub locale_cookie: String,
    /// Query-string parameter name read by [`QueryParamResolver`](super::QueryParamResolver).
    pub locale_query_param: String,
}

impl Default for I18nConfig {
    fn default() -> Self {
        Self {
            locales_path: "locales".into(),
            default_locale: "en".into(),
            locale_cookie: "lang".into(),
            locale_query_param: "lang".into(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn default_config_has_sensible_values() {
        let config = I18nConfig::default();
        assert_eq!(config.locales_path, "locales");
        assert_eq!(config.default_locale, "en");
        assert_eq!(config.locale_cookie, "lang");
        assert_eq!(config.locale_query_param, "lang");
    }

    #[test]
    fn config_deserializes_from_yaml() {
        let yaml = r#"
            locales_path: "i18n"
            default_locale: "uk"
            locale_cookie: "locale"
            locale_query_param: "locale"
        "#;
        let config: I18nConfig = serde_yaml_ng::from_str(yaml).unwrap();
        assert_eq!(config.locales_path, "i18n");
        assert_eq!(config.default_locale, "uk");
        assert_eq!(config.locale_cookie, "locale");
        assert_eq!(config.locale_query_param, "locale");
    }

    #[test]
    fn config_uses_defaults_for_missing_fields() {
        let yaml = r#"
            default_locale: "uk"
        "#;
        let config: I18nConfig = serde_yaml_ng::from_str(yaml).unwrap();
        assert_eq!(config.default_locale, "uk");
        assert_eq!(config.locales_path, "locales");
        assert_eq!(config.locale_cookie, "lang");
        assert_eq!(config.locale_query_param, "lang");
    }
}