diff --git a/src/lib.rs b/src/lib.rs index 34d390c..074fc9d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,10 +43,10 @@ impl Localiser { /// in the second case the same will be done to their chilren instead. /// [FluentResource]s within a same folder will be considered part of a same [FluentBundle], /// forming a single localisation for all intents and purposes. - pub fn try_load(path: &str, default_language: &str) -> Result { + pub fn try_load(path: impl Into, default_language: impl Into) -> Result { let mut bundles = HashMap::new(); let mut available_languages = HashMap::new(); - let paths = std::fs::read_dir(path)? + let paths = std::fs::read_dir(path.into())? .filter_map(|res| res.ok()) .map(|dir_entry| dir_entry.path()) .filter_map(|path| { @@ -58,7 +58,7 @@ impl Localiser { }).collect::>(); // validate default - let default_language = default_language.parse::()?.to_string(); + let default_language = default_language.into().parse::()?.to_string(); for path in paths { // validate filename as language code @@ -118,12 +118,15 @@ impl Localiser { } /// Extracts a message from the requested bundle, or from the default one if absent. - pub fn try_get_message(&self, key: &str, language: &str, args: Option<&FluentArgs>) -> Result { - let bundle = self.bundles.get(language) + pub fn try_get_message(&self, key: impl Into, language: impl Into, args: Option<&FluentArgs>) -> Result { + let key = key.into(); + let language = language.into(); + + 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()))?; - let pattern = bundle.get_message(key) + let pattern = bundle.get_message(&key) .and_then(|msg| msg.value()) .ok_or(error::Error::MissingMessageError(format!("No such message {} for language {}!", key, language)))?; @@ -137,8 +140,9 @@ impl Localiser { } /// 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()) + pub fn get_message(&self, key: impl Into, language: impl Into, args: Option<&FluentArgs>) -> String { + let key = key.into(); + self.try_get_message(&key, language, args) + .unwrap_or(key) } }