mirror of
https://github.com/zaaarf/fluent-fluently.git
synced 2024-11-22 21:15:00 +01:00
Compare commits
No commits in common. "066e74ea1d111086e2e2fcf3269791f08186e2e2" and "a497b3ad5b271695b8ea2fb1eab121b7bacddc53" have entirely different histories.
066e74ea1d
...
a497b3ad5b
1 changed files with 6 additions and 16 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: impl Into<String>, default_language: impl Into<String>) -> Result<Self> {
|
pub fn try_load(path: &str, default_language: &str) -> 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.into())?
|
let paths = std::fs::read_dir(path)?
|
||||||
.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.into().parse::<LanguageIdentifier>()?.to_string();
|
let default_language = default_language.parse::<LanguageIdentifier>()?.to_string();
|
||||||
|
|
||||||
for path in paths {
|
for path in paths {
|
||||||
// validate filename as language code
|
// validate filename as language code
|
||||||
|
@ -118,15 +118,12 @@ 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 try_get_message(&self, key: impl Into<String>, language: impl Into<String>, args: Option<&FluentArgs>) -> Result<String> {
|
pub fn get_message(&self, key: &str, language: &str, args: Option<&FluentArgs>) -> Result<String> {
|
||||||
let key = key.into();
|
let bundle = self.bundles.get(language)
|
||||||
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)))?;
|
||||||
|
|
||||||
|
@ -138,11 +135,4 @@ 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