Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to add default locale #1

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading