2019-07-05 20:05:57 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2019-06-25 22:39:53 +03:00
|
|
|
#ifndef mozilla_dom_l10n_DOMLocalization_h
|
|
|
|
#define mozilla_dom_l10n_DOMLocalization_h
|
2019-06-06 19:31:25 +03:00
|
|
|
|
2019-07-26 23:17:21 +03:00
|
|
|
#include "nsXULPrototypeDocument.h"
|
2019-06-25 22:39:53 +03:00
|
|
|
#include "mozilla/intl/Localization.h"
|
2019-06-06 19:31:25 +03:00
|
|
|
#include "mozilla/dom/DOMLocalizationBinding.h"
|
2019-06-25 22:39:53 +03:00
|
|
|
#include "mozilla/dom/Element.h"
|
2019-06-06 19:32:58 +03:00
|
|
|
#include "mozilla/dom/L10nMutations.h"
|
2019-06-25 22:39:53 +03:00
|
|
|
#include "mozilla/dom/L10nOverlaysBinding.h"
|
|
|
|
#include "mozilla/dom/LocalizationBinding.h"
|
2019-06-06 19:31:25 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2019-06-25 22:39:53 +03:00
|
|
|
class DOMLocalization : public intl::Localization {
|
2019-06-06 19:31:25 +03:00
|
|
|
public:
|
2019-06-25 22:39:53 +03:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DOMLocalization, Localization)
|
2019-06-06 19:31:25 +03:00
|
|
|
|
|
|
|
explicit DOMLocalization(nsIGlobalObject* aGlobal);
|
|
|
|
|
|
|
|
static already_AddRefed<DOMLocalization> Constructor(
|
2019-06-06 19:31:37 +03:00
|
|
|
const GlobalObject& aGlobal,
|
2019-06-06 19:32:41 +03:00
|
|
|
const Optional<Sequence<nsString>>& aResourceIds,
|
|
|
|
const Optional<OwningNonNull<GenerateMessages>>& aGenerateMessages,
|
|
|
|
ErrorResult& aRv);
|
2019-06-06 19:31:25 +03:00
|
|
|
|
|
|
|
virtual JSObject* WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) override;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DOMLocalization API
|
|
|
|
*
|
|
|
|
* Methods documentation in DOMLocalization.webidl
|
|
|
|
*/
|
|
|
|
|
2019-06-26 20:04:43 +03:00
|
|
|
void ConnectRoot(nsINode& aNode, ErrorResult& aRv);
|
|
|
|
void DisconnectRoot(nsINode& aNode, ErrorResult& aRv);
|
2019-06-06 19:31:25 +03:00
|
|
|
|
|
|
|
void PauseObserving(ErrorResult& aRv);
|
|
|
|
void ResumeObserving(ErrorResult& aRv);
|
|
|
|
|
|
|
|
void SetAttributes(JSContext* aCx, Element& aElement, const nsAString& aId,
|
|
|
|
const Optional<JS::Handle<JSObject*>>& aArgs,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
void GetAttributes(JSContext* aCx, Element& aElement, L10nKey& aResult,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<Promise> TranslateFragment(nsINode& aNode, ErrorResult& aRv);
|
|
|
|
|
|
|
|
already_AddRefed<Promise> TranslateElements(
|
|
|
|
const Sequence<OwningNonNull<Element>>& aElements, ErrorResult& aRv);
|
2019-07-26 23:17:21 +03:00
|
|
|
already_AddRefed<Promise> TranslateElements(
|
|
|
|
const Sequence<OwningNonNull<Element>>& aElements,
|
|
|
|
nsXULPrototypeDocument* aProto, ErrorResult& aRv);
|
2019-06-06 19:31:25 +03:00
|
|
|
|
|
|
|
already_AddRefed<Promise> TranslateRoots(ErrorResult& aRv);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accumulates all translatable elements (ones containing
|
|
|
|
* a `data-l10n-id` attribute) from under a node into
|
|
|
|
* a list of elements.
|
|
|
|
*/
|
|
|
|
static void GetTranslatables(nsINode& aNode,
|
|
|
|
Sequence<OwningNonNull<Element>>& aElements,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the root information such as locale and direction.
|
|
|
|
*/
|
|
|
|
static void SetRootInfo(Element* aElement);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies l10n translations on translatable elements.
|
2019-07-26 23:17:21 +03:00
|
|
|
*
|
|
|
|
* If `aProto` gets passed, it'll be used to cache
|
|
|
|
* the localized elements.
|
2019-06-06 19:31:25 +03:00
|
|
|
*/
|
|
|
|
void ApplyTranslations(nsTArray<nsCOMPtr<Element>>& aElements,
|
2019-06-25 22:39:53 +03:00
|
|
|
nsTArray<L10nMessage>& aTranslations,
|
2019-07-26 23:17:21 +03:00
|
|
|
nsXULPrototypeDocument* aProto, ErrorResult& aRv);
|
2019-06-06 19:31:25 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~DOMLocalization();
|
2019-09-13 13:08:26 +03:00
|
|
|
void OnChange() override;
|
2019-06-06 19:31:25 +03:00
|
|
|
void DisconnectMutations();
|
|
|
|
void DisconnectRoots();
|
2019-06-06 19:32:58 +03:00
|
|
|
void ReportL10nOverlaysErrors(nsTArray<L10nOverlaysError>& aErrors);
|
2019-06-25 22:43:50 +03:00
|
|
|
void ConvertStringToL10nArgs(JSContext* aCx, const nsString& aInput,
|
|
|
|
intl::L10nArgs& aRetVal, ErrorResult& aRv);
|
2019-06-06 19:31:25 +03:00
|
|
|
|
2019-06-06 19:32:58 +03:00
|
|
|
RefPtr<L10nMutations> mMutations;
|
2019-06-26 20:04:43 +03:00
|
|
|
nsTHashtable<nsRefPtrHashKey<nsINode>> mRoots;
|
2019-06-06 19:31:25 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2019-07-05 20:05:57 +03:00
|
|
|
#endif
|