@absolutesight/react-gettext 1.0.1 → 1.0.3

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/src/gettext.js CHANGED
@@ -1,123 +1,123 @@
1
- const DELIMITER = "\u0004"; // End of Transmission (EOT)
2
-
3
- /**
4
- * TODO: We need to add description here
5
- * @param {any} catalog string or function
6
- * @returns {string} string or string from a function
7
- */
8
- function getTranslations(catalog) {
9
- return typeof catalog === "function" ? catalog() : catalog;
10
- }
11
-
12
- /**
13
- * TODO: We need to add description here
14
- * @param {any} plural string or function
15
- * @param {any} n TODO: No idea what will pass in it
16
- * @returns {any} 0 if nothing
17
- */
18
- function getPluralForm(plural, n) {
19
- // return 0 if n is not integer
20
- if (Number.isNaN(parseInt(n, 10))) {
21
- return 0;
22
- }
23
-
24
- // if pluralForm is function, use it to get plural form index
25
- if (typeof plural === "function") {
26
- return plural(n);
27
- }
28
-
29
- // if pluralForm is string and contains only "n", "0-9", " ", "!=?:%+-/*><&|()"
30
- // characters, then we can "eval" it to calculate plural form
31
- if (typeof plural === "string" && !plural.match(/[^n0-9 !=?:%+-/*><&|()]/i)) {
32
- /* eslint-disable no-new-func */
33
- const calcPlural = Function("n", `return ${plural}`);
34
- /* eslint-enable no-new-func */
35
- return +calcPlural(n);
36
- }
37
-
38
- return 0;
39
- }
40
-
41
- /**
42
- * TODO: We need to add description here
43
- * @param {string} catalog Catalog
44
- * @param {string} message Message
45
- * @returns {string} Message
46
- */
47
- export function gettext(catalog, message) {
48
- const messages = getTranslations(catalog);
49
- return Object.prototype.hasOwnProperty.call(messages, message)
50
- ? messages[message]
51
- : message;
52
- }
53
-
54
- /**
55
- * TODO: WE need to add description here
56
- * @param {string} catalog Catalog
57
- * @param {string} plurality Plurality
58
- * @param {string} singular Singular
59
- * @param {string} plural Plural
60
- * @param {number} n n
61
- * @returns {string} Message
62
- */
63
- export function ngettext(catalog, plurality, singular, plural, n) {
64
- const messages = getTranslations(catalog);
65
- const pluralIndex = getPluralForm(plurality, n);
66
- const defaultValue = n > 1 ? plural : singular;
67
-
68
- return Object.prototype.hasOwnProperty.call(messages, singular) &&
69
- Array.isArray(messages[singular]) &&
70
- messages[singular].length > pluralIndex &&
71
- pluralIndex >= 0
72
- ? messages[singular][pluralIndex]
73
- : defaultValue;
74
- }
75
-
76
- /**
77
- * TODO: We need to add description here
78
- * @param {string} catalog Catalog
79
- * @param {string} message Message
80
- * @param {string} context Context
81
- * @returns {string} Message
82
- */
83
- export function xgettext(catalog, message, context) {
84
- const messages = getTranslations(catalog);
85
- const key = context + DELIMITER + message;
86
-
87
- return Object.prototype.hasOwnProperty.call(messages, key)
88
- ? messages[key]
89
- : message;
90
- }
91
-
92
- /**
93
- * TODO: We need to add description here
94
- * @param {string} catalog Catalog
95
- * @param {string} plurality Plurality
96
- * @param {string} singular Singular
97
- * @param {string} plural Plural
98
- * @param {number} n n
99
- * @param {string} context Context
100
- * @returns {string} Message
101
- */
102
- export function nxgettext(catalog, plurality, singular, plural, n, context) {
103
- const messages = getTranslations(catalog);
104
- const pluralIndex = getPluralForm(plurality, n);
105
- const defaultValue = n > 1 ? plural : singular;
106
- const key = context + DELIMITER + singular;
107
-
108
- return Object.prototype.hasOwnProperty.call(messages, key) &&
109
- Array.isArray(messages[key]) &&
110
- messages[key].length > pluralIndex &&
111
- pluralIndex >= 0
112
- ? messages[key][pluralIndex]
113
- : defaultValue;
114
- }
115
-
116
- const allDomain = {
117
- gettext,
118
- ngettext,
119
- xgettext,
120
- nxgettext
121
- };
122
-
123
- export default allDomain;
1
+ const DELIMITER = "\u0004"; // End of Transmission (EOT)
2
+
3
+ /**
4
+ * TODO: We need to add description here
5
+ * @param {any} catalog string or function
6
+ * @returns {string} string or string from a function
7
+ */
8
+ function getTranslations(catalog) {
9
+ return typeof catalog === "function" ? catalog() : catalog;
10
+ }
11
+
12
+ /**
13
+ * TODO: We need to add description here
14
+ * @param {any} plural string or function
15
+ * @param {any} n TODO: No idea what will pass in it
16
+ * @returns {any} 0 if nothing
17
+ */
18
+ function getPluralForm(plural, n) {
19
+ // return 0 if n is not integer
20
+ if (Number.isNaN(parseInt(n, 10))) {
21
+ return 0;
22
+ }
23
+
24
+ // if pluralForm is function, use it to get plural form index
25
+ if (typeof plural === "function") {
26
+ return plural(n);
27
+ }
28
+
29
+ // if pluralForm is string and contains only "n", "0-9", " ", "!=?:%+-/*><&|()"
30
+ // characters, then we can "eval" it to calculate plural form
31
+ if (typeof plural === "string" && !plural.match(/[^n0-9 !=?:%+-/*><&|()]/i)) {
32
+ /* eslint-disable no-new-func */
33
+ const calcPlural = Function("n", `return ${plural}`);
34
+ /* eslint-enable no-new-func */
35
+ return +calcPlural(n);
36
+ }
37
+
38
+ return 0;
39
+ }
40
+
41
+ /**
42
+ * TODO: We need to add description here
43
+ * @param {string} catalog Catalog
44
+ * @param {string} message Message
45
+ * @returns {string} Message
46
+ */
47
+ export function gettext(catalog, message) {
48
+ const messages = getTranslations(catalog);
49
+ return Object.prototype.hasOwnProperty.call(messages, message)
50
+ ? messages[message]
51
+ : message;
52
+ }
53
+
54
+ /**
55
+ * TODO: WE need to add description here
56
+ * @param {string} catalog Catalog
57
+ * @param {string} plurality Plurality
58
+ * @param {string} singular Singular
59
+ * @param {string} plural Plural
60
+ * @param {number} n n
61
+ * @returns {string} Message
62
+ */
63
+ export function ngettext(catalog, plurality, singular, plural, n) {
64
+ const messages = getTranslations(catalog);
65
+ const pluralIndex = getPluralForm(plurality, n);
66
+ const defaultValue = n > 1 ? plural : singular;
67
+
68
+ return Object.prototype.hasOwnProperty.call(messages, singular) &&
69
+ Array.isArray(messages[singular]) &&
70
+ messages[singular].length > pluralIndex &&
71
+ pluralIndex >= 0
72
+ ? messages[singular][pluralIndex]
73
+ : defaultValue;
74
+ }
75
+
76
+ /**
77
+ * TODO: We need to add description here
78
+ * @param {string} catalog Catalog
79
+ * @param {string} message Message
80
+ * @param {string} context Context
81
+ * @returns {string} Message
82
+ */
83
+ export function xgettext(catalog, message, context) {
84
+ const messages = getTranslations(catalog);
85
+ const key = context + DELIMITER + message;
86
+
87
+ return Object.prototype.hasOwnProperty.call(messages, key)
88
+ ? messages[key]
89
+ : message;
90
+ }
91
+
92
+ /**
93
+ * TODO: We need to add description here
94
+ * @param {string} catalog Catalog
95
+ * @param {string} plurality Plurality
96
+ * @param {string} singular Singular
97
+ * @param {string} plural Plural
98
+ * @param {number} n n
99
+ * @param {string} context Context
100
+ * @returns {string} Message
101
+ */
102
+ export function nxgettext(catalog, plurality, singular, plural, n, context) {
103
+ const messages = getTranslations(catalog);
104
+ const pluralIndex = getPluralForm(plurality, n);
105
+ const defaultValue = n > 1 ? plural : singular;
106
+ const key = context + DELIMITER + singular;
107
+
108
+ return Object.prototype.hasOwnProperty.call(messages, key) &&
109
+ Array.isArray(messages[key]) &&
110
+ messages[key].length > pluralIndex &&
111
+ pluralIndex >= 0
112
+ ? messages[key][pluralIndex]
113
+ : defaultValue;
114
+ }
115
+
116
+ const allDomain = {
117
+ gettext,
118
+ ngettext,
119
+ xgettext,
120
+ nxgettext
121
+ };
122
+
123
+ export default allDomain;
package/src/index.js CHANGED
@@ -1,12 +1,12 @@
1
- import withGettext from './withGettext';
2
- import TextDomain from './TextDomain';
3
- import TextDomainContext from './TextDomainContext';
4
- import buildTextDomain from './buildTextDomain';
5
-
6
- export {
7
- TextDomain,
8
- TextDomainContext,
9
- buildTextDomain,
10
- };
11
-
12
- export default withGettext;
1
+ import withGettext from './withGettext';
2
+ import TextDomain from './TextDomain';
3
+ import TextDomainContext from './TextDomainContext';
4
+ import buildTextDomain from './buildTextDomain';
5
+
6
+ export {
7
+ TextDomain,
8
+ TextDomainContext,
9
+ buildTextDomain,
10
+ };
11
+
12
+ export default withGettext;
@@ -1,35 +1,35 @@
1
- import React from 'react';
2
- import hoistNonReactStatic from 'hoist-non-react-statics';
3
- import TextDomain from './TextDomain';
4
-
5
- const withGettext = (translations = {}, pluralForm = 'n != 1', options = {}) => (WrappedComponent) => {
6
- const args = Object.assign({ withRef: false }, options);
7
-
8
- class WithGettext extends TextDomain {
9
-
10
- getWrappedComponent() {
11
- return this.refs.wrappedComponent;
12
- }
13
-
14
- render() {
15
- const newprops = Object.assign({}, this.props);
16
- if (args.withRef) {
17
- newprops.ref = 'wrappedComponent';
18
- }
19
-
20
- return React.createElement(WrappedComponent, newprops);
21
- }
22
-
23
- }
24
-
25
- WithGettext.defaultProps = {
26
- translations,
27
- plural: pluralForm,
28
- };
29
-
30
- WithGettext.displayName = `withGettext(${WrappedComponent.displayName || WrappedComponent.name || 'Component'})`;
31
-
32
- return hoistNonReactStatic(WithGettext, WrappedComponent);
33
- };
34
-
35
- export default withGettext;
1
+ import React from 'react';
2
+ import hoistNonReactStatic from 'hoist-non-react-statics';
3
+ import TextDomain from './TextDomain';
4
+
5
+ const withGettext = (translations = {}, pluralForm = 'n != 1', options = {}) => (WrappedComponent) => {
6
+ const args = Object.assign({ withRef: false }, options);
7
+
8
+ class WithGettext extends TextDomain {
9
+
10
+ getWrappedComponent() {
11
+ return this.refs.wrappedComponent;
12
+ }
13
+
14
+ render() {
15
+ const newprops = Object.assign({}, this.props);
16
+ if (args.withRef) {
17
+ newprops.ref = 'wrappedComponent';
18
+ }
19
+
20
+ return React.createElement(WrappedComponent, newprops);
21
+ }
22
+
23
+ }
24
+
25
+ WithGettext.defaultProps = {
26
+ translations,
27
+ plural: pluralForm,
28
+ };
29
+
30
+ WithGettext.displayName = `withGettext(${WrappedComponent.displayName || WrappedComponent.name || 'Component'})`;
31
+
32
+ return hoistNonReactStatic(WithGettext, WrappedComponent);
33
+ };
34
+
35
+ export default withGettext;