From 31f03928787ec29e72ebb91507b84b0ca68841d6 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Wed, 15 May 2024 18:26:07 +0200 Subject: [PATCH] feat: renamed get_message to try_get_message, added infallible alternative --- src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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 { + pub fn try_get_message(&self, key: &str, language: &str, args: Option<&FluentArgs>) -> Result { 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()) + } }