@zohodesk/i18n 1.0.0-beta.25 → 1.0.0-beta.27

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 (52) hide show
  1. package/README.md +109 -101
  2. package/es/components/I18NProvider.js +5 -1
  3. package/es/components/__tests__/__snapshots__/DateTimeDiffFormat.spec.js.snap +258 -258
  4. package/es/components/__tests__/__snapshots__/FormatText.spec.js.snap +17 -17
  5. package/es/components/__tests__/__snapshots__/HOCI18N.spec.js.snap +15 -15
  6. package/es/components/__tests__/__snapshots__/I18N.spec.js.snap +17 -17
  7. package/es/components/__tests__/__snapshots__/I18NProvider.spec.js.snap +13 -13
  8. package/es/components/__tests__/__snapshots__/PluralFormat.spec.js.snap +17 -17
  9. package/es/components/__tests__/__snapshots__/UserTimeDiffFormat.spec.js.snap +366 -366
  10. package/es/index.js +1 -1
  11. package/es/utils/__tests__/jsxTranslations.spec.js +174 -0
  12. package/es/{utils.js → utils/index.js} +59 -6
  13. package/es/utils/jsxTranslations.js +193 -0
  14. package/lib/components/I18NProvider.js +6 -1
  15. package/lib/components/__tests__/__snapshots__/DateTimeDiffFormat.spec.js.snap +258 -258
  16. package/lib/components/__tests__/__snapshots__/FormatText.spec.js.snap +17 -17
  17. package/lib/components/__tests__/__snapshots__/HOCI18N.spec.js.snap +15 -15
  18. package/lib/components/__tests__/__snapshots__/I18N.spec.js.snap +17 -17
  19. package/lib/components/__tests__/__snapshots__/I18NProvider.spec.js.snap +13 -13
  20. package/lib/components/__tests__/__snapshots__/PluralFormat.spec.js.snap +17 -17
  21. package/lib/components/__tests__/__snapshots__/UserTimeDiffFormat.spec.js.snap +366 -366
  22. package/lib/index.js +6 -0
  23. package/lib/utils/__tests__/jsxTranslations.spec.js +183 -0
  24. package/lib/{utils.js → utils/index.js} +64 -6
  25. package/lib/utils/jsxTranslations.js +242 -0
  26. package/package.json +30 -29
  27. package/src/I18NContext.js +2 -2
  28. package/src/components/DateTimeDiffFormat.js +256 -256
  29. package/src/components/FormatText.js +14 -14
  30. package/src/components/HOCI18N.js +37 -37
  31. package/src/components/I18N.js +74 -74
  32. package/src/components/I18NProvider.js +116 -110
  33. package/src/components/PluralFormat.js +37 -37
  34. package/src/components/UserTimeDiffFormat.js +97 -97
  35. package/src/components/__tests__/DateTimeDiffFormat.spec.js +618 -618
  36. package/src/components/__tests__/FormatText.spec.js +26 -26
  37. package/src/components/__tests__/HOCI18N.spec.js +33 -33
  38. package/src/components/__tests__/I18N.spec.js +29 -29
  39. package/src/components/__tests__/I18NProvider.spec.js +65 -65
  40. package/src/components/__tests__/PluralFormat.spec.js +27 -27
  41. package/src/components/__tests__/UserTimeDiffFormat.spec.js +1076 -1076
  42. package/src/components/__tests__/__snapshots__/DateTimeDiffFormat.spec.js.snap +258 -258
  43. package/src/components/__tests__/__snapshots__/FormatText.spec.js.snap +17 -17
  44. package/src/components/__tests__/__snapshots__/HOCI18N.spec.js.snap +15 -15
  45. package/src/components/__tests__/__snapshots__/I18N.spec.js.snap +17 -17
  46. package/src/components/__tests__/__snapshots__/I18NProvider.spec.js.snap +13 -13
  47. package/src/components/__tests__/__snapshots__/PluralFormat.spec.js.snap +17 -17
  48. package/src/components/__tests__/__snapshots__/UserTimeDiffFormat.spec.js.snap +366 -366
  49. package/src/index.js +37 -36
  50. package/src/utils/__tests__/jsxTranslations.spec.js +213 -0
  51. package/src/{utils.js → utils/index.js} +632 -585
  52. package/src/utils/jsxTranslations.js +180 -0
@@ -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
+ }