@zohodesk/i18n 1.0.0-beta.24 → 1.0.0-beta.26

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.
package/README.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # i18n
2
2
 
3
+ # 1.0.0-beta.26
4
+
5
+ - Localization feature enhancement in i18n method.
6
+
7
+ # 1.0.0-beta.25
8
+
9
+ - July i18n key correction
10
+
3
11
  # 1.0.0-beta.24
4
12
 
5
13
  - I18N support for Days, Months and 12-hour time variables.
package/es/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { formatDate, pad, replaceI18NValuesWithRegex, unescapeUnicode, getValues, getI18NInfo, isToday, isYesterday, isTomorrow, isWithinAWeek, isTwoWeeksOrMore, userDateFormat, getDiffObj, getLyears, getSuffix, getDatePatternWithoutYear, setLocalizedData, dayi18n, monthi18n, timei18n } from './utils';
1
+ export { formatDate, pad, replaceI18NValuesWithRegex, unescapeUnicode, getValues, getI18NInfo, isToday, isYesterday, isTomorrow, isWithinAWeek, isTwoWeeksOrMore, userDateFormat, getDiffObj, getLyears, getSuffix, getDatePatternWithoutYear, setLocalizedData, setI18NKeyMapping, dayi18n, monthi18n, timei18n } from './utils';
2
2
  import { getI18NValue as getI18NValue1 } from './utils';
3
3
  export { I18NContext } from './I18NContext';
4
4
  export { default as I18NProvider, i18NProviderUtils } from './components/I18NProvider';
