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");
let mut zip = read_omnijar_file(&path)?;
let locale = {
let locales = {
let buf = BufReader::new(
zip.by_name("res/multilocale.txt")
.context("failed to read multilocale file in zip archive")?,
);
buf.lines()
let line = buf
.lines()
.next()
.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!(
"localization/{locale}/crashreporter/crashreporter.ftl"
))
@ -42,34 +81,18 @@ pub fn read() -> anyhow::Result<LanguageInfo> {
file.read_to_string(&mut ftl_definitions)
.with_context(|| format!("failed to read localization file for {locale}"))?;
// The brand ftl is in the browser omnijar.
path.pop();
path.push("browser");
path.push("omni.ja");
Ok(ftl_definitions)
}
let ftl_branding = read_omnijar_file(&path)
.and_then(|mut zip| {
let mut file = zip
.by_name(&format!("localization/{locale}/branding/brand.ftl"))
.with_context(|| {
format!("failed to locate branding localization file for {locale}")
})?;
let mut s = String::new();
file.read_to_string(&mut 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,
})
/// Read the branding information from the given zip archive (omnijar).
fn read_branding(locale: &str, archive: &mut ZipArchive<File>) -> anyhow::Result<String> {
let mut file = archive
.by_name(&format!("localization/{locale}/branding/brand.ftl"))
.with_context(|| format!("failed to locate branding localization file for {locale}"))?;
let mut s = String::new();
file.read_to_string(&mut s)
.with_context(|| format!("failed to read branding localization file for {locale}"))?;
Ok(s)
}
fn read_omnijar_file(path: &Path) -> anyhow::Result<ZipArchive<File>> {