Bug 1895922 - Fix locale detection in the crash reporter r=cmartin

Differential Revision: https://phabricator.services.mozilla.com/D209937
This commit is contained in:
Alex Franchuk 2024-05-09 16:09:02 +00:00
parent 2f36d75c2a
commit 4e741aa8df

View file

@ -21,18 +21,57 @@ pub fn read() -> anyhow::Result<LanguageInfo> {
path.push("omni.ja"); path.push("omni.ja");
let mut zip = read_omnijar_file(&path)?; let mut zip = read_omnijar_file(&path)?;
let locale = { let locales = {
let buf = BufReader::new( let buf = BufReader::new(
zip.by_name("res/multilocale.txt") zip.by_name("res/multilocale.txt")
.context("failed to read multilocale file in zip archive")?, .context("failed to read multilocale file in zip archive")?,
); );
buf.lines() let line = buf
.lines()
.next() .next()
.ok_or(anyhow::anyhow!("multilocale file was empty"))? .ok_or(anyhow::anyhow!("multilocale file was empty"))?
.context("failed to read first line of multilocale file")? .context("failed to read first line of multilocale file")?;
line.split(",")
.map(|s| s.trim().to_owned())
.collect::<Vec<_>>()
}; };
let mut file = zip let (locale, ftl_definitions) = 'defs: {
for locale in &locales {
match read_strings(locale, &mut zip) {
Ok(v) => break 'defs (locale.to_string(), v),
Err(e) => log::warn!("{e:#}"),
}
}
anyhow::bail!("failed to find any usable localized strings in the omnijar")
};
// The brand ftl is in the browser omnijar.
path.pop();
path.push("browser");
path.push("omni.ja");
let ftl_branding = 'branding: {
for locale in &locales {
match read_branding(&locale, &mut zip) {
Ok(v) => break 'branding v,
Err(e) => log::warn!("failed to read branding from omnijar: {e:#}"),
}
}
log::info!("using fallback branding info");
LanguageInfo::default().ftl_branding
};
Ok(LanguageInfo {
identifier: locale,
ftl_definitions,
ftl_branding,
})
}
/// Read the localized strings from the given zip archive (omnijar).
fn read_strings(locale: &str, archive: &mut ZipArchive<File>) -> anyhow::Result<String> {
let mut file = archive
.by_name(&format!( .by_name(&format!(
"localization/{locale}/crashreporter/crashreporter.ftl" "localization/{locale}/crashreporter/crashreporter.ftl"
)) ))
@ -42,34 +81,18 @@ pub fn read() -> anyhow::Result<LanguageInfo> {
file.read_to_string(&mut ftl_definitions) file.read_to_string(&mut ftl_definitions)
.with_context(|| format!("failed to read localization file for {locale}"))?; .with_context(|| format!("failed to read localization file for {locale}"))?;
// The brand ftl is in the browser omnijar. Ok(ftl_definitions)
path.pop(); }
path.push("browser");
path.push("omni.ja");
let ftl_branding = read_omnijar_file(&path) /// Read the branding information from the given zip archive (omnijar).
.and_then(|mut zip| { fn read_branding(locale: &str, archive: &mut ZipArchive<File>) -> anyhow::Result<String> {
let mut file = zip let mut file = archive
.by_name(&format!("localization/{locale}/branding/brand.ftl")) .by_name(&format!("localization/{locale}/branding/brand.ftl"))
.with_context(|| { .with_context(|| format!("failed to locate branding localization file for {locale}"))?;
format!("failed to locate branding localization file for {locale}") let mut s = String::new();
})?; file.read_to_string(&mut s)
let mut s = String::new(); .with_context(|| format!("failed to read branding localization file for {locale}"))?;
file.read_to_string(&mut s) Ok(s)
.with_context(|| format!("failed to read localization file for {locale}"))?;
Ok(s)
})
.unwrap_or_else(|e| {
log::warn!("failed to read browser omnijar: {e}");
log::info!("using fallback branding info");
LanguageInfo::default().ftl_branding
});
Ok(LanguageInfo {
identifier: locale,
ftl_definitions,
ftl_branding,
})
} }
fn read_omnijar_file(path: &Path) -> anyhow::Result<ZipArchive<File>> { fn read_omnijar_file(path: &Path) -> anyhow::Result<ZipArchive<File>> {