use std::ffi::OsStr;
use std::path::Path;
const LC_MESSAGES: &str = "LC_MESSAGES";
pub fn locale_id(path: &Path) -> String {
let components: Vec<&OsStr> = path.components().map(|c| c.as_os_str()).collect();
for (index, component) in components.iter().enumerate() {
if *component == OsStr::new(LC_MESSAGES) && index > 0 {
return components[index - 1].to_string_lossy().into_owned();
}
}
path.to_string_lossy().into_owned()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn standard_layout_yields_locale() {
assert_eq!(
locale_id(Path::new("locales/ru/LC_MESSAGES/messages.po")),
"ru"
);
assert_eq!(
locale_id(Path::new("/app/i18n/pt_BR/LC_MESSAGES/django.po")),
"pt_BR"
);
}
#[test]
fn non_standard_layout_falls_back_to_path() {
let path = "weird/place/foo.po";
assert_eq!(locale_id(Path::new(path)), path);
}
}