@absolutesight/react-gettext 1.0.4 → 1.0.6
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/CHANGELOG.md +5 -5
- package/LICENSE +21 -21
- package/README.md +365 -365
- package/dist/react-gettext.js +267 -267
- package/lib/Textdomain.js +69 -69
- package/lib/TextdomainContext.js +10 -10
- package/lib/buildTextdomain.js +23 -23
- package/lib/gettext.js +102 -102
- package/lib/index.js +5 -5
- package/lib/withGettext.js +26 -26
- package/package.json +84 -84
- package/src/TextDomain.js +82 -82
- package/src/TextDomainContext.js +6 -6
- package/src/buildTextDomain.js +18 -18
- package/src/gettext.js +123 -123
- package/src/index.js +12 -12
- package/src/withGettext.js +35 -35
- package/dist/react-gettext.min.js +0 -1
- package/lib/TextDomain.js +0 -63
- package/lib/TextDomainContext.js +0 -4
- package/lib/buildTextDomain.js +0 -17
package/src/TextDomain.js
CHANGED
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
import { Component } from 'react';
|
|
2
|
-
import PropTypes from 'prop-types';
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
gettext,
|
|
6
|
-
ngettext,
|
|
7
|
-
xgettext,
|
|
8
|
-
nxgettext,
|
|
9
|
-
} from './gettext.js';
|
|
10
|
-
|
|
11
|
-
class TextDomain extends Component {
|
|
12
|
-
|
|
13
|
-
getChildContext() {
|
|
14
|
-
const self = this;
|
|
15
|
-
|
|
16
|
-
return {
|
|
17
|
-
gettext: self.gettext.bind(self),
|
|
18
|
-
xgettext: self.xgettext.bind(self),
|
|
19
|
-
ngettext: self.ngettext.bind(self),
|
|
20
|
-
nxgettext: self.nxgettext.bind(self),
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
gettext(message) {
|
|
25
|
-
const { translations } = this.props;
|
|
26
|
-
return gettext(translations, message);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
ngettext(singular, plural, n) {
|
|
30
|
-
const { translations, plural: plurality } = this.props;
|
|
31
|
-
return ngettext(translations, plurality, singular, plural, n);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
xgettext(message, context) {
|
|
35
|
-
const { translations } = this.props;
|
|
36
|
-
return xgettext(translations, message, context);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
nxgettext(singular, plural, n, context) {
|
|
40
|
-
const { translations, plural: plurality } = this.props;
|
|
41
|
-
return nxgettext(translations, plurality, singular, plural, n, context);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
render() {
|
|
45
|
-
const { children } = this.props;
|
|
46
|
-
return children;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
TextDomain.propTypes = {
|
|
52
|
-
translations: PropTypes.oneOfType([
|
|
53
|
-
PropTypes.func,
|
|
54
|
-
PropTypes.objectOf(PropTypes.oneOfType([
|
|
55
|
-
PropTypes.string,
|
|
56
|
-
PropTypes.arrayOf(PropTypes.string),
|
|
57
|
-
])),
|
|
58
|
-
]),
|
|
59
|
-
plural: PropTypes.oneOfType([
|
|
60
|
-
PropTypes.func,
|
|
61
|
-
PropTypes.string,
|
|
62
|
-
]),
|
|
63
|
-
children: PropTypes.oneOfType([
|
|
64
|
-
PropTypes.node,
|
|
65
|
-
PropTypes.arrayOf(PropTypes.node),
|
|
66
|
-
]),
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
TextDomain.defaultProps = {
|
|
70
|
-
translations: {},
|
|
71
|
-
plural: 'n != 1',
|
|
72
|
-
children: [],
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
TextDomain.childContextTypes = {
|
|
76
|
-
gettext: PropTypes.func,
|
|
77
|
-
ngettext: PropTypes.func,
|
|
78
|
-
xgettext: PropTypes.func,
|
|
79
|
-
nxgettext: PropTypes.func,
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
export default TextDomain;
|
|
1
|
+
import { Component } from 'react';
|
|
2
|
+
import PropTypes from 'prop-types';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
gettext,
|
|
6
|
+
ngettext,
|
|
7
|
+
xgettext,
|
|
8
|
+
nxgettext,
|
|
9
|
+
} from './gettext.js';
|
|
10
|
+
|
|
11
|
+
class TextDomain extends Component {
|
|
12
|
+
|
|
13
|
+
getChildContext() {
|
|
14
|
+
const self = this;
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
gettext: self.gettext.bind(self),
|
|
18
|
+
xgettext: self.xgettext.bind(self),
|
|
19
|
+
ngettext: self.ngettext.bind(self),
|
|
20
|
+
nxgettext: self.nxgettext.bind(self),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
gettext(message) {
|
|
25
|
+
const { translations } = this.props;
|
|
26
|
+
return gettext(translations, message);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
ngettext(singular, plural, n) {
|
|
30
|
+
const { translations, plural: plurality } = this.props;
|
|
31
|
+
return ngettext(translations, plurality, singular, plural, n);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
xgettext(message, context) {
|
|
35
|
+
const { translations } = this.props;
|
|
36
|
+
return xgettext(translations, message, context);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
nxgettext(singular, plural, n, context) {
|
|
40
|
+
const { translations, plural: plurality } = this.props;
|
|
41
|
+
return nxgettext(translations, plurality, singular, plural, n, context);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
render() {
|
|
45
|
+
const { children } = this.props;
|
|
46
|
+
return children;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
TextDomain.propTypes = {
|
|
52
|
+
translations: PropTypes.oneOfType([
|
|
53
|
+
PropTypes.func,
|
|
54
|
+
PropTypes.objectOf(PropTypes.oneOfType([
|
|
55
|
+
PropTypes.string,
|
|
56
|
+
PropTypes.arrayOf(PropTypes.string),
|
|
57
|
+
])),
|
|
58
|
+
]),
|
|
59
|
+
plural: PropTypes.oneOfType([
|
|
60
|
+
PropTypes.func,
|
|
61
|
+
PropTypes.string,
|
|
62
|
+
]),
|
|
63
|
+
children: PropTypes.oneOfType([
|
|
64
|
+
PropTypes.node,
|
|
65
|
+
PropTypes.arrayOf(PropTypes.node),
|
|
66
|
+
]),
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
TextDomain.defaultProps = {
|
|
70
|
+
translations: {},
|
|
71
|
+
plural: 'n != 1',
|
|
72
|
+
children: [],
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
TextDomain.childContextTypes = {
|
|
76
|
+
gettext: PropTypes.func,
|
|
77
|
+
ngettext: PropTypes.func,
|
|
78
|
+
xgettext: PropTypes.func,
|
|
79
|
+
nxgettext: PropTypes.func,
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export default TextDomain;
|
package/src/TextDomainContext.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import buildTextDomain from './buildTextDomain.js';
|
|
3
|
-
|
|
4
|
-
const TextDomainContext = React.createContext(buildTextDomain({}, 'n != 1'));
|
|
5
|
-
|
|
6
|
-
export default TextDomainContext;
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import buildTextDomain from './buildTextDomain.js';
|
|
3
|
+
|
|
4
|
+
const TextDomainContext = React.createContext(buildTextDomain({}, 'n != 1'));
|
|
5
|
+
|
|
6
|
+
export default TextDomainContext;
|
package/src/buildTextDomain.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { gettext, ngettext, nxgettext, xgettext } from "./gettext.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* TODO: We need to add description here
|
|
5
|
-
* @param {object} translations JSON
|
|
6
|
-
* @param {any} plural TODO: No idea obout type
|
|
7
|
-
* @returns {object} JSON
|
|
8
|
-
*/
|
|
9
|
-
function buildTextDomain(translations = {}, plural = "n != 1") {
|
|
10
|
-
return {
|
|
11
|
-
gettext: gettext.bind(null, translations),
|
|
12
|
-
ngettext: ngettext.bind(null, translations, plural),
|
|
13
|
-
xgettext: xgettext.bind(null, translations),
|
|
14
|
-
nxgettext: nxgettext.bind(null, translations, plural)
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export default buildTextDomain;
|
|
1
|
+
import { gettext, ngettext, nxgettext, xgettext } from "./gettext.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* TODO: We need to add description here
|
|
5
|
+
* @param {object} translations JSON
|
|
6
|
+
* @param {any} plural TODO: No idea obout type
|
|
7
|
+
* @returns {object} JSON
|
|
8
|
+
*/
|
|
9
|
+
function buildTextDomain(translations = {}, plural = "n != 1") {
|
|
10
|
+
return {
|
|
11
|
+
gettext: gettext.bind(null, translations),
|
|
12
|
+
ngettext: ngettext.bind(null, translations, plural),
|
|
13
|
+
xgettext: xgettext.bind(null, translations),
|
|
14
|
+
nxgettext: nxgettext.bind(null, translations, plural)
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default buildTextDomain;
|
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.js';
|
|
2
|
-
import TextDomain from './TextDomain.js';
|
|
3
|
-
import TextDomainContext from './TextDomainContext.js';
|
|
4
|
-
import buildTextDomain from './buildTextDomain.js';
|
|
5
|
-
|
|
6
|
-
export {
|
|
7
|
-
TextDomain,
|
|
8
|
-
TextDomainContext,
|
|
9
|
-
buildTextDomain,
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export default withGettext;
|
|
1
|
+
import withGettext from './withGettext.js';
|
|
2
|
+
import TextDomain from './TextDomain.js';
|
|
3
|
+
import TextDomainContext from './TextDomainContext.js';
|
|
4
|
+
import buildTextDomain from './buildTextDomain.js';
|
|
5
|
+
|
|
6
|
+
export {
|
|
7
|
+
TextDomain,
|
|
8
|
+
TextDomainContext,
|
|
9
|
+
buildTextDomain,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export default withGettext;
|
package/src/withGettext.js
CHANGED
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import hoistNonReactStatic from 'hoist-non-react-statics';
|
|
3
|
-
import TextDomain from './TextDomain.js';
|
|
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.js';
|
|
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 +0,0 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("React"),require("hoistNonReactStatic"),require("PropTypes")):"function"==typeof define&&define.amd?define(["React","hoistNonReactStatic","PropTypes"],e):"object"==typeof exports?exports.ReactGettext=e(require("React"),require("hoistNonReactStatic"),require("PropTypes")):t.ReactGettext=e(t.React,t.hoistNonReactStatic,t.PropTypes)}(self,((t,e,n)=>(()=>{"use strict";var r={262:t=>{t.exports=n},883:e=>{e.exports=t},419:t=>{t.exports=e}},o={};function s(t){var e=o[t];if(void 0!==e)return e.exports;var n=o[t]={exports:{}};return r[t](n,n.exports,s),n.exports}s.d=(t,e)=>{for(var n in e)s.o(e,n)&&!s.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:e[n]})},s.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),s.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var i={};return(()=>{s.r(i),s.d(i,{TextDomain:()=>x,TextDomainContext:()=>d,buildTextDomain:()=>f,default:()=>y});var t=s(883),e=s(419),n=s(262);function r(t){return"function"==typeof t?t():t}function o(t,e){return Number.isNaN(parseInt(e,10))?0:"function"==typeof t?t(e):"string"!=typeof t||t.match(/[^n0-9 !=?:%+-/*><&|()]/i)?0:+Function("n",`return ${t}`)(e)}function a(t,e){const n=r(t);return Object.prototype.hasOwnProperty.call(n,e)?n[e]:e}function p(t,e,n,s,i){const a=r(t),p=o(e,i),c=i>1?s:n;return Object.prototype.hasOwnProperty.call(a,n)&&Array.isArray(a[n])&&a[n].length>p&&p>=0?a[n][p]:c}function c(t,e,n){const o=r(t),s=n+""+e;return Object.prototype.hasOwnProperty.call(o,s)?o[s]:e}function u(t,e,n,s,i,a){const p=r(t),c=o(e,i),u=i>1?s:n,l=a+""+n;return Object.prototype.hasOwnProperty.call(p,l)&&Array.isArray(p[l])&&p[l].length>c&&c>=0?p[l][c]:u}class l extends t.Component{getChildContext(){const t=this;return{gettext:t.gettext.bind(t),xgettext:t.xgettext.bind(t),ngettext:t.ngettext.bind(t),nxgettext:t.nxgettext.bind(t)}}gettext(t){const{translations:e}=this.props;return a(e,t)}ngettext(t,e,n){const{translations:r,plural:o}=this.props;return p(r,o,t,e,n)}xgettext(t,e){const{translations:n}=this.props;return c(n,t,e)}nxgettext(t,e,n,r){const{translations:o,plural:s}=this.props;return u(o,s,t,e,n,r)}render(){const{children:t}=this.props;return t}}l.propTypes={translations:n.oneOfType([n.func,n.objectOf(n.oneOfType([n.string,n.arrayOf(n.string)]))]),plural:n.oneOfType([n.func,n.string]),children:n.oneOfType([n.node,n.arrayOf(n.node)])},l.defaultProps={translations:{},plural:"n != 1",children:[]},l.childContextTypes={gettext:n.func,ngettext:n.func,xgettext:n.func,nxgettext:n.func};const x=l,f=function(t={},e="n != 1"){return{gettext:a.bind(null,t),ngettext:p.bind(null,t,e),xgettext:c.bind(null,t),nxgettext:u.bind(null,t,e)}},d=t.createContext(f({},"n != 1")),y=(n={},r="n != 1",o={})=>s=>{const i=Object.assign({withRef:!1},o);class a extends x{getWrappedComponent(){return this.refs.wrappedComponent}render(){const e=Object.assign({},this.props);return i.withRef&&(e.ref="wrappedComponent"),t.createElement(s,e)}}return a.defaultProps={translations:n,plural:r},a.displayName=`withGettext(${s.displayName||s.name||"Component"})`,e(a,s)}})(),i})()));
|
package/lib/TextDomain.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { Component } from 'react';
|
|
2
|
-
import PropTypes from 'prop-types';
|
|
3
|
-
import { gettext, ngettext, xgettext, nxgettext } from './gettext.js';
|
|
4
|
-
class TextDomain extends Component {
|
|
5
|
-
getChildContext() {
|
|
6
|
-
const self = this;
|
|
7
|
-
return {
|
|
8
|
-
gettext: self.gettext.bind(self),
|
|
9
|
-
xgettext: self.xgettext.bind(self),
|
|
10
|
-
ngettext: self.ngettext.bind(self),
|
|
11
|
-
nxgettext: self.nxgettext.bind(self)
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
gettext(message) {
|
|
15
|
-
const {
|
|
16
|
-
translations
|
|
17
|
-
} = this.props;
|
|
18
|
-
return gettext(translations, message);
|
|
19
|
-
}
|
|
20
|
-
ngettext(singular, plural, n) {
|
|
21
|
-
const {
|
|
22
|
-
translations,
|
|
23
|
-
plural: plurality
|
|
24
|
-
} = this.props;
|
|
25
|
-
return ngettext(translations, plurality, singular, plural, n);
|
|
26
|
-
}
|
|
27
|
-
xgettext(message, context) {
|
|
28
|
-
const {
|
|
29
|
-
translations
|
|
30
|
-
} = this.props;
|
|
31
|
-
return xgettext(translations, message, context);
|
|
32
|
-
}
|
|
33
|
-
nxgettext(singular, plural, n, context) {
|
|
34
|
-
const {
|
|
35
|
-
translations,
|
|
36
|
-
plural: plurality
|
|
37
|
-
} = this.props;
|
|
38
|
-
return nxgettext(translations, plurality, singular, plural, n, context);
|
|
39
|
-
}
|
|
40
|
-
render() {
|
|
41
|
-
const {
|
|
42
|
-
children
|
|
43
|
-
} = this.props;
|
|
44
|
-
return children;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
TextDomain.propTypes = {
|
|
48
|
-
translations: PropTypes.oneOfType([PropTypes.func, PropTypes.objectOf(PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]))]),
|
|
49
|
-
plural: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
|
|
50
|
-
children: PropTypes.oneOfType([PropTypes.node, PropTypes.arrayOf(PropTypes.node)])
|
|
51
|
-
};
|
|
52
|
-
TextDomain.defaultProps = {
|
|
53
|
-
translations: {},
|
|
54
|
-
plural: 'n != 1',
|
|
55
|
-
children: []
|
|
56
|
-
};
|
|
57
|
-
TextDomain.childContextTypes = {
|
|
58
|
-
gettext: PropTypes.func,
|
|
59
|
-
ngettext: PropTypes.func,
|
|
60
|
-
xgettext: PropTypes.func,
|
|
61
|
-
nxgettext: PropTypes.func
|
|
62
|
-
};
|
|
63
|
-
export default TextDomain;
|
package/lib/TextDomainContext.js
DELETED
package/lib/buildTextDomain.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { gettext, ngettext, nxgettext, xgettext } from "./gettext.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* TODO: We need to add description here
|
|
5
|
-
* @param {object} translations JSON
|
|
6
|
-
* @param {any} plural TODO: No idea obout type
|
|
7
|
-
* @returns {object} JSON
|
|
8
|
-
*/
|
|
9
|
-
function buildTextDomain(translations = {}, plural = "n != 1") {
|
|
10
|
-
return {
|
|
11
|
-
gettext: gettext.bind(null, translations),
|
|
12
|
-
ngettext: ngettext.bind(null, translations, plural),
|
|
13
|
-
xgettext: xgettext.bind(null, translations),
|
|
14
|
-
nxgettext: nxgettext.bind(null, translations, plural)
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
export default buildTextDomain;
|