@zohodesk/i18n 1.0.0-beta.38-murphy → 1.0.0-beta.39-murphy

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,82 +6,58 @@ export const I18N_ERROR_TYPES = {
6
6
  const reportedKeys = new Set();
7
7
  const pendingErrors = new Map();
8
8
  const MAX_PENDING_ERRORS = 100;
9
+ let hasLoggedQueueUse = false;
10
+ let hasLoggedFlushUse = false;
9
11
  function isMurphyAvailable() {
10
12
  return typeof murphy !== 'undefined' && typeof murphy.error === 'function';
11
13
  }
12
- function isDebugEnabled() {
13
- return typeof window !== 'undefined' && window.__I18N_DEBUG__ === true;
14
- }
15
- function debugLog(message, meta) {
16
- if (!isDebugEnabled() || typeof console === 'undefined') {
17
- return;
18
- }
19
- if (typeof console.log !== 'function') {
20
- return;
21
- }
22
- if (typeof meta === 'undefined') {
23
- console.log(`[i18n] ${message}`);
24
- } else {
25
- console.log(`[i18n] ${message}`, meta);
26
- }
27
- }
28
14
  function sendToMurphy(type, key) {
29
- debugLog('reporting i18n error', {
30
- type,
31
- key
32
- });
33
15
  const error = new Error(`i18n ${type}: ${key}`);
34
16
  error.name = type;
35
- murphy.error(error, undefined, {
17
+ const murphyOptions = {
36
18
  customTags: {
37
19
  errorType: type,
38
20
  i18nKey: key,
39
21
  category: 'i18n'
40
22
  },
41
23
  preventClientGrouping: true
42
- });
24
+ };
25
+ try {
26
+ murphy.error(error, undefined, murphyOptions);
27
+ } catch (err) {
28
+ // Avoid throwing from error reporting
29
+ }
43
30
  }
44
31
  function queuePendingError(dedupeKey, type, key) {
45
32
  if (pendingErrors.has(dedupeKey)) {
46
- debugLog('pending i18n error deduped', {
47
- type,
48
- key
49
- });
50
33
  return;
51
34
  }
35
+ if (!hasLoggedQueueUse && typeof console !== 'undefined') {
36
+ console.log('[i18n] queued errors while murphy not available');
37
+ hasLoggedQueueUse = true;
38
+ }
52
39
  if (pendingErrors.size >= MAX_PENDING_ERRORS) {
53
40
  const oldestKey = pendingErrors.keys().next().value;
54
41
  pendingErrors.delete(oldestKey);
55
- debugLog('pending i18n error dropped (queue full)', {
56
- droppedKey: oldestKey,
57
- pendingSize: pendingErrors.size
58
- });
59
42
  }
60
43
  pendingErrors.set(dedupeKey, {
61
44
  type,
62
45
  key
63
46
  });
64
- debugLog('queued i18n error (murphy not ready)', {
65
- type,
66
- key,
67
- pendingSize: pendingErrors.size
68
- });
69
47
  }
70
48
  export function flushI18NErrors() {
71
49
  if (!isMurphyAvailable()) {
72
- debugLog('flush skipped (murphy not available)', {
73
- pendingSize: pendingErrors.size
74
- });
75
50
  return;
76
51
  }
77
52
  if (pendingErrors.size === 0) {
78
- debugLog('flush skipped (no pending errors)');
79
53
  return;
80
54
  }
81
- debugLog('flushing pending i18n errors', {
82
- pendingSize: pendingErrors.size
83
- });
84
- let flushedCount = 0;
55
+ if (!hasLoggedFlushUse && typeof console !== 'undefined') {
56
+ console.log('[i18n] flushing queued errors', {
57
+ count: pendingErrors.size
58
+ });
59
+ hasLoggedFlushUse = true;
60
+ }
85
61
  for (const [dedupeKey, data] of pendingErrors) {
86
62
  if (reportedKeys.has(dedupeKey)) {
87
63
  pendingErrors.delete(dedupeKey);
@@ -90,40 +66,19 @@ export function flushI18NErrors() {
90
66
  sendToMurphy(data.type, data.key);
91
67
  reportedKeys.add(dedupeKey);
92
68
  pendingErrors.delete(dedupeKey);
93
- flushedCount += 1;
94
69
  }
95
- debugLog('flush complete', {
96
- flushedCount,
97
- pendingRemaining: pendingErrors.size
98
- });
99
70
  }
100
71
  export function reportI18NError(type, key) {
101
- debugLog('reportI18NError called', {
102
- type,
103
- key
104
- });
105
72
  const dedupeKey = `${type}:${key}`;
106
73
  if (reportedKeys.has(dedupeKey)) {
107
- debugLog('reported i18n error deduped', {
108
- type,
109
- key
110
- });
111
74
  return;
112
75
  }
113
76
  if (!isMurphyAvailable()) {
114
- debugLog('murphy not available, queueing i18n error', {
115
- type,
116
- key
117
- });
118
77
  queuePendingError(dedupeKey, type, key);
119
78
  return;
120
79
  }
121
80
  flushI18NErrors();
122
81
  if (reportedKeys.has(dedupeKey)) {
123
- debugLog('reported i18n error deduped after flush', {
124
- type,
125
- key
126
- });
127
82
  return;
128
83
  }
129
84
  sendToMurphy(type, key);
@@ -132,7 +87,6 @@ export function reportI18NError(type, key) {
132
87
  export function clearReportedKeys() {
133
88
  reportedKeys.clear();
134
89
  pendingErrors.clear();
135
- debugLog('cleared reported and pending i18n errors');
136
90
  }
137
91
  export function getFallbackText(key) {
138
92
  const isNumeric = /^\d+$/.test(key);
package/es/utils/index.js CHANGED
@@ -244,7 +244,8 @@ export function getI18NValue(i18n) {
244
244
  let i18nStr = i18n[finalKey];
245
245
  if (i18nStr === undefined) {
246
246
  const isNumeric = /^\d+$/.test(finalKey);
247
- reportI18NError(isNumeric ? I18N_ERROR_TYPES.NUMERIC_FALLBACK : I18N_ERROR_TYPES.MISSING_KEY, finalKey);
247
+ const errorType = isNumeric ? I18N_ERROR_TYPES.NUMERIC_FALLBACK : I18N_ERROR_TYPES.MISSING_KEY;
248
+ reportI18NError(errorType, finalKey);
248
249
  return localizedValue || getFallbackText(finalKey);
249
250
  }
250
251
  i18nStr = replaceI18NValuesWithRegex(i18nStr, values);
@@ -23,82 +23,58 @@ var I18N_ERROR_TYPES = exports.I18N_ERROR_TYPES = {
23
23
  var reportedKeys = new Set();
24
24
  var pendingErrors = new Map();
25
25
  var MAX_PENDING_ERRORS = 100;
26
+ var hasLoggedQueueUse = false;
27
+ var hasLoggedFlushUse = false;
26
28
  function isMurphyAvailable() {
27
29
  return typeof murphy !== 'undefined' && typeof murphy.error === 'function';
28
30
  }
29
- function isDebugEnabled() {
30
- return typeof window !== 'undefined' && window.__I18N_DEBUG__ === true;
31
- }
32
- function debugLog(message, meta) {
33
- if (!isDebugEnabled() || typeof console === 'undefined') {
34
- return;
35
- }
36
- if (typeof console.log !== 'function') {
37
- return;
38
- }
39
- if (typeof meta === 'undefined') {
40
- console.log("[i18n] ".concat(message));
41
- } else {
42
- console.log("[i18n] ".concat(message), meta);
43
- }
44
- }
45
31
  function sendToMurphy(type, key) {
46
- debugLog('reporting i18n error', {
47
- type: type,
48
- key: key
49
- });
50
32
  var error = new Error("i18n ".concat(type, ": ").concat(key));
51
33
  error.name = type;
52
- murphy.error(error, undefined, {
34
+ var murphyOptions = {
53
35
  customTags: {
54
36
  errorType: type,
55
37
  i18nKey: key,
56
38
  category: 'i18n'
57
39
  },
58
40
  preventClientGrouping: true
59
- });
41
+ };
42
+ try {
43
+ murphy.error(error, undefined, murphyOptions);
44
+ } catch (err) {
45
+ // Avoid throwing from error reporting
46
+ }
60
47
  }
61
48
  function queuePendingError(dedupeKey, type, key) {
62
49
  if (pendingErrors.has(dedupeKey)) {
63
- debugLog('pending i18n error deduped', {
64
- type: type,
65
- key: key
66
- });
67
50
  return;
68
51
  }
52
+ if (!hasLoggedQueueUse && typeof console !== 'undefined') {
53
+ console.log('[i18n] queued errors while murphy not available');
54
+ hasLoggedQueueUse = true;
55
+ }
69
56
  if (pendingErrors.size >= MAX_PENDING_ERRORS) {
70
57
  var oldestKey = pendingErrors.keys().next().value;
71
58
  pendingErrors["delete"](oldestKey);
72
- debugLog('pending i18n error dropped (queue full)', {
73
- droppedKey: oldestKey,
74
- pendingSize: pendingErrors.size
75
- });
76
59
  }
77
60
  pendingErrors.set(dedupeKey, {
78
61
  type: type,
79
62
  key: key
80
63
  });
81
- debugLog('queued i18n error (murphy not ready)', {
82
- type: type,
83
- key: key,
84
- pendingSize: pendingErrors.size
85
- });
86
64
  }
87
65
  function flushI18NErrors() {
88
66
  if (!isMurphyAvailable()) {
89
- debugLog('flush skipped (murphy not available)', {
90
- pendingSize: pendingErrors.size
91
- });
92
67
  return;
93
68
  }
94
69
  if (pendingErrors.size === 0) {
95
- debugLog('flush skipped (no pending errors)');
96
70
  return;
97
71
  }
98
- debugLog('flushing pending i18n errors', {
99
- pendingSize: pendingErrors.size
100
- });
101
- var flushedCount = 0;
72
+ if (!hasLoggedFlushUse && typeof console !== 'undefined') {
73
+ console.log('[i18n] flushing queued errors', {
74
+ count: pendingErrors.size
75
+ });
76
+ hasLoggedFlushUse = true;
77
+ }
102
78
  var _iterator = _createForOfIteratorHelper(pendingErrors),
103
79
  _step;
104
80
  try {
@@ -113,45 +89,24 @@ function flushI18NErrors() {
113
89
  sendToMurphy(data.type, data.key);
114
90
  reportedKeys.add(dedupeKey);
115
91
  pendingErrors["delete"](dedupeKey);
116
- flushedCount += 1;
117
92
  }
118
93
  } catch (err) {
119
94
  _iterator.e(err);
120
95
  } finally {
121
96
  _iterator.f();
122
97
  }
123
- debugLog('flush complete', {
124
- flushedCount: flushedCount,
125
- pendingRemaining: pendingErrors.size
126
- });
127
98
  }
128
99
  function reportI18NError(type, key) {
129
- debugLog('reportI18NError called', {
130
- type: type,
131
- key: key
132
- });
133
100
  var dedupeKey = "".concat(type, ":").concat(key);
134
101
  if (reportedKeys.has(dedupeKey)) {
135
- debugLog('reported i18n error deduped', {
136
- type: type,
137
- key: key
138
- });
139
102
  return;
140
103
  }
141
104
  if (!isMurphyAvailable()) {
142
- debugLog('murphy not available, queueing i18n error', {
143
- type: type,
144
- key: key
145
- });
146
105
  queuePendingError(dedupeKey, type, key);
147
106
  return;
148
107
  }
149
108
  flushI18NErrors();
150
109
  if (reportedKeys.has(dedupeKey)) {
151
- debugLog('reported i18n error deduped after flush', {
152
- type: type,
153
- key: key
154
- });
155
110
  return;
156
111
  }
157
112
  sendToMurphy(type, key);
@@ -160,7 +115,6 @@ function reportI18NError(type, key) {
160
115
  function clearReportedKeys() {
161
116
  reportedKeys.clear();
162
117
  pendingErrors.clear();
163
- debugLog('cleared reported and pending i18n errors');
164
118
  }
165
119
  function getFallbackText(key) {
166
120
  var isNumeric = /^\d+$/.test(key);
@@ -281,7 +281,8 @@ function getI18NValue(i18n) {
281
281
  var i18nStr = i18n[finalKey];
282
282
  if (i18nStr === undefined) {
283
283
  var isNumeric = /^\d+$/.test(finalKey);
284
- (0, _errorReporter.reportI18NError)(isNumeric ? _errorReporter.I18N_ERROR_TYPES.NUMERIC_FALLBACK : _errorReporter.I18N_ERROR_TYPES.MISSING_KEY, finalKey);
284
+ var errorType = isNumeric ? _errorReporter.I18N_ERROR_TYPES.NUMERIC_FALLBACK : _errorReporter.I18N_ERROR_TYPES.MISSING_KEY;
285
+ (0, _errorReporter.reportI18NError)(errorType, finalKey);
285
286
  return localizedValue || (0, _errorReporter.getFallbackText)(finalKey);
286
287
  }
287
288
  i18nStr = replaceI18NValuesWithRegex(i18nStr, values);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zohodesk/i18n",
3
- "version": "1.0.0-beta.38-murphy",
3
+ "version": "1.0.0-beta.39-murphy",
4
4
  "main": "lib/index",
5
5
  "module": "es/index.js",
6
6
  "jsnext:main": "es/index.js",
@@ -7,129 +7,104 @@ export const I18N_ERROR_TYPES = {
7
7
  const reportedKeys = new Set();
8
8
  const pendingErrors = new Map();
9
9
  const MAX_PENDING_ERRORS = 100;
10
+ let hasLoggedQueueUse = false;
11
+ let hasLoggedFlushUse = false;
10
12
 
11
13
  function isMurphyAvailable() {
12
14
  return typeof murphy !== 'undefined' && typeof murphy.error === 'function';
13
15
  }
14
16
 
15
- function isDebugEnabled() {
16
- return typeof window !== 'undefined' && window.__I18N_DEBUG__ === true;
17
- }
18
-
19
- function debugLog(message, meta) {
20
- if (!isDebugEnabled() || typeof console === 'undefined') {
21
- return;
22
- }
23
- if (typeof console.log !== 'function') {
24
- return;
25
- }
26
- if (typeof meta === 'undefined') {
27
- console.log(`[i18n] ${message}`);
28
- } else {
29
- console.log(`[i18n] ${message}`, meta);
30
- }
31
- }
32
-
33
17
  function sendToMurphy(type, key) {
34
- debugLog('reporting i18n error', { type, key });
35
18
  const error = new Error(`i18n ${type}: ${key}`);
36
19
  error.name = type;
37
20
 
38
- murphy.error(error, undefined, {
21
+ const murphyOptions = {
39
22
  customTags: {
40
23
  errorType: type,
41
24
  i18nKey: key,
42
25
  category: 'i18n'
43
26
  },
44
27
  preventClientGrouping: true
45
- });
28
+ };
29
+
30
+ try {
31
+ murphy.error(error, undefined, murphyOptions);
32
+ } catch (err) {
33
+ // Avoid throwing from error reporting
34
+ }
46
35
  }
47
36
 
48
37
  function queuePendingError(dedupeKey, type, key) {
49
38
  if (pendingErrors.has(dedupeKey)) {
50
- debugLog('pending i18n error deduped', { type, key });
51
39
  return;
52
40
  }
53
41
 
42
+ if (!hasLoggedQueueUse && typeof console !== 'undefined') {
43
+ console.log('[i18n] queued errors while murphy not available');
44
+ hasLoggedQueueUse = true;
45
+ }
46
+
54
47
  if (pendingErrors.size >= MAX_PENDING_ERRORS) {
55
48
  const oldestKey = pendingErrors.keys().next().value;
56
49
  pendingErrors.delete(oldestKey);
57
- debugLog('pending i18n error dropped (queue full)', {
58
- droppedKey: oldestKey,
59
- pendingSize: pendingErrors.size
60
- });
61
50
  }
62
51
 
63
52
  pendingErrors.set(dedupeKey, { type, key });
64
- debugLog('queued i18n error (murphy not ready)', {
65
- type,
66
- key,
67
- pendingSize: pendingErrors.size
68
- });
69
53
  }
70
54
 
71
55
  export function flushI18NErrors() {
72
56
  if (!isMurphyAvailable()) {
73
- debugLog('flush skipped (murphy not available)', {
74
- pendingSize: pendingErrors.size
75
- });
76
57
  return;
77
58
  }
78
59
 
79
60
  if (pendingErrors.size === 0) {
80
- debugLog('flush skipped (no pending errors)');
81
61
  return;
82
62
  }
83
63
 
84
- debugLog('flushing pending i18n errors', {
85
- pendingSize: pendingErrors.size
86
- });
64
+ if (!hasLoggedFlushUse && typeof console !== 'undefined') {
65
+ console.log('[i18n] flushing queued errors', {
66
+ count: pendingErrors.size
67
+ });
68
+ hasLoggedFlushUse = true;
69
+ }
87
70
 
88
- let flushedCount = 0;
89
71
  for (const [dedupeKey, data] of pendingErrors) {
90
72
  if (reportedKeys.has(dedupeKey)) {
91
73
  pendingErrors.delete(dedupeKey);
92
74
  continue;
93
75
  }
76
+
94
77
  sendToMurphy(data.type, data.key);
95
78
  reportedKeys.add(dedupeKey);
96
79
  pendingErrors.delete(dedupeKey);
97
- flushedCount += 1;
98
80
  }
99
-
100
- debugLog('flush complete', {
101
- flushedCount,
102
- pendingRemaining: pendingErrors.size
103
- });
104
81
  }
105
82
 
106
83
  export function reportI18NError(type, key) {
107
- debugLog('reportI18NError called', { type, key });
108
84
  const dedupeKey = `${type}:${key}`;
85
+
109
86
  if (reportedKeys.has(dedupeKey)) {
110
- debugLog('reported i18n error deduped', { type, key });
111
87
  return;
112
88
  }
113
89
 
114
90
  if (!isMurphyAvailable()) {
115
- debugLog('murphy not available, queueing i18n error', { type, key });
116
91
  queuePendingError(dedupeKey, type, key);
117
92
  return;
118
93
  }
119
94
 
120
95
  flushI18NErrors();
96
+
121
97
  if (reportedKeys.has(dedupeKey)) {
122
- debugLog('reported i18n error deduped after flush', { type, key });
123
98
  return;
124
99
  }
125
100
 
126
101
  sendToMurphy(type, key);
127
102
  reportedKeys.add(dedupeKey);
128
103
  }
104
+
129
105
  export function clearReportedKeys() {
130
106
  reportedKeys.clear();
131
107
  pendingErrors.clear();
132
- debugLog('cleared reported and pending i18n errors');
133
108
  }
134
109
 
135
110
  export function getFallbackText(key) {
@@ -294,16 +294,21 @@ export function getI18NValue(i18n) {
294
294
  reportI18NError(I18N_ERROR_TYPES.UNDEFINED_OBJECT, 'i18n_object');
295
295
  return (key) => key;
296
296
  }
297
+
297
298
  return (key, values, localizedProps) => {
298
- const localizedValue = localizedProps ? getLocalizedValue(localizedProps) : localizedStringData[key];
299
+ const localizedValue = localizedProps
300
+ ? getLocalizedValue(localizedProps)
301
+ : localizedStringData[key];
299
302
  const finalKey = getMappedKey(key);
300
303
  let i18nStr = i18n[finalKey];
304
+
301
305
  if (i18nStr === undefined) {
302
306
  const isNumeric = /^\d+$/.test(finalKey);
303
- reportI18NError(
304
- isNumeric ? I18N_ERROR_TYPES.NUMERIC_FALLBACK : I18N_ERROR_TYPES.MISSING_KEY,
305
- finalKey
306
- );
307
+ const errorType = isNumeric
308
+ ? I18N_ERROR_TYPES.NUMERIC_FALLBACK
309
+ : I18N_ERROR_TYPES.MISSING_KEY;
310
+
311
+ reportI18NError(errorType, finalKey);
307
312
  return localizedValue || getFallbackText(finalKey);
308
313
  }
309
314
  i18nStr = replaceI18NValuesWithRegex(i18nStr, values);