i18n: disable bidi for tests
> This is important for cases such as when a right-to-left user name is presented in the left-to-right message. > In some cases, such as testing, the user may want to disable the isolating. See: https://docs.rs/fluent/latest/fluent/bundle/struct.FluentBundle.html#method.set_use_isolating Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
@@ -62,6 +62,8 @@ pub struct Localization {
|
|||||||
normalized_key_cache: HashMap<String, IntlKeyBuf>,
|
normalized_key_cache: HashMap<String, IntlKeyBuf>,
|
||||||
/// Bundles
|
/// Bundles
|
||||||
bundles: HashMap<LanguageIdentifier, Bundle>,
|
bundles: HashMap<LanguageIdentifier, Bundle>,
|
||||||
|
|
||||||
|
use_isolating: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Localization {
|
impl Default for Localization {
|
||||||
@@ -84,6 +86,7 @@ impl Default for Localization {
|
|||||||
current_locale: default_locale.to_owned(),
|
current_locale: default_locale.to_owned(),
|
||||||
available_locales,
|
available_locales,
|
||||||
fallback_locale,
|
fallback_locale,
|
||||||
|
use_isolating: true,
|
||||||
normalized_key_cache: HashMap::new(),
|
normalized_key_cache: HashMap::new(),
|
||||||
string_cache: HashMap::new(),
|
string_cache: HashMap::new(),
|
||||||
bundles: HashMap::new(),
|
bundles: HashMap::new(),
|
||||||
@@ -97,6 +100,14 @@ impl Localization {
|
|||||||
Localization::default()
|
Localization::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Disable bidirectional isolation markers. mostly useful for tests
|
||||||
|
pub fn no_bidi() -> Self {
|
||||||
|
Localization {
|
||||||
|
use_isolating: false,
|
||||||
|
..Localization::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Gets a localized string by its ID
|
/// Gets a localized string by its ID
|
||||||
pub fn get_string(&mut self, id: IntlKey<'_>) -> Result<String, IntlError> {
|
pub fn get_string(&mut self, id: IntlKey<'_>) -> Result<String, IntlError> {
|
||||||
self.get_cached_string(id, None)
|
self.get_cached_string(id, None)
|
||||||
@@ -152,8 +163,11 @@ impl Localization {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn try_load_bundle(&mut self, lang: &LanguageIdentifier) -> Result<(), IntlError> {
|
fn try_load_bundle(&mut self, lang: &LanguageIdentifier) -> Result<(), IntlError> {
|
||||||
self.bundles
|
let mut bundle = Self::load_bundle(lang)?;
|
||||||
.insert(lang.to_owned(), Self::load_bundle(lang)?);
|
if !self.use_isolating {
|
||||||
|
bundle.set_use_isolating(false);
|
||||||
|
}
|
||||||
|
self.bundles.insert(lang.to_owned(), bundle);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -228,6 +242,7 @@ impl Localization {
|
|||||||
);
|
);
|
||||||
self.try_load_bundle(&locale)
|
self.try_load_bundle(&locale)
|
||||||
.expect("failed to load fallback bundle!?");
|
.expect("failed to load fallback bundle!?");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_now_condition() {
|
fn test_now_condition() {
|
||||||
let now = get_current_timestamp();
|
let now = get_current_timestamp();
|
||||||
let mut intl = Localization::default();
|
let mut intl = Localization::no_bidi();
|
||||||
|
|
||||||
// Test 0 seconds ago
|
// Test 0 seconds ago
|
||||||
let result = time_ago_between(&mut intl, now, now);
|
let result = time_ago_between(&mut intl, now, now);
|
||||||
@@ -137,7 +137,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_seconds_condition() {
|
fn test_seconds_condition() {
|
||||||
let now = get_current_timestamp();
|
let now = get_current_timestamp();
|
||||||
let mut i18n = Localization::default();
|
let mut i18n = Localization::no_bidi();
|
||||||
|
|
||||||
// Test 3 seconds ago
|
// Test 3 seconds ago
|
||||||
let result = time_ago_between(&mut i18n, now - 3, now);
|
let result = time_ago_between(&mut i18n, now - 3, now);
|
||||||
@@ -163,7 +163,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_minutes_condition() {
|
fn test_minutes_condition() {
|
||||||
let now = get_current_timestamp();
|
let now = get_current_timestamp();
|
||||||
let mut i18n = Localization::default();
|
let mut i18n = Localization::no_bidi();
|
||||||
|
|
||||||
// Test 1 minute ago
|
// Test 1 minute ago
|
||||||
let result = time_ago_between(&mut i18n, now - ONE_MINUTE_IN_SECONDS, now);
|
let result = time_ago_between(&mut i18n, now - ONE_MINUTE_IN_SECONDS, now);
|
||||||
@@ -189,7 +189,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_hours_condition() {
|
fn test_hours_condition() {
|
||||||
let now = get_current_timestamp();
|
let now = get_current_timestamp();
|
||||||
let mut i18n = Localization::default();
|
let mut i18n = Localization::no_bidi();
|
||||||
|
|
||||||
// Test 1 hour ago
|
// Test 1 hour ago
|
||||||
let result = time_ago_between(&mut i18n, now - ONE_HOUR_IN_SECONDS, now);
|
let result = time_ago_between(&mut i18n, now - ONE_HOUR_IN_SECONDS, now);
|
||||||
@@ -215,7 +215,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_days_condition() {
|
fn test_days_condition() {
|
||||||
let now = get_current_timestamp();
|
let now = get_current_timestamp();
|
||||||
let mut i18n = Localization::default();
|
let mut i18n = Localization::no_bidi();
|
||||||
|
|
||||||
// Test 1 day ago
|
// Test 1 day ago
|
||||||
let result = time_ago_between(&mut i18n, now - ONE_DAY_IN_SECONDS, now);
|
let result = time_ago_between(&mut i18n, now - ONE_DAY_IN_SECONDS, now);
|
||||||
@@ -233,7 +233,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_weeks_condition() {
|
fn test_weeks_condition() {
|
||||||
let now = get_current_timestamp();
|
let now = get_current_timestamp();
|
||||||
let mut i18n = Localization::default();
|
let mut i18n = Localization::no_bidi();
|
||||||
|
|
||||||
// Test 1 week ago
|
// Test 1 week ago
|
||||||
let result = time_ago_between(&mut i18n, now - ONE_WEEK_IN_SECONDS, now);
|
let result = time_ago_between(&mut i18n, now - ONE_WEEK_IN_SECONDS, now);
|
||||||
@@ -247,7 +247,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_months_condition() {
|
fn test_months_condition() {
|
||||||
let now = get_current_timestamp();
|
let now = get_current_timestamp();
|
||||||
let mut i18n = Localization::default();
|
let mut i18n = Localization::no_bidi();
|
||||||
|
|
||||||
// Test 1 month ago
|
// Test 1 month ago
|
||||||
let result = time_ago_between(&mut i18n, now - ONE_MONTH_IN_SECONDS, now);
|
let result = time_ago_between(&mut i18n, now - ONE_MONTH_IN_SECONDS, now);
|
||||||
@@ -265,7 +265,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_years_condition() {
|
fn test_years_condition() {
|
||||||
let now = get_current_timestamp();
|
let now = get_current_timestamp();
|
||||||
let mut i18n = Localization::default();
|
let mut i18n = Localization::no_bidi();
|
||||||
|
|
||||||
// Test 1 year ago
|
// Test 1 year ago
|
||||||
let result = time_ago_between(&mut i18n, now - ONE_YEAR_IN_SECONDS, now);
|
let result = time_ago_between(&mut i18n, now - ONE_YEAR_IN_SECONDS, now);
|
||||||
@@ -287,7 +287,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_future_timestamps() {
|
fn test_future_timestamps() {
|
||||||
let now = get_current_timestamp();
|
let now = get_current_timestamp();
|
||||||
let mut i18n = Localization::default();
|
let mut i18n = Localization::no_bidi();
|
||||||
|
|
||||||
// Test 1 minute in the future
|
// Test 1 minute in the future
|
||||||
let result = time_ago_between(&mut i18n, now + ONE_MINUTE_IN_SECONDS, now);
|
let result = time_ago_between(&mut i18n, now + ONE_MINUTE_IN_SECONDS, now);
|
||||||
@@ -317,7 +317,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_boundary_conditions() {
|
fn test_boundary_conditions() {
|
||||||
let now = get_current_timestamp();
|
let now = get_current_timestamp();
|
||||||
let mut i18n = Localization::default();
|
let mut i18n = Localization::no_bidi();
|
||||||
|
|
||||||
// Test boundary between seconds and minutes
|
// Test boundary between seconds and minutes
|
||||||
let result = time_ago_between(&mut i18n, now - 60, now);
|
let result = time_ago_between(&mut i18n, now - 60, now);
|
||||||
|
|||||||
Reference in New Issue
Block a user