mirror of
https://github.com/zaaarf/fluent-fluently.git
synced 2024-12-22 06:44:53 +01:00
feat: made more generic
This commit is contained in:
parent
31f0392878
commit
066e74ea1d
1 changed files with 13 additions and 9 deletions
22
src/lib.rs
22
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<Self> {
|
||||
pub fn try_load(path: impl Into<String>, default_language: impl Into<String>) -> Result<Self> {
|
||||
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::<Vec<_>>();
|
||||
|
||||
// validate default
|
||||
let default_language = default_language.parse::<LanguageIdentifier>()?.to_string();
|
||||
let default_language = default_language.into().parse::<LanguageIdentifier>()?.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<String> {
|
||||
let bundle = self.bundles.get(language)
|
||||
pub fn try_get_message(&self, key: impl Into<String>, language: impl Into<String>, args: Option<&FluentArgs>) -> Result<String> {
|
||||
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<String>, language: impl Into<String>, args: Option<&FluentArgs>) -> String {
|
||||
let key = key.into();
|
||||
self.try_get_message(&key, language, args)
|
||||
.unwrap_or(key)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue