#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Direction {
#[default]
Ltr,
Rtl,
}
impl Direction {
pub fn is_rtl(self) -> bool {
matches!(self, Direction::Rtl)
}
pub fn for_locale(locale: &str) -> Self {
let lang = locale
.split(['-', '_'])
.next()
.unwrap_or(locale)
.to_ascii_lowercase();
const RTL: &[&str] = &[
"ar", "arc", "ckb", "dv", "fa", "ha", "he", "khw", "ks", "ps", "sd", "ur", "uz", "yi",
];
if RTL.contains(&lang.as_str()) {
Direction::Rtl
} else {
Direction::Ltr
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rtl_languages_are_recognised_with_and_without_a_region() {
for tag in ["ar", "ar-EG", "he_IL", "fa", "ur-PK", "HE"] {
assert_eq!(Direction::for_locale(tag), Direction::Rtl, "{tag}");
}
}
#[test]
fn everything_else_is_left_to_right() {
for tag in ["en", "es-AR", "ja", "", "zz"] {
assert_eq!(Direction::for_locale(tag), Direction::Ltr, "{tag}");
}
}
}