genericl10n.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. /** @typedef {import("./interfaces").IL10n} IL10n */
  16. import "../external/webL10n/l10n.js";
  17. import { fixupLangCode, getL10nFallback } from "./l10n_utils.js";
  18. const webL10n = document.webL10n;
  19. /**
  20. * @implements {IL10n}
  21. */
  22. class GenericL10n {
  23. constructor(lang) {
  24. this._lang = lang;
  25. this._ready = new Promise((resolve, reject) => {
  26. webL10n.setLanguage(fixupLangCode(lang), () => {
  27. resolve(webL10n);
  28. });
  29. });
  30. }
  31. async getLanguage() {
  32. const l10n = await this._ready;
  33. return l10n.getLanguage();
  34. }
  35. async getDirection() {
  36. const l10n = await this._ready;
  37. return l10n.getDirection();
  38. }
  39. async get(key, args = null, fallback = getL10nFallback(key, args)) {
  40. const l10n = await this._ready;
  41. return l10n.get(key, args, fallback);
  42. }
  43. async translate(element) {
  44. const l10n = await this._ready;
  45. return l10n.translate(element);
  46. }
  47. }
  48. export { GenericL10n };