Skip to content

Commit

Permalink
feat: add option to add default locale
Browse files Browse the repository at this point in the history
  • Loading branch information
jreppnow committed May 22, 2024
1 parent f179928 commit cb173a0
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/fluent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub type Locales = HashMap<LanguageIdentifier, Bundle>;

pub struct Localizer {
locales: Locales,
default_locale: Option<LanguageIdentifier>,
number_options: FluentNumberOptions,
}

Expand Down Expand Up @@ -51,6 +52,7 @@ impl Localizer {

Self {
locales,
default_locale: None,
number_options: FluentNumberOptions::default(),
}
}
Expand All @@ -66,6 +68,11 @@ impl Localizer {
&self.number_options
}

pub fn set_default_locale(mut self, locale: Option<LanguageIdentifier>) -> 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<P>(
&mut self,
Expand Down Expand Up @@ -130,7 +137,12 @@ impl Localizer {
) -> Option<String> {
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()?;

Expand Down

0 comments on commit cb173a0

Please sign in to comment.