Pulled in some Unix-specific code from https://github.com/rust-locale/locale_config to reduce dependencies. First reason to reduce dependencies: gettext-rs is not in Debian. Copying gettext-sys might have made sense, but the interface is somewhat confusing. For translating a couple identifiers, detection and some hand-rolled hash map is all that is needed, and the option to move to gettext later remains. locale_config has been stripped of the lazy_static dependency, which, messing with the strtup sequence, might be a source of debugging woes. Plus setting language once in the beginning is somewhat inflexible regarding runtime changes.
40 lines
841 B
Rust
40 lines
841 B
Rust
/*! Locale-specific functions */
|
|
|
|
use std::cmp;
|
|
use std::ffi::CString;
|
|
|
|
mod c {
|
|
use std::os::raw::c_char;
|
|
|
|
#[allow(non_camel_case_types)]
|
|
pub type c_int = i32;
|
|
|
|
#[no_mangle]
|
|
extern "C" {
|
|
// from libc
|
|
pub fn strcoll(cs: *const c_char, ct: *const c_char) -> c_int;
|
|
}
|
|
}
|
|
|
|
fn cstring_safe(s: &str) -> CString {
|
|
CString::new(s)
|
|
.unwrap_or(CString::new("").unwrap())
|
|
}
|
|
|
|
pub fn compare_current_locale(a: &str, b: &str) -> cmp::Ordering {
|
|
let a = cstring_safe(a);
|
|
let b = cstring_safe(b);
|
|
let a = a.as_ptr();
|
|
let b = b.as_ptr();
|
|
let result = unsafe { c::strcoll(a, b) };
|
|
if result == 0 {
|
|
cmp::Ordering::Equal
|
|
} else if result > 0 {
|
|
cmp::Ordering::Greater
|
|
} else if result < 0 {
|
|
cmp::Ordering::Less
|
|
} else {
|
|
unreachable!()
|
|
}
|
|
}
|