diff --git a/src/fluent.rs b/src/fluent.rs index 4b9536c..4825f69 100644 --- a/src/fluent.rs +++ b/src/fluent.rs @@ -9,6 +9,7 @@ pub type Locales = HashMap; pub struct Localizer { locales: Locales, + default_locale: Option, number_options: FluentNumberOptions, } @@ -51,6 +52,7 @@ impl Localizer { Self { locales, + default_locale: None, number_options: FluentNumberOptions::default(), } } @@ -66,6 +68,11 @@ impl Localizer { &self.number_options } + pub fn set_default_locale(mut self, locale: Option) -> Self { + self.default_locale = locale; + self + } + /// Adds a bundle to the localizer including all the FTL files given by their file paths pub fn add_bundle

( &mut self, @@ -130,7 +137,12 @@ impl Localizer { ) -> Option { let bundle = self.get_locale(locale)?; - let message = bundle.get_message(key)?; + let message = bundle.get_message(key).or_else(|| { + self.default_locale + .as_ref() + .and_then(|locale| self.get_locale(locale)) + .and_then(|bundle| bundle.get_message(key)) + })?; let pattern = message.value()?;