message.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * DevExtreme (localization/message.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var $ = require("../core/renderer");
  11. var dependencyInjector = require("../core/utils/dependency_injector");
  12. var extend = require("../core/utils/extend").extend;
  13. var each = require("../core/utils/iterator").each;
  14. var stringFormat = require("../core/utils/string").format;
  15. var humanize = require("../core/utils/inflector").humanize;
  16. var coreLocalization = require("./core");
  17. require("./core");
  18. var PARENT_LOCALE_SEPARATOR = "-";
  19. var baseDictionary = extend(true, {}, require("./default_messages"));
  20. var parentLocales = require("./cldr-data/parentLocales");
  21. var getParentLocale = function(locale) {
  22. var parentLocale = parentLocales[locale];
  23. if (parentLocale) {
  24. return "root" !== parentLocale && parentLocale
  25. }
  26. return locale.substr(0, locale.lastIndexOf(PARENT_LOCALE_SEPARATOR))
  27. };
  28. var getDataByLocale = function(localeData, locale) {
  29. return localeData[locale] || {}
  30. };
  31. var getValueByClosestLocale = function(localeData, locale, key) {
  32. var value = getDataByLocale(localeData, locale)[key];
  33. var isRootLocale;
  34. while (!value && !isRootLocale) {
  35. locale = getParentLocale(locale);
  36. if (locale) {
  37. value = getDataByLocale(localeData, locale)[key]
  38. } else {
  39. isRootLocale = true
  40. }
  41. }
  42. return value
  43. };
  44. var newMessages = {};
  45. var messageLocalization = dependencyInjector({
  46. _dictionary: baseDictionary,
  47. load: function(messages) {
  48. extend(true, this._dictionary, messages)
  49. },
  50. _localizablePrefix: "@",
  51. setup: function(localizablePrefix) {
  52. this._localizablePrefix = localizablePrefix
  53. },
  54. localizeString: function(text) {
  55. var that = this;
  56. var regex = new RegExp("(^|[^a-zA-Z_0-9" + that._localizablePrefix + "-]+)(" + that._localizablePrefix + "{1,2})([a-zA-Z_0-9-]+)", "g");
  57. var escapeString = that._localizablePrefix + that._localizablePrefix;
  58. return text.replace(regex, function(str, prefix, escape, localizationKey) {
  59. var defaultResult = that._localizablePrefix + localizationKey;
  60. var result;
  61. if (escape !== escapeString) {
  62. result = that.format(localizationKey)
  63. }
  64. if (!result) {
  65. newMessages[localizationKey] = humanize(localizationKey)
  66. }
  67. return prefix + (result || defaultResult)
  68. })
  69. },
  70. _messageLoaded: function(key, locale) {
  71. return void 0 !== getValueByClosestLocale(this._dictionary, locale || coreLocalization.locale(), key)
  72. },
  73. localizeNode: function(node) {
  74. var that = this;
  75. $(node).each(function(index, nodeItem) {
  76. if (!nodeItem.nodeType) {
  77. return
  78. }
  79. if (3 === nodeItem.nodeType) {
  80. nodeItem.nodeValue = that.localizeString(nodeItem.nodeValue)
  81. } else {
  82. if (!$(nodeItem).is("iframe")) {
  83. each(nodeItem.attributes || [], function(index, attr) {
  84. if ("string" === typeof attr.value) {
  85. var localizedValue = that.localizeString(attr.value);
  86. if (attr.value !== localizedValue) {
  87. attr.value = localizedValue
  88. }
  89. }
  90. });
  91. $(nodeItem).contents().each(function(index, node) {
  92. that.localizeNode(node)
  93. })
  94. }
  95. }
  96. })
  97. },
  98. getMessagesByLocales: function() {
  99. return this._dictionary
  100. },
  101. getDictionary: function(onlyNew) {
  102. if (onlyNew) {
  103. return newMessages
  104. }
  105. return extend({}, newMessages, this.getMessagesByLocales()[coreLocalization.locale()])
  106. },
  107. getFormatter: function(key) {
  108. return this._getFormatterBase(key) || this._getFormatterBase(key, "en")
  109. },
  110. _getFormatterBase: function(key, locale) {
  111. var message = getValueByClosestLocale(this._dictionary, locale || coreLocalization.locale(), key);
  112. if (message) {
  113. return function() {
  114. var args = 1 === arguments.length && Array.isArray(arguments[0]) ? arguments[0].slice(0) : Array.prototype.slice.call(arguments, 0);
  115. args.unshift(message);
  116. return stringFormat.apply(this, args)
  117. }
  118. }
  119. },
  120. format: function(key) {
  121. var formatter = this.getFormatter(key);
  122. var values = Array.prototype.slice.call(arguments, 1);
  123. return formatter && formatter.apply(this, values) || ""
  124. }
  125. });
  126. module.exports = messageLocalization;