package/es/utils.js CHANGED
@@ -35,7 +35,7 @@ export const monthi18n = {
35
35
  'April': 'deskreact.calendar.monthname.april',
36
36
  'May': 'deskreact.calendar.monthname.may',
37
37
  'June': 'deskreact.calendar.monthname.june',
38
- 'July': 'deskreact.calendar.monthname.July',
38
+ 'July': 'deskreact.calendar.monthname.july',
39
39
  'August': 'deskreact.calendar.monthname.august',
40
40
  'September': 'deskreact.calendar.monthname.september',
41
41
  'October': 'deskreact.calendar.monthname.october',
@@ -176,9 +176,38 @@ export function getValues() {
176
176
  let diff = arguments.length > 1 ? arguments[1] : undefined;
177
177
  return params.map(param => diff[param]);
178
178
  }
179
+ export function isObject(item) {
180
+ return item && typeof item === 'object' && !Array.isArray(item);
181
+ }
179
182
  let localizedData = {};
183
+ let localizedStringData = {};
184
+
185
+ let getMappedKey = key => key;
186
+
187
+ function setLocalizedStringData(data) {
188
+ let keyString = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
189
+
190
+ if (typeof data === 'string') {
191
+ localizedStringData[keyString] = data;
192
+ return;
193
+ }
194
+
195
+ const keys = Object.keys(data);
196
+
197
+ for (let i = 0; i < keys.length; i++) {
198
+ const key = keys[i];
199
+ const newKeyString = keyString ? `${keyString}.${key}` : key;
200
+ setLocalizedStringData(data[key], newKeyString);
201
+ }
202
+ }
203
+
180
204
  export function setLocalizedData(data) {
181
- localizedData = data;
205
+ const translationData = isObject(data) ? data : {};
206
+ localizedData = translationData;
207
+ setLocalizedStringData(translationData);
208
+ }
209
+ export function setI18NKeyMapping(func) {
210
+ getMappedKey = func;
182
211
  }
183
212
  export function getLocalizedValue() {
184
213
  let {
@@ -214,11 +243,12 @@ export function getI18NValue(i18n) {
214
243
  }
215
244
 
216
245
  return (key, values, localizedProps) => {
217
- const localizedValue = localizedProps ? getLocalizedValue(localizedProps) : null;
218
- let i18nStr = i18n[key];
246
+ const localizedValue = localizedProps ? getLocalizedValue(localizedProps) : localizedStringData[key];
247
+ const finalKey = getMappedKey(key);
248
+ let i18nStr = i18n[finalKey];
219
249
 
220
250
  if (i18nStr === undefined) {
221
- return localizedValue || key;
251
+ return localizedValue || finalKey;
222
252
  }
223
253
 
224
254
  i18nStr = replaceI18NValuesWithRegex(i18nStr, values);
package/lib/index.js CHANGED
@@ -156,6 +156,12 @@ Object.defineProperty(exports, "replaceI18NValuesWithRegex", {
156
156
  return _utils.replaceI18NValuesWithRegex;
157
157
  }
158
158
  });
159
+ Object.defineProperty(exports, "setI18NKeyMapping", {
160
+ enumerable: true,
161
+ get: function get() {
162
+ return _utils.setI18NKeyMapping;
163
+ }
164
+ });
159
165
  Object.defineProperty(exports, "setLocalizedData", {
160
166
  enumerable: true,
161
167
  get: function get() {
package/lib/utils.js CHANGED
@@ -13,6 +13,7 @@ exports.getLocalizedValue = getLocalizedValue;
13
13
  exports.getLyears = getLyears;
14
14
  exports.getSuffix = getSuffix;
15
15
  exports.getValues = getValues;
16
+ exports.isObject = isObject;
16
17
  exports.isToday = isToday;
17
18
  exports.isTomorrow = isTomorrow;
18
19
  exports.isTwoWeeksOrMore = isTwoWeeksOrMore;
@@ -21,6 +22,7 @@ exports.isYesterday = isYesterday;
21
22
  exports.monthi18n = void 0;
22
23
  exports.pad = pad;
23
24
  exports.replaceI18NValuesWithRegex = replaceI18NValuesWithRegex;
25
+ exports.setI18NKeyMapping = setI18NKeyMapping;
24
26
  exports.setLocalizedData = setLocalizedData;
25
27
  exports.timei18n = void 0;
26
28
  exports.unescapeUnicode = unescapeUnicode;
@@ -72,7 +74,7 @@ var monthi18n = (_monthi18n = {
72
74
  'February': 'deskreact.calendar.monthname.february',
73
75
  'March': 'deskreact.calendar.monthname.march',
74
76
  'April': 'deskreact.calendar.monthname.april'
75
- }, _defineProperty(_monthi18n, "May", 'deskreact.calendar.monthname.may'), _defineProperty(_monthi18n, 'June', 'deskreact.calendar.monthname.june'), _defineProperty(_monthi18n, 'July', 'deskreact.calendar.monthname.July'), _defineProperty(_monthi18n, 'August', 'deskreact.calendar.monthname.august'), _defineProperty(_monthi18n, 'September', 'deskreact.calendar.monthname.september'), _defineProperty(_monthi18n, 'October', 'deskreact.calendar.monthname.october'), _defineProperty(_monthi18n, 'November', 'deskreact.calendar.monthname.november'), _defineProperty(_monthi18n, 'December', 'deskreact.calendar.monthname.december'), _monthi18n);
77
+ }, _defineProperty(_monthi18n, "May", 'deskreact.calendar.monthname.may'), _defineProperty(_monthi18n, 'June', 'deskreact.calendar.monthname.june'), _defineProperty(_monthi18n, 'July', 'deskreact.calendar.monthname.july'), _defineProperty(_monthi18n, 'August', 'deskreact.calendar.monthname.august'), _defineProperty(_monthi18n, 'September', 'deskreact.calendar.monthname.september'), _defineProperty(_monthi18n, 'October', 'deskreact.calendar.monthname.october'), _defineProperty(_monthi18n, 'November', 'deskreact.calendar.monthname.november'), _defineProperty(_monthi18n, 'December', 'deskreact.calendar.monthname.december'), _monthi18n);
76
78
  exports.monthi18n = monthi18n;
77
79
  var timei18n = {
78
80
  'a': 'support.time.format.time.am.short.small',
@@ -217,10 +219,42 @@ function getValues() {
217
219
  });
218
220
  }
219
221
 
222
+ function isObject(item) {
223
+ return item && _typeof(item) === 'object' && !Array.isArray(item);
224
+ }
225
+
220
226
  var localizedData = {};
227
+ var localizedStringData = {};
228
+
229
+ var getMappedKey = function getMappedKey(key) {
230
+ return key;
231
+ };
232
+
233
+ function setLocalizedStringData(data) {
234
+ var keyString = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
235
+
236
+ if (typeof data === 'string') {
237
+ localizedStringData[keyString] = data;
238
+ return;
239
+ }
240
+
241
+ var keys = Object.keys(data);
242
+
243
+ for (var i = 0; i < keys.length; i++) {
244
+ var key = keys[i];
245
+ var newKeyString = keyString ? "".concat(keyString, ".").concat(key) : key;
246
+ setLocalizedStringData(data[key], newKeyString);
247
+ }
248
+ }
221
249
 
222
250
  function setLocalizedData(data) {
223
- localizedData = data;
251
+ var translationData = isObject(data) ? data : {};
252
+ localizedData = translationData;
253
+ setLocalizedStringData(translationData);
254
+ }
255
+
256
+ function setI18NKeyMapping(func) {
257
+ getMappedKey = func;
224
258
  }
225
259
 
226
260
  function getLocalizedValue() {
@@ -260,11 +294,12 @@ function getI18NValue(i18n) {
260
294
  }
261
295
 
262
296
  return function (key, values, localizedProps) {
263
- var localizedValue = localizedProps ? getLocalizedValue(localizedProps) : null;
264
- var i18nStr = i18n[key];
297
+ var localizedValue = localizedProps ? getLocalizedValue(localizedProps) : localizedStringData[key];
298
+ var finalKey = getMappedKey(key);
299
+ var i18nStr = i18n[finalKey];
265
300
 
266
301
  if (i18nStr === undefined) {
267
- return localizedValue || key;
302
+ return localizedValue || finalKey;
268
303
  }
269
304
 
270
305
  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.24",
3
+ "version": "1.0.0-beta.26",
4
4
  "main": "lib/index",
5
5
  "module": "es/index.js",
6
6
  "jsnext:main": "es/index.js",
package/src/index.js CHANGED
@@ -16,6 +16,7 @@ export {
16
16
  getSuffix,
17
17
  getDatePatternWithoutYear,
18
18
  setLocalizedData,
19
+ setI18NKeyMapping,
19
20
  dayi18n,
20
21
  monthi18n,
21
22
  timei18n
package/src/utils.js CHANGED
@@ -39,7 +39,7 @@ export const monthi18n = {
39
39
  'April': 'deskreact.calendar.monthname.april',
40
40
  'May': 'deskreact.calendar.monthname.may',
41
41
  'June': 'deskreact.calendar.monthname.june',
42
- 'July': 'deskreact.calendar.monthname.July',
42
+ 'July': 'deskreact.calendar.monthname.july',
43
43
  'August': 'deskreact.calendar.monthname.august',
44
44
  'September': 'deskreact.calendar.monthname.september',
45
45
  'October': 'deskreact.calendar.monthname.october',
@@ -214,11 +214,37 @@ export function getValues(params = [], diff) {
214
214
  return params.map((param) => diff[param]);
215
215
  }
216
216
 
217
+ export function isObject(item) {
218
+ return (item && typeof item === 'object' && !Array.isArray(item));
219
+ }
220
+
217
221
  let localizedData = {};
222
+ let localizedStringData = {};
223
+ let getMappedKey = (key) => key;
224
+
225
+ function setLocalizedStringData(data, keyString = '') {
226
+ if(typeof data === 'string') {
227
+ localizedStringData[keyString] = data;
228
+ return;
229
+ }
230
+ const keys = Object.keys(data);
231
+ for(let i = 0; i < keys.length; i++) {
232
+ const key = keys[i];
233
+ const newKeyString = keyString ? `${keyString}.${key}` : key;
234
+ setLocalizedStringData(data[key], newKeyString);
235
+ }
236
+ }
218
237
 
219
238
  export function setLocalizedData(data) {
220
- localizedData = data;
239
+ const translationData = isObject(data) ? data : {};
240
+ localizedData = translationData;
241
+ setLocalizedStringData(translationData);
242
+ }
243
+
244
+ export function setI18NKeyMapping(func) {
245
+ getMappedKey = func;
221
246
  }
247
+
222
248
  export function getLocalizedValue({
223
249
  type,
224
250
  moduleName,
@@ -247,10 +273,11 @@ export function getI18NValue(i18n) {
247
273
  return (key) => key;
248
274
  }
249
275
  return (key, values, localizedProps) => {
250
- const localizedValue = localizedProps ? getLocalizedValue(localizedProps) : null;
251
- let i18nStr = i18n[key];
276
+ const localizedValue = localizedProps ? getLocalizedValue(localizedProps) : localizedStringData[key];
277
+ const finalKey = getMappedKey(key);
278
+ let i18nStr = i18n[finalKey];
252
279
  if (i18nStr === undefined) {
253
- return localizedValue || key;
280
+ return localizedValue || finalKey;
254
281
  }
255
282
  i18nStr = replaceI18NValuesWithRegex(i18nStr, values);
256
283
  return localizedValue || unescapeUnicode(i18nStr);