Bug 1726535 - Restore the ja-JP-macos -> ja-JP-mac L10nRegistry path overload. r=platform-i18n-reviewers,jfkthame

Differential Revision: https://phabricator.services.mozilla.com/D123133
This commit is contained in:
Zibi Braniecki 2021-08-19 17:49:11 +00:00
Родитель 1c4e04a80e
Коммит 3fa745e118
2 изменённых файлов: 46 добавлений и 8 удалений

Просмотреть файл

@ -4,7 +4,7 @@
use l10nregistry::source::FileFetcher;
use std::io;
use std::{borrow::Cow, io};
pub struct GeckoFileFetcher;
@ -13,13 +13,30 @@ fn try_string_from_box_u8(input: Box<[u8]>) -> io::Result<String> {
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err.utf8_error()))
}
// For historical reasons we maintain a locale in Firefox with a codename `ja-JP-mac`.
// This string is an invalid BCP-47 language tag, so we don't store it in Gecko, which uses
// valid BCP-47 tags only, but rather keep that quirk local to Gecko L10nRegistry file fetcher.
//
// Here, if we encounter `ja-JP-macos` (valid BCP-47), we swap it for `ja-JP-mac`.
//
// See bug 1726586 for details, and source::get_locale_from_gecko.
fn get_path_for_gecko<'s>(input: &'s str) -> Cow<'s, str> {
if input.contains("ja-JP-macos") {
input.replace("ja-JP-macos", "ja-JP-mac").into()
} else {
input.into()
}
}
#[async_trait::async_trait(?Send)]
impl FileFetcher for GeckoFileFetcher {
fn fetch_sync(&self, path: &str) -> io::Result<String> {
let path = get_path_for_gecko(path);
crate::load::load_sync(path).and_then(try_string_from_box_u8)
}
async fn fetch(&self, path: &str) -> io::Result<String> {
let path = get_path_for_gecko(path);
crate::load::load_async(path)
.await
.and_then(try_string_from_box_u8)

Просмотреть файл

@ -12,7 +12,7 @@ use nsstring::{nsACString, nsCString};
use thin_vec::ThinVec;
use unic_langid::LanguageIdentifier;
use std::{mem, rc::Rc};
use std::{borrow::Cow, mem, rc::Rc};
use xpcom::RefPtr;
#[repr(C)]
@ -24,6 +24,21 @@ pub enum L10nFileSourceStatus {
InvalidLocaleCode,
}
// For historical reasons we maintain a locale in Firefox with a codename `ja-JP-mac`.
// This string is an invalid BCP-47 language tag, so we don't store it in Gecko, which uses
// valid BCP-47 tags only, but rather keep that quirk local to Gecko L10nRegistry file fetcher.
//
// Here, if we encounter `ja-JP-mac` (invalid BCP-47), we swap it for a valid equivalent: `ja-JP-macos`.
//
// See bug 1726586 for details and fetcher::get_locale_for_gecko.
fn get_locale_from_gecko<'s>(input: Cow<'s, str>) -> Cow<'s, str> {
if input == "ja-JP-mac" {
"ja-JP-macos".into()
} else {
input
}
}
#[no_mangle]
pub extern "C" fn l10nfilesource_new(
name: &nsACString,
@ -41,8 +56,10 @@ pub extern "C" fn l10nfilesource_new(
return std::ptr::null();
}
let locales: Result<Vec<LanguageIdentifier>, _> =
locales.iter().map(|l| l.to_utf8().parse()).collect();
let locales: Result<Vec<LanguageIdentifier>, _> = locales
.iter()
.map(|l| get_locale_from_gecko(l.to_utf8()).parse())
.collect();
let locales = match locales {
Ok(locales) => locales,
@ -84,8 +101,10 @@ pub unsafe extern "C" fn l10nfilesource_new_with_index(
return std::ptr::null();
}
let locales: Result<Vec<LanguageIdentifier>, _> =
locales.iter().map(|l| l.to_utf8().parse()).collect();
let locales: Result<Vec<LanguageIdentifier>, _> = locales
.iter()
.map(|l| get_locale_from_gecko(l.to_utf8()).parse())
.collect();
let index = if index_length > 0 {
assert!(!index_elements.is_null());
@ -142,8 +161,10 @@ pub extern "C" fn l10nfilesource_new_mock(
return std::ptr::null();
}
let locales: Result<Vec<LanguageIdentifier>, _> =
locales.iter().map(|l| l.to_utf8().parse()).collect();
let locales: Result<Vec<LanguageIdentifier>, _> = locales
.iter()
.map(|l| get_locale_from_gecko(l.to_utf8()).parse())
.collect();
let locales = match locales {
Ok(locales) => locales,