Drop custom translation handling

xkb layouts are translated fine by libgnome-desktop. If not
we should go fixing there since that makes keyboard layout
strings consistent with e.g. gnome-control-center.

There weren't any 'exotic' strings in the current translations
so what we get from libgnome-desktop should be just fine.
This commit is contained in:
Guido Günther
2021-12-08 14:18:17 +01:00
parent 72a7825c85
commit 10ea276052
3 changed files with 11 additions and 125 deletions

View File

@ -88,15 +88,6 @@ impl Drop for XkbInfo {
} }
} }
#[derive(Clone, Debug, PartialEq)]
pub struct Translation<'a>(pub &'a str);
impl<'a> Translation<'a> {
pub fn to_owned(&'a self) -> OwnedTranslation {
OwnedTranslation(self.0.to_owned())
}
}
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct OwnedTranslation(pub String); pub struct OwnedTranslation(pub String);

View File

@ -5,9 +5,7 @@ use gtk;
use std::ffi::CString; use std::ffi::CString;
use std::cmp::Ordering; use std::cmp::Ordering;
use ::layout::c::{ Bounds, EekGtkKeyboard }; use ::layout::c::{ Bounds, EekGtkKeyboard };
use ::locale; use ::locale::{ OwnedTranslation, compare_current_locale };
use ::locale::{ OwnedTranslation, Translation, compare_current_locale };
use ::locale_config::system_locale;
use ::logging; use ::logging;
use ::manager; use ::manager;
use ::resources; use ::resources;
@ -209,13 +207,8 @@ fn get_current_layout(
/// Translates all provided layout names according to current locale, /// Translates all provided layout names according to current locale,
/// for the purpose of display (i.e. errors will be caught and reported) /// for the purpose of display (i.e. errors will be caught and reported)
fn translate_layout_names(layouts: &Vec<LayoutId>) -> Vec<OwnedTranslation> { fn translate_layout_names(layouts: &Vec<LayoutId>) -> Vec<OwnedTranslation> {
// This procedure is rather ugly... // `XkbInfo` being temporary means that its return values must be
// Xkb lookup *must not* be applied to non-system layouts, // copied, forcing the use of `OwnedTranslation`.
// so both translators can't be merged into one lookup table,
// therefore must be done in two steps.
// `XkbInfo` being temporary also means
// that its return values must be copied,
// forcing the use of `OwnedTranslation`.
enum Status { enum Status {
/// xkb names should get all translated here /// xkb names should get all translated here
Translated(OwnedTranslation), Translated(OwnedTranslation),
@ -224,7 +217,7 @@ fn translate_layout_names(layouts: &Vec<LayoutId>) -> Vec<OwnedTranslation> {
} }
// Attempt to take all xkb names from gnome-desktop's xkb info. // Attempt to take all xkb names from gnome-desktop's xkb info.
let xkb_translator = locale::XkbInfo::new(); let xkb_translator = ::locale::XkbInfo::new();
let translated_names = layouts.iter() let translated_names = layouts.iter()
.map(|id| match id { .map(|id| match id {
@ -236,44 +229,15 @@ fn translate_layout_names(layouts: &Vec<LayoutId>) -> Vec<OwnedTranslation> {
&format!("No display name for xkb layout {}", name), &format!("No display name for xkb layout {}", name),
).unwrap_or_else(|| Status::Remaining(name.clone())) ).unwrap_or_else(|| Status::Remaining(name.clone()))
}, },
LayoutId::Local(name) => Status::Remaining(name.clone()), LayoutId::Local (_) => unreachable!(),
}); });
// Weird xkb layouts still need to be looked up in the internal database. translated_names
let builtin_translations = system_locale() .map(|status| match status {
.map(|locale| Status::Remaining(name) => OwnedTranslation(name),
locale.tags_for("messages") Status::Translated(t) => t,
.next().unwrap() // guaranteed to exist })
.as_ref() .collect()
.to_owned()
)
.or_print(logging::Problem::Surprise, "No locale detected")
.and_then(|lang| {
resources::get_layout_names(lang.as_str())
});
match builtin_translations {
Some(translations) => {
translated_names
.map(|status| match status {
Status::Remaining(name) => {
translations.get(name.as_str())
.unwrap_or(&Translation(name.as_str()))
.to_owned()
},
Status::Translated(t) => t,
})
.collect()
},
None => {
translated_names
.map(|status| match status {
Status::Remaining(name) => OwnedTranslation(name),
Status::Translated(t) => t,
})
.collect()
},
}
} }
pub fn show( pub fn show(

View File

@ -2,11 +2,6 @@
* This could be done using GResource, but that would need additional work. * This could be done using GResource, but that would need additional work.
*/ */
use std::collections::HashMap;
use ::locale::Translation;
use std::iter::FromIterator;
// TODO: keep a list of what is a language layout, // TODO: keep a list of what is a language layout,
// and what a convenience layout. "_wide" is not a layout, // and what a convenience layout. "_wide" is not a layout,
// neither is "number" // neither is "number"
@ -125,42 +120,6 @@ pub fn get_overlays() -> Vec<&'static str> {
OVERLAY_NAMES.to_vec() OVERLAY_NAMES.to_vec()
} }
/// Translations of the layout identifier strings
static LAYOUT_NAMES: &[(&'static str, &'static str)] = &[
("es-ES", include_str!("../data/langs/es-ES.txt")),
("fur-IT", include_str!("../data/langs/fur-IT.txt")),
("he-IL", include_str!("../data/langs/he-IL.txt")),
("ru-RU", include_str!("../data/langs/ru-RU.txt")),
];
pub fn get_layout_names(lang: &str)
-> Option<HashMap<&'static str, Translation<'static>>>
{
let translations = LAYOUT_NAMES.iter()
.find(|(name, _data)| *name == lang)
.map(|(_name, data)| *data);
translations.map(make_mapping)
}
fn parse_line(line: &str) -> Option<(&str, Translation)> {
let comment = line.trim().starts_with("#");
if comment {
None
} else {
let mut iter = line.splitn(2, " ");
let name = iter.next().unwrap();
// will skip empty and unfinished lines
iter.next().map(|tr| (name, Translation(tr.trim())))
}
}
fn make_mapping(data: &str) -> HashMap<&str, Translation> {
HashMap::from_iter(
data.split("\n")
.filter_map(parse_line)
)
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
@ -171,32 +130,4 @@ mod test {
assert!(get_keyboard(&format!("{}/us", name)).is_some()); assert!(get_keyboard(&format!("{}/us", name)).is_some());
} }
} }
#[test]
fn mapping_line() {
assert_eq!(
Some(("name", Translation("translation"))),
parse_line("name translation")
);
}
#[test]
fn mapping_bad() {
assert_eq!(None, parse_line("bad"));
}
#[test]
fn mapping_empty() {
assert_eq!(None, parse_line(""));
}
#[test]
fn mapping_comment() {
assert_eq!(None, parse_line("# comment"));
}
#[test]
fn mapping_comment_offset() {
assert_eq!(None, parse_line(" # comment"));
}
} }