зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1613705 - [localization] part9: Update DOMLocalization to match Localization constructor. r=nika,emilio
Depends on D113691 Differential Revision: https://phabricator.services.mozilla.com/D113692
This commit is contained in:
Родитель
c5240d354f
Коммит
458a2494df
|
@ -4171,7 +4171,7 @@ void Document::LocalizationLinkAdded(Element* aLinkElement) {
|
|||
mDocumentL10n = DocumentL10n::Create(this, isSync);
|
||||
MOZ_ASSERT(mDocumentL10n);
|
||||
}
|
||||
mDocumentL10n->AddResourceId(href);
|
||||
mDocumentL10n->AddResourceId(NS_ConvertUTF16toUTF8(href));
|
||||
|
||||
if (mReadyState >= READYSTATE_INTERACTIVE) {
|
||||
mDocumentL10n->TriggerInitialTranslation();
|
||||
|
@ -4194,7 +4194,8 @@ void Document::LocalizationLinkRemoved(Element* aLinkElement) {
|
|||
if (mDocumentL10n) {
|
||||
nsAutoString href;
|
||||
aLinkElement->GetAttr(kNameSpaceID_None, nsGkAtoms::href, href);
|
||||
uint32_t remaining = mDocumentL10n->RemoveResourceId(href);
|
||||
uint32_t remaining =
|
||||
mDocumentL10n->RemoveResourceId(NS_ConvertUTF16toUTF8(href));
|
||||
if (remaining == 0) {
|
||||
if (mDocumentL10n->mBlockingLayout) {
|
||||
mDocumentL10n->mBlockingLayout = false;
|
||||
|
|
|
@ -35,40 +35,53 @@ NS_IMPL_RELEASE_INHERITED(DOMLocalization, Localization)
|
|||
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMLocalization)
|
||||
NS_INTERFACE_MAP_END_INHERITING(Localization)
|
||||
|
||||
/* static */
|
||||
already_AddRefed<DOMLocalization> DOMLocalization::Create(
|
||||
nsIGlobalObject* aGlobal, const bool aSync,
|
||||
const BundleGenerator& aBundleGenerator) {
|
||||
RefPtr<DOMLocalization> domLoc =
|
||||
new DOMLocalization(aGlobal, aSync, aBundleGenerator);
|
||||
|
||||
return domLoc.forget();
|
||||
}
|
||||
|
||||
DOMLocalization::DOMLocalization(nsIGlobalObject* aGlobal, const bool aSync,
|
||||
const BundleGenerator& aBundleGenerator)
|
||||
DOMLocalization::DOMLocalization(nsIGlobalObject* aGlobal, bool aSync)
|
||||
: Localization(aGlobal, aSync) {
|
||||
mMutations = new L10nMutations(this);
|
||||
}
|
||||
|
||||
DOMLocalization::DOMLocalization(nsIGlobalObject* aGlobal, bool aIsSync,
|
||||
const ffi::LocalizationRc* aRaw)
|
||||
: Localization(aGlobal, aIsSync, aRaw) {
|
||||
mMutations = new L10nMutations(this);
|
||||
}
|
||||
|
||||
already_AddRefed<DOMLocalization> DOMLocalization::Constructor(
|
||||
const GlobalObject& aGlobal, const Sequence<nsString>& aResourceIds,
|
||||
const bool aSync, const BundleGenerator& aBundleGenerator,
|
||||
ErrorResult& aRv) {
|
||||
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
||||
if (!global) {
|
||||
aRv.Throw(NS_ERROR_FAILURE);
|
||||
return nullptr;
|
||||
const GlobalObject& aGlobal, const Sequence<nsCString>& aResourceIds,
|
||||
bool aIsSync, const Optional<NonNull<L10nRegistry>>& aRegistry,
|
||||
const Optional<Sequence<nsCString>>& aLocales, ErrorResult& aRv) {
|
||||
nsTArray<nsCString> resIds = ToTArray<nsTArray<nsCString>>(aResourceIds);
|
||||
Maybe<nsTArray<nsCString>> locales;
|
||||
|
||||
if (aLocales.WasPassed()) {
|
||||
locales.emplace();
|
||||
locales->SetCapacity(aLocales.Value().Length());
|
||||
for (const auto& locale : aLocales.Value()) {
|
||||
locales->AppendElement(locale);
|
||||
}
|
||||
}
|
||||
|
||||
RefPtr<DOMLocalization> domLoc =
|
||||
DOMLocalization::Create(global, aSync, aBundleGenerator);
|
||||
RefPtr<const ffi::LocalizationRc> raw;
|
||||
bool result;
|
||||
|
||||
if (aResourceIds.Length()) {
|
||||
domLoc->AddResourceIds(aResourceIds);
|
||||
if (aRegistry.WasPassed()) {
|
||||
result = ffi::localization_new_with_locales(
|
||||
&resIds, aIsSync, aRegistry.Value().Raw(), locales.ptrOr(nullptr),
|
||||
getter_AddRefs(raw));
|
||||
} else {
|
||||
result = ffi::localization_new_with_locales(
|
||||
&resIds, aIsSync, nullptr, locales.ptrOr(nullptr), getter_AddRefs(raw));
|
||||
}
|
||||
|
||||
return domLoc.forget();
|
||||
if (result) {
|
||||
nsCOMPtr<nsIGlobalObject> global =
|
||||
do_QueryInterface(aGlobal.GetAsSupports());
|
||||
|
||||
return do_AddRef(new DOMLocalization(global, aIsSync, raw));
|
||||
}
|
||||
aRv.ThrowInvalidStateError(
|
||||
"Failed to create the Localization. Check the locales arguments.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
JSObject* DOMLocalization::WrapObject(JSContext* aCx,
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#include "mozilla/dom/L10nOverlaysBinding.h"
|
||||
#include "mozilla/dom/LocalizationBinding.h"
|
||||
#include "mozilla/dom/PromiseNativeHandler.h"
|
||||
#include "mozilla/intl/L10nRegistry.h"
|
||||
|
||||
// XXX Avoid including this here by moving function bodies to the cpp file
|
||||
#include "nsINode.h"
|
||||
|
@ -30,15 +31,13 @@ class DOMLocalization : public intl::Localization {
|
|||
NS_DECL_ISUPPORTS_INHERITED
|
||||
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DOMLocalization, Localization)
|
||||
|
||||
static already_AddRefed<DOMLocalization> Create(
|
||||
nsIGlobalObject* aGlobal, const bool aSync,
|
||||
const BundleGenerator& aBundleGenerator);
|
||||
|
||||
void Destroy();
|
||||
|
||||
static already_AddRefed<DOMLocalization> Constructor(
|
||||
const GlobalObject& aGlobal, const Sequence<nsString>& aResourceIds,
|
||||
const bool aSync, const BundleGenerator& aBundleGenerator,
|
||||
const dom::GlobalObject& aGlobal,
|
||||
const dom::Sequence<nsCString>& aResourceIds, bool aIsSync,
|
||||
const dom::Optional<dom::NonNull<intl::L10nRegistry>>& aRegistry,
|
||||
const dom::Optional<dom::Sequence<nsCString>>& aLocales,
|
||||
ErrorResult& aRv);
|
||||
|
||||
virtual JSObject* WrapObject(JSContext* aCx,
|
||||
|
@ -112,9 +111,11 @@ class DOMLocalization : public intl::Localization {
|
|||
return false;
|
||||
}
|
||||
|
||||
DOMLocalization(nsIGlobalObject* aGlobal, bool aSync);
|
||||
DOMLocalization(nsIGlobalObject* aGlobal, bool aIsSync,
|
||||
const intl::ffi::LocalizationRc* aRaw);
|
||||
|
||||
protected:
|
||||
explicit DOMLocalization(nsIGlobalObject* aGlobal, const bool aSync,
|
||||
const BundleGenerator& aBundleGenerator);
|
||||
virtual ~DOMLocalization();
|
||||
void OnChange() override;
|
||||
void DisconnectMutations();
|
||||
|
|
|
@ -48,7 +48,7 @@ RefPtr<DocumentL10n> DocumentL10n::Create(Document* aDocument, bool aSync) {
|
|||
}
|
||||
|
||||
DocumentL10n::DocumentL10n(Document* aDocument, bool aSync)
|
||||
: DOMLocalization(aDocument->GetScopeObject(), aSync, {}),
|
||||
: DOMLocalization(aDocument->GetScopeObject(), aSync),
|
||||
mDocument(aDocument),
|
||||
mState(DocumentL10nState::Constructed) {
|
||||
mContentSink = do_QueryInterface(aDocument->GetCurrentContentSink());
|
||||
|
|
|
@ -7,22 +7,24 @@
|
|||
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
|
||||
<script type="application/javascript">
|
||||
"use strict";
|
||||
async function* generateBundles(resourceIds) {
|
||||
const bundle = new FluentBundle("en-US");
|
||||
bundle.addResource(new FluentResource(`
|
||||
const l10nReg = new L10nRegistry();
|
||||
const fs = [
|
||||
{ path: "/localization/en-US/mock.ftl", source: `
|
||||
key1 = Value for Key 1
|
||||
|
||||
key2 = Value for <a>Key 2<a/>.
|
||||
`));
|
||||
yield bundle;
|
||||
}
|
||||
` },
|
||||
];
|
||||
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
|
||||
l10nReg.registerSources([source]);
|
||||
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
addLoadEvent(async () => {
|
||||
const domLoc = new DOMLocalization(
|
||||
[],
|
||||
["/mock.ftl"],
|
||||
false,
|
||||
{ generateBundles },
|
||||
l10nReg,
|
||||
["en-US"],
|
||||
);
|
||||
|
||||
await domLoc.translateFragment(document.body);
|
||||
|
|
|
@ -7,13 +7,14 @@
|
|||
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
|
||||
<script type="application/javascript">
|
||||
"use strict";
|
||||
async function* generateBundles(resourceIds) {
|
||||
const bundle = new FluentBundle("en-US");
|
||||
bundle.addResource(new FluentResource(`
|
||||
const l10nReg = new L10nRegistry();
|
||||
const fs = [
|
||||
{ path: "/localization/en-US/mock.ftl", source: `
|
||||
key1 = Value for Key 1
|
||||
`));
|
||||
yield bundle;
|
||||
}
|
||||
` },
|
||||
];
|
||||
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
|
||||
l10nReg.registerSources([source]);
|
||||
|
||||
window.onload = async function() {
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
|
@ -21,9 +22,10 @@ key1 = Value for Key 1
|
|||
const p1 = document.getElementById("p1");
|
||||
|
||||
const domLoc = new DOMLocalization(
|
||||
[],
|
||||
["/mock.ftl"],
|
||||
false,
|
||||
{ generateBundles },
|
||||
l10nReg,
|
||||
["en-US"],
|
||||
);
|
||||
|
||||
await domLoc.translateRoots();
|
||||
|
|
|
@ -44,18 +44,20 @@
|
|||
</script>
|
||||
<script type="application/javascript">
|
||||
"use strict";
|
||||
async function* generateBundles(resourceIds) {
|
||||
const bundle = new FluentBundle("en-US");
|
||||
bundle.addResource(new FluentResource(`
|
||||
const l10nReg = new L10nRegistry();
|
||||
const fs = [
|
||||
{ path: "/localization/en-US/mock.ftl", source: `
|
||||
key1 = Value for Key 1
|
||||
`));
|
||||
yield bundle;
|
||||
}
|
||||
` },
|
||||
];
|
||||
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
|
||||
l10nReg.registerSources([source]);
|
||||
|
||||
document.domLoc = new DOMLocalization(
|
||||
[],
|
||||
["/mock.ftl"],
|
||||
false,
|
||||
{ generateBundles }
|
||||
l10nReg,
|
||||
["en-US"],
|
||||
);
|
||||
</script>
|
||||
</head>
|
||||
|
|
|
@ -7,14 +7,15 @@
|
|||
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
|
||||
<script type="application/javascript">
|
||||
"use strict";
|
||||
async function* generateBundles(resourceIds) {
|
||||
const bundle = new FluentBundle("en-US");
|
||||
bundle.addResource(new FluentResource(`
|
||||
const l10nReg = new L10nRegistry();
|
||||
const fs = [
|
||||
{ path: "/localization/en-US/mock.ftl", source: `
|
||||
key1 = Value for Key 1
|
||||
key2 = Value for Key 2
|
||||
`));
|
||||
yield bundle;
|
||||
}
|
||||
` },
|
||||
];
|
||||
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
|
||||
l10nReg.registerSources([source]);
|
||||
|
||||
window.onload = async function() {
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
|
@ -22,9 +23,10 @@ key2 = Value for Key 2
|
|||
const p1 = document.getElementById("p1");
|
||||
|
||||
const domLoc = new DOMLocalization(
|
||||
[],
|
||||
["/mock.ftl"],
|
||||
false,
|
||||
{ generateBundles },
|
||||
l10nReg,
|
||||
["en-US"],
|
||||
);
|
||||
|
||||
await domLoc.translateRoots();
|
||||
|
|
|
@ -10,9 +10,9 @@
|
|||
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js" />
|
||||
<script type="application/javascript">
|
||||
<![CDATA[
|
||||
async function* generateBundles(resourceIds) {
|
||||
const bundle = new FluentBundle("en-US");
|
||||
bundle.addResource(new FluentResource(`
|
||||
const l10nReg = new L10nRegistry();
|
||||
const fs = [
|
||||
{ path: "/localization/en-US/mock.ftl", source: `
|
||||
file-menu =
|
||||
.label = File
|
||||
.accesskey = F
|
||||
|
@ -20,9 +20,10 @@ new-tab =
|
|||
.label = New Tab
|
||||
.accesskey = N
|
||||
container = Some text with an <image data-l10n-name="foo"> inside it.
|
||||
`));
|
||||
yield bundle;
|
||||
}
|
||||
` },
|
||||
];
|
||||
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
|
||||
l10nReg.registerSources([source]);
|
||||
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
|
||||
|
@ -30,11 +31,12 @@ container = Some text with an <image data-l10n-name="foo"> inside it.
|
|||
const domLoc = new DOMLocalization(
|
||||
[],
|
||||
false,
|
||||
{ generateBundles },
|
||||
l10nReg,
|
||||
["en-US"],
|
||||
);
|
||||
|
||||
async function foo() {
|
||||
domLoc.addResourceIds(["dummy.ftl"]);
|
||||
domLoc.addResourceIds(["/mock.ftl"]);
|
||||
domLoc.connectRoot(document.documentElement);
|
||||
await domLoc.translateRoots();
|
||||
|
||||
|
|
|
@ -8,15 +8,12 @@
|
|||
<script type="application/javascript">
|
||||
"use strict";
|
||||
|
||||
async function* generateBundles(resourceIds) {}
|
||||
|
||||
window.onload = function() {
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
|
||||
const domLoc = new DOMLocalization(
|
||||
[],
|
||||
false,
|
||||
{ generateBundles },
|
||||
);
|
||||
|
||||
const p1 = document.querySelectorAll("p")[0];
|
||||
|
|
|
@ -7,12 +7,15 @@
|
|||
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
|
||||
<script type="application/javascript">
|
||||
"use strict";
|
||||
async function* generateBundles(resourceIds) {
|
||||
const bundle = new FluentBundle("en-US");
|
||||
bundle.addResource(new FluentResource("title = Hello World"));
|
||||
bundle.addResource(new FluentResource("title2 = Hello Another World"));
|
||||
yield bundle;
|
||||
}
|
||||
const l10nReg = new L10nRegistry();
|
||||
const fs = [
|
||||
{ path: "/localization/en-US/mock.ftl", source: `
|
||||
title = Hello World
|
||||
title2 = Hello Another World
|
||||
` },
|
||||
];
|
||||
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
|
||||
l10nReg.registerSources([source]);
|
||||
|
||||
window.onload = async function() {
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
|
@ -20,12 +23,13 @@
|
|||
const domLoc = new DOMLocalization(
|
||||
[],
|
||||
false,
|
||||
{ generateBundles },
|
||||
l10nReg,
|
||||
["en-US"],
|
||||
);
|
||||
|
||||
const h1 = document.querySelectorAll("h1")[0];
|
||||
|
||||
domLoc.addResourceIds(["dummy.ftl"]);
|
||||
domLoc.addResourceIds(["/mock.ftl"]);
|
||||
domLoc.connectRoot(document.body);
|
||||
|
||||
await domLoc.translateRoots();
|
||||
|
|
|
@ -7,20 +7,24 @@
|
|||
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
|
||||
<script type="application/javascript">
|
||||
"use strict";
|
||||
async function* generateBundles(resourceIds) {
|
||||
const bundle = new FluentBundle("en-US");
|
||||
bundle.addResource(new FluentResource("title = <strong>Hello</strong> World"));
|
||||
bundle.addResource(new FluentResource(`title2 = This is <a data-l10n-name="link">a link</a>!`));
|
||||
yield bundle;
|
||||
}
|
||||
const l10nReg = new L10nRegistry();
|
||||
const fs = [
|
||||
{ path: "/localization/en-US/mock.ftl", source: `
|
||||
title = <strong>Hello</strong> World
|
||||
title2 = This is <a data-l10n-name="link">a link</a>!
|
||||
` },
|
||||
];
|
||||
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
|
||||
l10nReg.registerSources([source]);
|
||||
|
||||
window.onload = async function() {
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
|
||||
const domLoc = new DOMLocalization(
|
||||
[],
|
||||
["/mock.ftl"],
|
||||
false,
|
||||
{ generateBundles },
|
||||
l10nReg,
|
||||
["en-US"],
|
||||
);
|
||||
|
||||
const p1 = document.querySelectorAll("p")[0];
|
||||
|
|
|
@ -7,18 +7,15 @@
|
|||
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
|
||||
<script type="application/javascript">
|
||||
"use strict";
|
||||
async function* generateBundles(resourceIds) {
|
||||
const bundle = new FluentBundle("en-US");
|
||||
// No translations!
|
||||
yield bundle;
|
||||
}
|
||||
const l10nReg = new L10nRegistry();
|
||||
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
addLoadEvent(async () => {
|
||||
const domLoc = new DOMLocalization(
|
||||
[],
|
||||
false,
|
||||
{ generateBundles },
|
||||
l10nReg,
|
||||
["en-US"],
|
||||
);
|
||||
|
||||
await domLoc.translateFragment(document.body).then(() => {
|
||||
|
|
|
@ -7,19 +7,23 @@
|
|||
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
|
||||
<script type="application/javascript">
|
||||
"use strict";
|
||||
async function* generateBundles(resourceIds) {
|
||||
const bundle = new FluentBundle("en-US");
|
||||
bundle.addResource(new FluentResource(`title = Visit <a data-l10n-name="mozilla-link">Mozilla</a> or <a data-l10n-name="firefox-link">Firefox</a> website!`));
|
||||
yield bundle;
|
||||
}
|
||||
const l10nReg = new L10nRegistry();
|
||||
const fs = [
|
||||
{ path: "/localization/en-US/mock.ftl", source: `
|
||||
title = Visit <a data-l10n-name="mozilla-link">Mozilla</a> or <a data-l10n-name="firefox-link">Firefox</a> website!
|
||||
` },
|
||||
];
|
||||
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
|
||||
l10nReg.registerSources([source]);
|
||||
|
||||
window.onload = async function() {
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
|
||||
const domLoc = new DOMLocalization(
|
||||
[],
|
||||
["/mock.ftl"],
|
||||
false,
|
||||
{ generateBundles },
|
||||
l10nReg,
|
||||
["en-US"],
|
||||
);
|
||||
|
||||
await domLoc.translateFragment(document.body);
|
||||
|
|
|
@ -7,19 +7,23 @@
|
|||
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
|
||||
<script type="application/javascript">
|
||||
"use strict";
|
||||
async function* generateBundles(resourceIds) {
|
||||
const bundle = new FluentBundle("en-US");
|
||||
bundle.addResource(new FluentResource(`title = Visit <a data-l10n-name="mozilla-link">Mozilla</a> or <a data-l10n-name="firefox-link">Firefox</a> website!`));
|
||||
yield bundle;
|
||||
}
|
||||
const l10nReg = new L10nRegistry();
|
||||
const fs = [
|
||||
{ path: "/localization/en-US/mock.ftl", source: `
|
||||
title = Visit <a data-l10n-name="mozilla-link">Mozilla</a> or <a data-l10n-name="firefox-link">Firefox</a> website!
|
||||
` },
|
||||
];
|
||||
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
|
||||
l10nReg.registerSources([source]);
|
||||
|
||||
window.onload = async function() {
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
|
||||
const domLoc = new DOMLocalization(
|
||||
[],
|
||||
["/mock.ftl"],
|
||||
false,
|
||||
{ generateBundles },
|
||||
l10nReg,
|
||||
["en-US"],
|
||||
);
|
||||
|
||||
await domLoc.translateFragment(document.body);
|
||||
|
|
|
@ -7,23 +7,25 @@
|
|||
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
|
||||
<script type="application/javascript">
|
||||
"use strict";
|
||||
async function* generateBundles(resourceIds) {
|
||||
const bundle = new FluentBundle("en-US");
|
||||
bundle.addResource(new FluentResource(`
|
||||
const l10nReg = new L10nRegistry();
|
||||
const fs = [
|
||||
{ path: "/localization/en-US/mock.ftl", source: `
|
||||
key1 =
|
||||
.href = https://www.hacked.com
|
||||
|
||||
key2 =
|
||||
.href = https://pl.wikipedia.org
|
||||
`));
|
||||
yield bundle;
|
||||
}
|
||||
` },
|
||||
];
|
||||
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
|
||||
l10nReg.registerSources([source]);
|
||||
|
||||
async function test() {
|
||||
const domLoc = new DOMLocalization(
|
||||
[],
|
||||
["/mock.ftl"],
|
||||
false,
|
||||
{ generateBundles },
|
||||
l10nReg,
|
||||
["en-US"],
|
||||
);
|
||||
|
||||
await domLoc.translateFragment(document.body);
|
||||
|
|
|
@ -7,22 +7,24 @@
|
|||
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
|
||||
<script type="application/javascript">
|
||||
"use strict";
|
||||
async function* generateBundles(resourceIds) {
|
||||
const bundle = new FluentBundle("en-US");
|
||||
bundle.addResource(new FluentResource(`
|
||||
const l10nReg = new L10nRegistry();
|
||||
const fs = [
|
||||
{ path: "/localization/en-US/mock.ftl", source: `
|
||||
key1 = Translation For Key 1
|
||||
|
||||
key2 = Visit <a data-l10n-name="link">this link<a/>.
|
||||
`));
|
||||
yield bundle;
|
||||
}
|
||||
` },
|
||||
];
|
||||
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
|
||||
l10nReg.registerSources([source]);
|
||||
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
addLoadEvent(async () => {
|
||||
const domLoc = new DOMLocalization(
|
||||
[],
|
||||
["/mock.ftl"],
|
||||
false,
|
||||
{ generateBundles },
|
||||
l10nReg,
|
||||
["en-US"],
|
||||
);
|
||||
|
||||
await domLoc.translateFragment(document.body);
|
||||
|
|
|
@ -7,8 +7,7 @@
|
|||
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
|
||||
<script type="application/javascript">
|
||||
"use strict";
|
||||
|
||||
async function* generateBundles(resourceIds) {}
|
||||
const l10nReg = new L10nRegistry();
|
||||
|
||||
window.onload = function() {
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
|
@ -16,7 +15,7 @@
|
|||
const domLoc = new DOMLocalization(
|
||||
[],
|
||||
false,
|
||||
{ generateBundles },
|
||||
l10nReg,
|
||||
);
|
||||
|
||||
const p1 = document.querySelectorAll("p")[0];
|
||||
|
|
|
@ -7,20 +7,25 @@
|
|||
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
|
||||
<script type="application/javascript">
|
||||
"use strict";
|
||||
async function* generateBundles(resourceIds) {
|
||||
const bundle = new FluentBundle("en-US");
|
||||
bundle.addResource(new FluentResource("title = Hello World"));
|
||||
bundle.addResource(new FluentResource("link =\n .title = Click me"));
|
||||
yield bundle;
|
||||
}
|
||||
const l10nReg = new L10nRegistry();
|
||||
const fs = [
|
||||
{ path: "/localization/en-US/mock.ftl", source: `
|
||||
title = Hello World
|
||||
link =
|
||||
.title = Click me
|
||||
` },
|
||||
];
|
||||
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
|
||||
l10nReg.registerSources([source]);
|
||||
|
||||
window.onload = async function() {
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
|
||||
const domLoc = new DOMLocalization(
|
||||
[],
|
||||
["/mock.ftl"],
|
||||
false,
|
||||
{ generateBundles },
|
||||
l10nReg,
|
||||
["en-US"],
|
||||
);
|
||||
|
||||
const p1 = document.querySelectorAll("p")[0];
|
||||
|
|
|
@ -7,20 +7,24 @@
|
|||
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
|
||||
<script type="application/javascript">
|
||||
"use strict";
|
||||
async function* generateBundles(resourceIds) {
|
||||
const bundle = new FluentBundle("en-US");
|
||||
bundle.addResource(new FluentResource("title = Hello World"));
|
||||
bundle.addResource(new FluentResource("subtitle = Welcome to FluentBundle"));
|
||||
yield bundle;
|
||||
}
|
||||
const l10nReg = new L10nRegistry();
|
||||
const fs = [
|
||||
{ path: "/localization/en-US/mock.ftl", source: `
|
||||
title = Hello World
|
||||
subtitle = Welcome to FluentBundle
|
||||
` },
|
||||
];
|
||||
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
|
||||
l10nReg.registerSources([source]);
|
||||
|
||||
window.onload = async function() {
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
|
||||
const domLoc = new DOMLocalization(
|
||||
[],
|
||||
["/mock.ftl"],
|
||||
false,
|
||||
{ generateBundles },
|
||||
l10nReg,
|
||||
["en-US"],
|
||||
);
|
||||
|
||||
const frag = document.querySelectorAll("div")[0];
|
||||
|
|
|
@ -7,12 +7,15 @@
|
|||
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
|
||||
<script type="application/javascript">
|
||||
"use strict";
|
||||
async function* generateBundles(resourceIds) {
|
||||
const bundle = new FluentBundle("en-US");
|
||||
bundle.addResource(new FluentResource("title = Hello World"));
|
||||
bundle.addResource(new FluentResource("title2 = Hello Another World"));
|
||||
yield bundle;
|
||||
}
|
||||
const l10nReg = new L10nRegistry();
|
||||
const fs = [
|
||||
{ path: "/localization/en-US/mock.ftl", source: `
|
||||
title = Hello World
|
||||
title2 = Hello Another World
|
||||
` },
|
||||
];
|
||||
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
|
||||
l10nReg.registerSources([source]);
|
||||
|
||||
window.onload = async function() {
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
|
@ -20,7 +23,8 @@
|
|||
const domLoc = new DOMLocalization(
|
||||
[],
|
||||
false,
|
||||
{ generateBundles },
|
||||
l10nReg,
|
||||
["en-US"],
|
||||
);
|
||||
|
||||
const frag1 = document.querySelectorAll("div")[0];
|
||||
|
@ -28,7 +32,7 @@
|
|||
const h1 = document.querySelectorAll("h1")[0];
|
||||
const h2 = document.querySelectorAll("h2")[0];
|
||||
|
||||
domLoc.addResourceIds(["dummy.ftl"]);
|
||||
domLoc.addResourceIds(["/mock.ftl"]);
|
||||
domLoc.connectRoot(frag1);
|
||||
domLoc.connectRoot(frag2);
|
||||
|
||||
|
|
|
@ -7,21 +7,23 @@
|
|||
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
|
||||
<script type="application/javascript">
|
||||
"use strict";
|
||||
async function* generateBundles(resourceIds) {
|
||||
const bundle = new FluentBundle("en-US");
|
||||
bundle.addResource(new FluentResource(`
|
||||
const l10nReg = new L10nRegistry();
|
||||
const fs = [
|
||||
{ path: "/localization/en-US/mock.ftl", source: `
|
||||
key1 = Key 1
|
||||
key2 = Key 2
|
||||
key3 = Key 3
|
||||
key4 = Key 4
|
||||
`));
|
||||
yield bundle;
|
||||
}
|
||||
` },
|
||||
];
|
||||
const source = L10nFileSource.createMock("test", ["en-US"], "/localization/{locale}", fs);
|
||||
l10nReg.registerSources([source]);
|
||||
|
||||
document.domLoc = new DOMLocalization(
|
||||
[],
|
||||
["/mock.ftl"],
|
||||
false,
|
||||
{ generateBundles }
|
||||
l10nReg,
|
||||
["en-US"],
|
||||
);
|
||||
document.domLoc.connectRoot(document.documentElement);
|
||||
</script>
|
||||
|
|
|
@ -36,14 +36,14 @@ interface DOMLocalization : Localization {
|
|||
* This enables a number of synchronous methods on the
|
||||
* Localization API and uses it for `TranslateElements`
|
||||
* making the method return a synchronusly resolved promise.
|
||||
* - aBundleGenerator - an object with two methods - `generateBundles` and
|
||||
* `generateBundlesSync` allowing consumers to overload the
|
||||
* default generators provided by Gecko.
|
||||
* - aRegistry - optional custom L10nRegistry to be used by this Localization instance.
|
||||
* - aLocales - custom set of locales to be used for this Localization.
|
||||
*/
|
||||
[Throws]
|
||||
constructor(sequence<DOMString> aResourceIds,
|
||||
constructor(sequence<UTF8String> aResourceIds,
|
||||
optional boolean aSync = false,
|
||||
optional BundleGenerator aBundleGenerator = {});
|
||||
optional L10nRegistry aRegistry,
|
||||
optional sequence<UTF8String> aLocales);
|
||||
|
||||
/**
|
||||
* Adds a node to nodes observed for localization
|
||||
|
|
|
@ -43,24 +43,6 @@ dictionary L10nMessage {
|
|||
sequence<AttributeNameValue>? attributes = null;
|
||||
};
|
||||
|
||||
/**
|
||||
* A callback function which takes a list of localization resources
|
||||
* and produces an iterator over FluentBundle objects used for
|
||||
* localization with fallbacks.
|
||||
*/
|
||||
callback GenerateBundles = Promise<any> (sequence<DOMString> aResourceIds);
|
||||
callback GenerateBundlesSync = any (sequence<DOMString> aResourceIds);
|
||||
|
||||
/**
|
||||
* The structure provides custom methods for the Localization API that
|
||||
* will be used to generate the `FluentBundle` iterator.
|
||||
* This allows the consumer to overload the default Gecko generator.
|
||||
*/
|
||||
dictionary BundleGenerator {
|
||||
GenerateBundles generateBundles;
|
||||
GenerateBundlesSync generateBundlesSync;
|
||||
};
|
||||
|
||||
/**
|
||||
* Localization is an implementation of the Fluent Localization API.
|
||||
*
|
||||
|
@ -99,14 +81,14 @@ interface Localization {
|
|||
/**
|
||||
* A method for adding resources to the localization context.
|
||||
*/
|
||||
void addResourceIds(sequence<DOMString> aResourceIds);
|
||||
void addResourceIds(sequence<UTF8String> aResourceIds);
|
||||
|
||||
/**
|
||||
* A method for removing resources from the localization context.
|
||||
*
|
||||
* Returns a new count of resources used by the context.
|
||||
*/
|
||||
unsigned long removeResourceIds(sequence<DOMString> aResourceIds);
|
||||
unsigned long removeResourceIds(sequence<UTF8String> aResourceIds);
|
||||
|
||||
/**
|
||||
* Formats a value of a localization message with a given id.
|
||||
|
|
|
@ -237,31 +237,21 @@ void Localization::RegisterObservers() {
|
|||
|
||||
void Localization::OnChange() { ffi::localization_on_change(mRaw.get()); }
|
||||
|
||||
void Localization::AddResourceId(const nsAString& aResourceId) {
|
||||
NS_ConvertUTF16toUTF8 resId(aResourceId);
|
||||
ffi::localization_add_res_id(mRaw.get(), &resId);
|
||||
void Localization::AddResourceId(const nsACString& aResourceId) {
|
||||
ffi::localization_add_res_id(mRaw.get(), &aResourceId);
|
||||
}
|
||||
|
||||
uint32_t Localization::RemoveResourceId(const nsAString& aResourceId) {
|
||||
NS_ConvertUTF16toUTF8 resId(aResourceId);
|
||||
return ffi::localization_remove_res_id(mRaw.get(), &resId);
|
||||
uint32_t Localization::RemoveResourceId(const nsACString& aResourceId) {
|
||||
return ffi::localization_remove_res_id(mRaw.get(), &aResourceId);
|
||||
}
|
||||
|
||||
void Localization::AddResourceIds(const nsTArray<nsString>& aResourceIds) {
|
||||
nsTArray<nsCString> resIds(aResourceIds.Length());
|
||||
for (const auto& resId : aResourceIds) {
|
||||
resIds.AppendElement(NS_ConvertUTF16toUTF8(resId));
|
||||
}
|
||||
ffi::localization_add_res_ids(mRaw.get(), &resIds);
|
||||
void Localization::AddResourceIds(const nsTArray<nsCString>& aResourceIds) {
|
||||
ffi::localization_add_res_ids(mRaw.get(), &aResourceIds);
|
||||
}
|
||||
|
||||
uint32_t Localization::RemoveResourceIds(
|
||||
const nsTArray<nsString>& aResourceIds) {
|
||||
nsTArray<nsCString> resIds(aResourceIds.Length());
|
||||
for (const auto& resId : aResourceIds) {
|
||||
resIds.AppendElement(NS_ConvertUTF16toUTF8(resId));
|
||||
}
|
||||
return ffi::localization_remove_res_ids(mRaw.get(), &resIds);
|
||||
const nsTArray<nsCString>& aResourceIds) {
|
||||
return ffi::localization_remove_res_ids(mRaw.get(), &aResourceIds);
|
||||
}
|
||||
|
||||
already_AddRefed<Promise> Localization::FormatValue(
|
||||
|
|
|
@ -71,10 +71,10 @@ class Localization : public nsIObserver,
|
|||
const dom::Sequence<dom::OwningUTF8StringOrL10nIdArgs>& aKeys,
|
||||
nsTArray<dom::Nullable<dom::L10nMessage>>& aRetVal, ErrorResult& aRv);
|
||||
|
||||
void AddResourceId(const nsAString& aResourceId);
|
||||
uint32_t RemoveResourceId(const nsAString& aResourceId);
|
||||
void AddResourceIds(const nsTArray<nsString>& aResourceIds);
|
||||
uint32_t RemoveResourceIds(const nsTArray<nsString>& aResourceIds);
|
||||
void AddResourceId(const nsACString& aResourceId);
|
||||
uint32_t RemoveResourceId(const nsACString& aResourceId);
|
||||
void AddResourceIds(const nsTArray<nsCString>& aResourceIds);
|
||||
uint32_t RemoveResourceIds(const nsTArray<nsCString>& aResourceIds);
|
||||
|
||||
void SetAsync();
|
||||
bool IsSync();
|
||||
|
|
|
@ -113,9 +113,11 @@ using dom::AutoNoJSAPI;
|
|||
using dom::BrowserHost;
|
||||
using dom::BrowsingContext;
|
||||
using dom::Document;
|
||||
using dom::DocumentL10n;
|
||||
using dom::Element;
|
||||
using dom::EventTarget;
|
||||
using dom::LoadURIOptions;
|
||||
using dom::Promise;
|
||||
|
||||
AppWindow::AppWindow(uint32_t aChromeFlags)
|
||||
: mChromeTreeOwner(nullptr),
|
||||
|
|
Загрузка…
Ссылка в новой задаче