feat: renamed get_message to try_get_message, added infallible alternative

This commit is contained in:
zaaarf 2024-05-15 18:26:07 +02:00
parent a497b3ad5b
commit 31f0392878
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B

View file

@ -118,7 +118,7 @@ impl Localiser {
}
/// 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)
.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()))?;
@ -135,4 +135,10 @@ impl Localiser {
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())
}
}