use serde_json::{Value, json};
use crate::profiles::BrowserProfile;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChromeLanguageConfig {
application_locale: String,
selected_languages: Vec<String>,
}
impl ChromeLanguageConfig {
pub fn from_profile(profile: &BrowserProfile) -> Self {
Self {
application_locale: profile.locale().locale.clone(),
selected_languages: profile.navigator().languages.clone(),
}
}
pub fn chrome_argument(&self) -> (&'static str, &str) {
("lang", &self.application_locale)
}
pub fn application_locale(&self) -> &str {
&self.application_locale
}
pub fn selected_languages(&self) -> &[String] {
&self.selected_languages
}
pub fn preference_patch(&self) -> Value {
json!({
"intl": {
"app_locale": self.application_locale,
"selected_languages": self.selected_languages.join(",")
}
})
}
pub fn cdp_accept_language(&self) -> String {
self.selected_languages.join(",")
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::PlatformProfile;
#[test]
fn derives_native_language_values_without_setting_derived_pref() {
let config = ChromeLanguageConfig::from_profile(&PlatformProfile::Linux.profile());
assert_eq!(config.chrome_argument(), ("lang", "en-US"));
assert_eq!(config.cdp_accept_language(), "en-US,en");
assert_eq!(
config.preference_patch()["intl"]["selected_languages"],
"en-US,en"
);
assert!(config.preference_patch()["intl"]["accept_languages"].is_null());
}
}