mirror of
https://github.com/zaaarf/fluent-fluently.git
synced 2024-11-25 16:04:49 +01:00
Compare commits
2 commits
a497b3ad5b
...
066e74ea1d
Author | SHA1 | Date | |
---|---|---|---|
066e74ea1d | |||
31f0392878 |
1 changed files with 16 additions and 6 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.
|
/// 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],
|
/// [FluentResource]s within a same folder will be considered part of a same [FluentBundle],
|
||||||
/// forming a single localisation for all intents and purposes.
|
/// 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 bundles = HashMap::new();
|
||||||
let mut available_languages = 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())
|
.filter_map(|res| res.ok())
|
||||||
.map(|dir_entry| dir_entry.path())
|
.map(|dir_entry| dir_entry.path())
|
||||||
.filter_map(|path| {
|
.filter_map(|path| {
|
||||||
|
@ -58,7 +58,7 @@ impl Localiser {
|
||||||
}).collect::<Vec<_>>();
|
}).collect::<Vec<_>>();
|
||||||
|
|
||||||
// validate default
|
// 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 {
|
for path in paths {
|
||||||
// validate filename as language code
|
// 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.
|
/// 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: impl Into<String>, language: impl Into<String>, args: Option<&FluentArgs>) -> Result<String> {
|
||||||
let bundle = self.bundles.get(language)
|
let key = key.into();
|
||||||
|
let language = language.into();
|
||||||
|
|
||||||
|
let bundle = self.bundles.get(&language)
|
||||||
.or_else(|| self.bundles.get(&self.default_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()))?;
|
.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())
|
.and_then(|msg| msg.value())
|
||||||
.ok_or(error::Error::MissingMessageError(format!("No such message {} for language {}!", key, language)))?;
|
.ok_or(error::Error::MissingMessageError(format!("No such message {} for language {}!", key, language)))?;
|
||||||
|
|
||||||
|
@ -135,4 +138,11 @@ impl Localiser {
|
||||||
Err(error::Error::FluentError(err))
|
Err(error::Error::FluentError(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Similar to [Localiser::try_get_message], but returns the given key on failure.
|
||||||
|
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