//! A language is a data file: `languages/.json` beside this module //! holds its spoken punctuation, its sentence ends, whether it is written //! with spaces, and the words it always writes in capitals. Adding a //! language is adding its file and naming it in [`FILES`]; no rule changes. //! //! A spoken entry: `say` (every way of saying it, any script: "కామా" and //! "comma" both in Telugu's file, since Telugu speakers say either), `mark` //! (what is written), `join` (`left`: glued to the word before, a space //! after; `right`: a space before, glued to the word after; `space`: spaces //! both sides; `none`: glued both sides; `break`: a line break, the next //! word starts a sentence), and `whole_word` (in an unspaced script, only //! when the engine gave it as a word of its own: "まる" inside "まるで" is //! not a full stop). `apple_speech` says Apple's on-device recogniser hears //! the language well (an iPhone's live pass); `command_lead` lists the words //! a spoken command starts with that are not what to open ("open", "show me"). use std::sync::OnceLock; use serde::Deserialize; /// Every language file, `(code, json)`. pub const FILES: &[(&str, &str)] = &[ ("en", include_str!("languages/en.json")), ("hi", include_str!("languages/hi.json")), ("ja", include_str!("languages/ja.json")), ("te", include_str!("languages/te.json")), ]; #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)] #[serde(rename_all = "lowercase")] pub enum Join { Left, Right, Space, None, Break, } #[derive(Debug, Clone, Deserialize)] #[serde(deny_unknown_fields)] pub struct Spoken { pub say: Vec, pub mark: String, pub join: Join, #[serde(default)] pub whole_word: bool, } #[derive(Debug, Clone, Deserialize)] #[serde(deny_unknown_fields)] pub struct Language { pub language: String, pub name: String, pub spaced: bool, /// Apple's on-device recogniser hears it well: an iPhone's live pass. #[serde(default)] pub apple_speech: bool, pub sentence_end: Vec, #[serde(default)] pub always_upper: Vec, /// Common words that are also names ("will", "may"): a name in the /// vocabulary never recases them. #[serde(default)] pub never_recase: Vec, pub spoken: Vec, /// Words a command starts with that are not what to open ("open", /// "show me"): left out of the search, longest first. #[serde(default)] pub command_lead: Vec, } /// Every language, parsed once. A file that does not parse is a build's /// mistake, caught by this module's tests; at run time it is left out. pub fn all() -> &'static [Language] { static ALL: OnceLock> = OnceLock::new(); ALL.get_or_init(|| FILES.iter().filter_map(|(_, json)| serde_json::from_str(json).ok()).collect()) } /// The language for a BCP 47 code: `te-Latn`, `te_IN` and `TE` read `te`. pub fn find(code: &str) -> Option<&'static Language> { let base = code.split(['-', '_']).next().unwrap_or_default().to_ascii_lowercase(); all().iter().find(|language| language.language == base) } /// The codes with a file, in file order. pub fn known() -> Vec<&'static str> { all().iter().map(|language| language.language.as_str()).collect() }