translate.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /* eslint-disable strict */
  6. (function (factory) {
  7. if (typeof define === 'function' && define.amd) {
  8. define([
  9. 'jquery',
  10. 'mage/mage'
  11. ], factory);
  12. } else {
  13. factory(jQuery);
  14. }
  15. }(function ($) {
  16. $.extend(true, $, {
  17. mage: {
  18. translate: (function () {
  19. /**
  20. * Key-value translations storage
  21. * @type {Object}
  22. * @private
  23. */
  24. var _data = {};
  25. /**
  26. * Add new translation (two string parameters) or several translations (object)
  27. */
  28. this.add = function () {
  29. if (arguments.length > 1) {
  30. _data[arguments[0]] = arguments[1];
  31. } else if (typeof arguments[0] === 'object') {
  32. $.extend(_data, arguments[0]);
  33. }
  34. };
  35. /**
  36. * Make a translation with parsing (to handle case when _data represents tuple)
  37. * @param {String} text
  38. * @return {String}
  39. */
  40. this.translate = function (text) {
  41. return _data[text] ? _data[text] : text;
  42. };
  43. return this;
  44. }())
  45. }
  46. });
  47. $.mage.__ = $.proxy($.mage.translate.translate, $.mage.translate);
  48. return $.mage.__;
  49. }));