summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author zaaarf <me@zaaarf.foo>2024-05-15 18:26:07 +0200
committer zaaarf <me@zaaarf.foo>2024-05-15 18:26:07 +0200
commit31f03928787ec29e72ebb91507b84b0ca68841d6 (patch)
tree10aa94d03d8d9ca223dd73297a8e13af85b3423a
parenta497b3ad5b271695b8ea2fb1eab121b7bacddc53 (diff)
feat: renamed get_message to try_get_message, added infallible alternative0.2.0
-rw-r--r--src/lib.rs8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 978dbbe..34d390c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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())
+ }
}