Skip to content

Commit

Permalink
Bug 1642415 - Use metasource in FileSource; r=platform-i18n-reviewers…
Browse files Browse the repository at this point in the history
…,extension-reviewers,gregtatum,zombie

Differential Revision: https://phabricator.services.mozilla.com/D125239
  • Loading branch information
dminor committed Sep 22, 2021
1 parent c5d2261 commit 7e6caf3
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 15 deletions.
1 change: 1 addition & 0 deletions browser/components/newtab/lib/RemoteL10n.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class _RemoteL10n {
);
let cfrIndexedFileSource = new L10nFileSource(
"cfr",
"app",
[appLocale],
`file://${l10nFluentDir}/`,
{
Expand Down
18 changes: 15 additions & 3 deletions dom/chrome-webidl/L10nRegistry.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ dictionary FileSourceOptions {
Exposed=Window]
interface L10nFileSource {
/**
* The `name` is the name of the `L10nFileSource`.
*
* The `metaSource` is the name of the package that contains the
* `L10nFileSource`, e.g. `app` for sources packaged with the browser, or
* `langpack` for sources packaged in an addon.
*
* The `locales` is a list of locales for which resources are contained in
* the `L10nFileSource`.
*
* The `prePath` is the path prefix for files contained in the `L10nFileSource`.
*
* Optional argument `index` can be used to provide a list
* of files available in the source.
*
Expand All @@ -41,7 +52,7 @@ interface L10nFileSource {
* files not available in the source.
*/
[Throws]
constructor(UTF8String name, sequence<UTF8String> locales, UTF8String prePath, optional FileSourceOptions options = {}, optional sequence<UTF8String> index);
constructor(UTF8String name, UTF8String metaSource, sequence<UTF8String> locales, UTF8String prePath, optional FileSourceOptions options = {}, optional sequence<UTF8String> index);

/**
* Tests may want to introduce custom file sources and
Expand All @@ -57,7 +68,7 @@ interface L10nFileSource {
* "source": "key = Hello World!",
* }
* ];
* let mockSource = L10nFileSource.createMock("mock", ["en-US"], "/localization/{locale}/", fs);
* let mockSource = L10nFileSource.createMock("mock", "app", ["en-US"], "/localization/{locale}/", fs);
*
* let reg = new L10nRegistry();
* reg.registerSources([mockSource]);
Expand All @@ -68,9 +79,10 @@ interface L10nFileSource {
* ```
*/
[Throws]
static L10nFileSource createMock(UTF8String name, sequence<UTF8String> locales, UTF8String prePath, sequence<L10nFileSourceMockFile> fs);
static L10nFileSource createMock(UTF8String name, UTF8String metasource, sequence<UTF8String> locales, UTF8String prePath, sequence<L10nFileSourceMockFile> fs);

readonly attribute UTF8String name;
readonly attribute UTF8String metaSource;
[Pure, Cached, Frozen]
readonly attribute sequence<UTF8String> locales;
/**
Expand Down
2 changes: 1 addition & 1 deletion intl/docs/locale.rst
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ It may look like this:
.. code-block:: javascript
let source = L10nFileSource.createMock(
"mock-source",
"mock-source", "app",
["ko-KR", "ar"],
"resource://mock-addon/localization/{locale}",
[
Expand Down
21 changes: 13 additions & 8 deletions intl/l10n/FileSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ L10nFileSource::L10nFileSource(RefPtr<const ffi::FileSource> aRaw,
/* static */
already_AddRefed<L10nFileSource> L10nFileSource::Constructor(
const GlobalObject& aGlobal, const nsACString& aName,
const nsTArray<nsCString>& aLocales, const nsACString& aPrePath,
const dom::FileSourceOptions& aOptions,
const nsACString& aMetaSource, const nsTArray<nsCString>& aLocales,
const nsACString& aPrePath, const dom::FileSourceOptions& aOptions,
const Optional<Sequence<nsCString>>& aIndex, ErrorResult& aRv) {
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());

Expand All @@ -35,11 +35,11 @@ already_AddRefed<L10nFileSource> L10nFileSource::Constructor(
RefPtr<const ffi::FileSource> raw;
if (aIndex.WasPassed()) {
raw = dont_AddRef(ffi::l10nfilesource_new_with_index(
&aName, &aLocales, &aPrePath, aIndex.Value().Elements(),
&aName, &aMetaSource, &aLocales, &aPrePath, aIndex.Value().Elements(),
aIndex.Value().Length(), allowOverrides, &status));
} else {
raw = dont_AddRef(ffi::l10nfilesource_new(&aName, &aLocales, &aPrePath,
allowOverrides, &status));
raw = dont_AddRef(ffi::l10nfilesource_new(
&aName, &aMetaSource, &aLocales, &aPrePath, allowOverrides, &status));
}

if (PopulateError(aRv, status)) {
Expand All @@ -51,8 +51,9 @@ already_AddRefed<L10nFileSource> L10nFileSource::Constructor(
/* static */
already_AddRefed<L10nFileSource> L10nFileSource::CreateMock(
const GlobalObject& aGlobal, const nsACString& aName,
const nsTArray<nsCString>& aLocales, const nsACString& aPrePath,
const nsTArray<L10nFileSourceMockFile>& aFS, ErrorResult& aRv) {
const nsACString& aMetaSource, const nsTArray<nsCString>& aLocales,
const nsACString& aPrePath, const nsTArray<L10nFileSourceMockFile>& aFS,
ErrorResult& aRv) {
nsTArray<ffi::L10nFileSourceMockFile> fs(aFS.Length());
for (const auto& file : aFS) {
auto f = fs.AppendElement();
Expand All @@ -64,7 +65,7 @@ already_AddRefed<L10nFileSource> L10nFileSource::CreateMock(
ffi::L10nFileSourceStatus status;

RefPtr<const ffi::FileSource> raw(dont_AddRef(ffi::l10nfilesource_new_mock(
&aName, &aLocales, &aPrePath, &fs, &status)));
&aName, &aMetaSource, &aLocales, &aPrePath, &fs, &status)));

if (PopulateError(aRv, status)) {
return nullptr;
Expand All @@ -81,6 +82,10 @@ void L10nFileSource::GetName(nsCString& aRetVal) {
ffi::l10nfilesource_get_name(mRaw.get(), &aRetVal);
}

void L10nFileSource::GetMetaSource(nsCString& aRetVal) {
ffi::l10nfilesource_get_metasource(mRaw.get(), &aRetVal);
}

void L10nFileSource::GetLocales(nsTArray<nsCString>& aRetVal) {
ffi::l10nfilesource_get_locales(mRaw.get(), &aRetVal);
}
Expand Down
8 changes: 5 additions & 3 deletions intl/l10n/FileSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ class L10nFileSource : public nsWrapperCache {

static already_AddRefed<L10nFileSource> Constructor(
const dom::GlobalObject& aGlobal, const nsACString& aName,
const nsTArray<nsCString>& aLocales, const nsACString& aPrePath,
const dom::FileSourceOptions& aOptions,
const nsACString& aMetaSource, const nsTArray<nsCString>& aLocales,
const nsACString& aPrePath, const dom::FileSourceOptions& aOptions,
const dom::Optional<dom::Sequence<nsCString>>& aIndex, ErrorResult& aRv);

static already_AddRefed<L10nFileSource> CreateMock(
const dom::GlobalObject& aGlobal, const nsACString& aName,
const nsTArray<nsCString>& aLocales, const nsACString& aPrePath,
const nsACString& aMetaSource, const nsTArray<nsCString>& aLocales,
const nsACString& aPrePath,
const nsTArray<dom::L10nFileSourceMockFile>& aFS, ErrorResult& aRv);

nsIGlobalObject* GetParentObject() const { return mGlobal; }
Expand All @@ -42,6 +43,7 @@ class L10nFileSource : public nsWrapperCache {
JS::Handle<JSObject*> aGivenProto) override;

void GetName(nsCString& aRetVal);
void GetMetaSource(nsCString& aRetVal);
void GetLocales(nsTArray<nsCString>& aRetVal);
void GetPrePath(nsCString& aRetVal);
void GetIndex(dom::Nullable<nsTArray<nsCString>>& aRetVal);
Expand Down
1 change: 1 addition & 0 deletions toolkit/components/extensions/Extension.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -3002,6 +3002,7 @@ class Langpack extends ExtensionData {
const [sourceName, basePath] = entry;
return new L10nFileSource(
`${sourceName}-${langpackId}`,
langpackId,
this.startupData.languages,
`resource://${langpackId}/${basePath}localization/{locale}/`
);
Expand Down

0 comments on commit 7e6caf3

Please sign in to comment.