@zohodesk/i18n 1.0.0-beta.3 → 1.0.0-beta.30

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.
Files changed (60) hide show
  1. package/README.md +116 -2
  2. package/es/I18NContext.js +1 -2
  3. package/es/components/DateTimeDiffFormat.js +192 -200
  4. package/es/components/FormatText.js +4 -25
  5. package/es/components/HOCI18N.js +33 -45
  6. package/es/components/I18N.js +48 -63
  7. package/es/components/I18NProvider.js +59 -85
  8. package/es/components/PluralFormat.js +29 -48
  9. package/es/components/UserTimeDiffFormat.js +65 -74
  10. package/es/components/__tests__/DateTimeDiffFormat.spec.js +868 -657
  11. package/es/components/__tests__/FormatText.spec.js +20 -17
  12. package/es/components/__tests__/HOCI18N.spec.js +18 -22
  13. package/es/components/__tests__/I18N.spec.js +20 -19
  14. package/es/components/__tests__/I18NProvider.spec.js +36 -45
  15. package/es/components/__tests__/PluralFormat.spec.js +20 -17
  16. package/es/components/__tests__/UserTimeDiffFormat.spec.js +1343 -1095
  17. package/es/index.js +2 -6
  18. package/es/utils/__tests__/jsxTranslations.spec.js +174 -0
  19. package/es/utils/index.js +592 -0
  20. package/es/utils/jsxTranslations.js +193 -0
  21. package/lib/I18NContext.js +6 -6
  22. package/lib/components/DateTimeDiffFormat.js +151 -123
  23. package/lib/components/FormatText.js +32 -22
  24. package/lib/components/HOCI18N.js +47 -23
  25. package/lib/components/I18N.js +62 -36
  26. package/lib/components/I18NProvider.js +82 -70
  27. package/lib/components/PluralFormat.js +42 -32
  28. package/lib/components/UserTimeDiffFormat.js +79 -56
  29. package/lib/components/__tests__/DateTimeDiffFormat.spec.js +815 -629
  30. package/lib/components/__tests__/FormatText.spec.js +23 -25
  31. package/lib/components/__tests__/HOCI18N.spec.js +26 -34
  32. package/lib/components/__tests__/I18N.spec.js +21 -26
  33. package/lib/components/__tests__/I18NProvider.spec.js +43 -51
  34. package/lib/components/__tests__/PluralFormat.spec.js +24 -28
  35. package/lib/components/__tests__/UserTimeDiffFormat.spec.js +1256 -1039
  36. package/lib/index.js +85 -110
  37. package/lib/utils/__tests__/jsxTranslations.spec.js +183 -0
  38. package/lib/utils/index.js +658 -0
  39. package/lib/utils/jsxTranslations.js +242 -0
  40. package/package.json +3 -2
  41. package/src/components/DateTimeDiffFormat.js +86 -55
  42. package/src/components/I18N.js +2 -0
  43. package/src/components/I18NProvider.js +34 -25
  44. package/src/components/UserTimeDiffFormat.js +24 -18
  45. package/src/index.js +7 -9
  46. package/src/utils/__tests__/jsxTranslations.spec.js +213 -0
  47. package/src/utils/index.js +632 -0
  48. package/src/utils/jsxTranslations.js +180 -0
  49. package/es/components/NewDateFormat.js +0 -50
  50. package/es/offset.js +0 -629
  51. package/es/timezones.js +0 -118
  52. package/es/utils.js +0 -621
  53. package/lib/components/NewDateFormat.js +0 -68
  54. package/lib/offset.js +0 -634
  55. package/lib/timezones.js +0 -129
  56. package/lib/utils.js +0 -651
  57. package/src/components/NewDateFormat.js +0 -60
  58. package/src/offset.js +0 -629
  59. package/src/timezones.js +0 -113
  60. package/src/utils.js +0 -648
@@ -0,0 +1,180 @@
1
+ import React from 'react';
2
+ import {
3
+ unescapeUnicode
4
+ } from './index';
5
+
6
+ export function splitMatchedExp({
7
+ expression,
8
+ charLenToOmit,
9
+ string
10
+ }) {
11
+ let splitString = string.split(new RegExp(expression, 'g'));
12
+ let matchedExpAll = string.matchAll(new RegExp(expression, 'g'));
13
+ let matchedExpKeys = [];
14
+ for(let match of matchedExpAll) {
15
+ const value = match[0];
16
+ matchedExpKeys.push(value.substring(charLenToOmit, value.length - charLenToOmit));
17
+ }
18
+ return {
19
+ splitString,
20
+ matchedExpKeys
21
+ }
22
+ }
23
+
24
+ export function generateChildren({
25
+ children,
26
+ child
27
+ }) {
28
+ let newChildren = [], stringPlaceHolders = {};
29
+
30
+ const handleString = (string) => {
31
+ if(string) {
32
+ let {
33
+ splitString,
34
+ matchedExpKeys
35
+ } = splitMatchedExp({
36
+ expression: '{[0-9]+?}',
37
+ charLenToOmit: 1,
38
+ string
39
+ });
40
+ stringPlaceHolders[newChildren.length] = matchedExpKeys;
41
+ newChildren.push(splitString);
42
+ }
43
+ }
44
+
45
+ let {
46
+ splitString: splitChild,
47
+ matchedExpKeys: childrenKeys
48
+ } = splitMatchedExp({
49
+ expression: '<[A-Za-z0-9]+?>',
50
+ charLenToOmit: 1,
51
+ string: child
52
+ });
53
+ childrenKeys.forEach((childKey, childIndex) => {
54
+ const matchedChild = splitChild[childIndex];
55
+ handleString(matchedChild);
56
+ newChildren.push(children[childKey]);
57
+ });
58
+ handleString(splitChild[childrenKeys.length]);
59
+ return {
60
+ newChildren,
61
+ stringPlaceHolders
62
+ }
63
+ }
64
+
65
+ export function createElement({
66
+ componentId,
67
+ children = [],
68
+ stringPlaceHolders = {}
69
+ } = {}) {
70
+ return ({
71
+ components = {},
72
+ values = []
73
+ } = {}) => {
74
+ const {
75
+ type = React.Fragment,
76
+ props
77
+ } = components[componentId] || {};
78
+ const childElements = children.map((child, index) => {
79
+ if(typeof child === 'function') {
80
+ return child({components, values});
81
+ }
82
+ let string = '', stringIndex = 0;
83
+ (stringPlaceHolders[index] || []).map((stringId, index) => {
84
+ if(child[index]) {
85
+ string += child[index];
86
+ }
87
+ string += values[stringId];
88
+ stringIndex++;
89
+ });
90
+ if(child[stringIndex]) {
91
+ string += child[stringIndex]
92
+ }
93
+ return string;
94
+ });
95
+ return React.createElement(type, props, ...childElements)
96
+ }
97
+ };
98
+
99
+ export function replaceWithComponentPlaceHolder({
100
+ componentId,
101
+ i18nKey,
102
+ childrenLength
103
+ }) {
104
+ const closingTagIndex = i18nKey.indexOf(`</${componentId}>`);
105
+ const childWithOpenTag = i18nKey.substring(0, closingTagIndex);
106
+ const openTagIndex = childWithOpenTag.lastIndexOf(`<${componentId}>`);
107
+
108
+ return i18nKey.substring(0, openTagIndex) + `<0${childrenLength}${componentId}>` + i18nKey.substring(
109
+ closingTagIndex +`${componentId}`.length+ 3
110
+ )
111
+ }
112
+
113
+ export function prepareI18NFunc({
114
+ i18nKey,
115
+ children = {}
116
+ } = {}) {
117
+ const componentIdMatch = i18nKey.match(/<\/[A-Za-z0-9]+?>/);
118
+ if(componentIdMatch) {
119
+ const componentId = componentIdMatch[0].substring(2, componentIdMatch[0].length - 1);
120
+ const childWithOpenTag = (i18nKey.match(new RegExp(`(.*?)(?=<\/${componentId}>)`)) || [])[0] || '';
121
+ const child = childWithOpenTag.split(`<${componentId}>`).pop();
122
+ const {
123
+ newChildren,
124
+ stringPlaceHolders
125
+ } = generateChildren({
126
+ children,
127
+ child
128
+ });
129
+ const childrenLength = Object.keys(children).length;
130
+ children[`0${childrenLength}${componentId}`] = createElement({
131
+ componentId,
132
+ children: newChildren,
133
+ stringPlaceHolders
134
+ });
135
+ i18nKey = replaceWithComponentPlaceHolder({
136
+ componentId,
137
+ i18nKey,
138
+ childrenLength
139
+ });
140
+ return prepareI18NFunc({
141
+ i18nKey,
142
+ children
143
+ });
144
+ }
145
+ const {
146
+ newChildren,
147
+ stringPlaceHolders
148
+ } = generateChildren({
149
+ child: i18nKey,
150
+ children
151
+ });
152
+ return createElement({
153
+ componentId: -1,
154
+ children: newChildren,
155
+ stringPlaceHolders
156
+ });
157
+ }
158
+
159
+ export function getI18NComponent(i18n) {
160
+ if(typeof i18n === 'undefined') {
161
+ return (key) => key;
162
+ }
163
+ return (key) => {
164
+ let i18nStr = i18n[key];
165
+ if (i18nStr === undefined) {
166
+ return key;
167
+ }
168
+ if(typeof i18nStr === 'string') {
169
+ i18nStr = unescapeUnicode(i18nStr);
170
+ const value = prepareI18NFunc({
171
+ i18nKey: i18nStr
172
+ });
173
+ window.loadI18nChunk && window.loadI18nChunk({
174
+ [key] : value
175
+ });
176
+ i18nStr = value;
177
+ }
178
+ return i18nStr;
179
+ }
180
+ }
@@ -1,50 +0,0 @@
1
- import React from 'react';
2
- import PropTypes from 'prop-types';
3
-
4
- import { getFormatedDate } from '../utils';
5
- import FormatText from './FormatText';
6
- import { I18NContext } from '../I18NContext';
7
-
8
- export default function NewDateFormat(props, context) {
9
- var currentTime = new Date();
10
- var to = props.to,
11
- today = props.today,
12
- yesterday = props.yesterday,
13
- tomorrow = props.tomorrow,
14
- others = props.others,
15
- ago = props.ago,
16
- later = props.later,
17
- format = props.format,
18
- titleFormat = props.titleFormat,
19
- titleString = props.titleString,
20
- className = props.className,
21
- displayType = props.displayType,
22
- calculateFrom = props.calculateFrom;
23
-
24
- calculateFrom = calculateFrom === undefined ? currentTime : calculateFrom;
25
- var tzData = context.tzData;
26
-
27
- var formatResponse = getFormatedDate(to, today, yesterday, tomorrow, others, ago, later, format, titleFormat, titleString, calculateFrom, tzData);
28
- var isI18nValue = formatResponse.isFormatText;
29
- if (isI18nValue) {
30
- var i18nText = formatResponse.i18nText,
31
- values = formatResponse.values,
32
- _title = formatResponse.title;
33
-
34
- return React.createElement(FormatText, {
35
- i18NKey: i18nText,
36
- values: values,
37
- className: className,
38
- 'data-title': _title
39
- });
40
- }
41
- var title = formatResponse.title,
42
- formatedDate = formatResponse.formatedDate;
43
-
44
- return React.createElement(
45
- 'span',
46
- { className: className, 'data-title': title },
47
- formatedDate
48
- );
49
- }
50
- NewDateFormat.contextType = I18NContext;