core.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. // Following pseudo module is set by `api-utils/addon/runner` and its load
  5. // method needs to be called before loading `core` module. But it may have
  6. // failed, so that this pseudo won't be available
  7. module.metadata = {
  8. "stability": "unstable"
  9. };
  10. let hash = {}, bestMatchingLocale = null;
  11. try {
  12. let data = require("@l10n/data");
  13. hash = data.hash;
  14. bestMatchingLocale = data.bestMatchingLocale;
  15. }
  16. catch(e) {}
  17. // Returns the translation for a given key, if available.
  18. exports.get = function get(k) {
  19. return k in hash ? hash[k] : null;
  20. }
  21. // Returns the full length locale code: ja-JP-mac, en-US or fr
  22. exports.locale = function locale() {
  23. return bestMatchingLocale;
  24. }
  25. // Returns the short locale code: ja, en, fr
  26. exports.language = function language() {
  27. return bestMatchingLocale ? bestMatchingLocale.split("-")[0].toLowerCase()
  28. : null;
  29. }