mirror of
https://github.com/zaaarf/fluent-fluently.git
synced 2024-11-13 02:39:20 +01:00
feat: renamed get_message to try_get_message, added infallible alternative
This commit is contained in:
parent
a497b3ad5b
commit
31f0392878
1 changed files with 7 additions and 1 deletions
|
@ -118,7 +118,7 @@ impl Localiser {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extracts a message from the requested bundle, or from the default one if absent.
|
/// Extracts a message from the requested bundle, or from the default one if absent.
|
||||||
pub fn get_message(&self, key: &str, language: &str, args: Option<&FluentArgs>) -> Result<String> {
|
pub fn try_get_message(&self, key: &str, language: &str, args: Option<&FluentArgs>) -> Result<String> {
|
||||||
let bundle = self.bundles.get(language)
|
let bundle = self.bundles.get(language)
|
||||||
.or_else(|| self.bundles.get(&self.default_language))
|
.or_else(|| self.bundles.get(&self.default_language))
|
||||||
.ok_or(error::Error::GenericError("Failed to get default bundle! This is not supposed to happen!".to_string()))?;
|
.ok_or(error::Error::GenericError("Failed to get default bundle! This is not supposed to happen!".to_string()))?;
|
||||||
|
@ -135,4 +135,10 @@ impl Localiser {
|
||||||
Err(error::Error::FluentError(err))
|
Err(error::Error::FluentError(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Similar to [Localiser::try_get_message], but returns the given key on failure.
|
||||||
|
pub fn get_message(&self, key: &str, language: &str, args: Option<&FluentArgs>) -> String {
|
||||||
|
self.try_get_message(key, language, args)
|
||||||
|
.unwrap_or(key.to_string())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue