@absolutesight/react-gettext 1.0.6 → 1.0.7

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/lib/Textdomain.js CHANGED
@@ -1,70 +1,70 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _react = require("react");
8
- var _propTypes = _interopRequireDefault(require("prop-types"));
9
- var _gettext = require("./gettext");
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
- class TextDomain extends _react.Component {
12
- getChildContext() {
13
- const self = this;
14
- return {
15
- gettext: self.gettext.bind(self),
16
- xgettext: self.xgettext.bind(self),
17
- ngettext: self.ngettext.bind(self),
18
- nxgettext: self.nxgettext.bind(self)
19
- };
20
- }
21
- gettext(message) {
22
- const {
23
- translations
24
- } = this.props;
25
- return (0, _gettext.gettext)(translations, message);
26
- }
27
- ngettext(singular, plural, n) {
28
- const {
29
- translations,
30
- plural: plurality
31
- } = this.props;
32
- return (0, _gettext.ngettext)(translations, plurality, singular, plural, n);
33
- }
34
- xgettext(message, context) {
35
- const {
36
- translations
37
- } = this.props;
38
- return (0, _gettext.xgettext)(translations, message, context);
39
- }
40
- nxgettext(singular, plural, n, context) {
41
- const {
42
- translations,
43
- plural: plurality
44
- } = this.props;
45
- return (0, _gettext.nxgettext)(translations, plurality, singular, plural, n, context);
46
- }
47
- render() {
48
- const {
49
- children
50
- } = this.props;
51
- return children;
52
- }
53
- }
54
- TextDomain.propTypes = {
55
- translations: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.objectOf(_propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.arrayOf(_propTypes.default.string)]))]),
56
- plural: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.string]),
57
- children: _propTypes.default.oneOfType([_propTypes.default.node, _propTypes.default.arrayOf(_propTypes.default.node)])
58
- };
59
- TextDomain.defaultProps = {
60
- translations: {},
61
- plural: 'n != 1',
62
- children: []
63
- };
64
- TextDomain.childContextTypes = {
65
- gettext: _propTypes.default.func,
66
- ngettext: _propTypes.default.func,
67
- xgettext: _propTypes.default.func,
68
- nxgettext: _propTypes.default.func
69
- };
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _react = require("react");
8
+ var _propTypes = _interopRequireDefault(require("prop-types"));
9
+ var _gettext = require("./gettext");
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+ class TextDomain extends _react.Component {
12
+ getChildContext() {
13
+ const self = this;
14
+ return {
15
+ gettext: self.gettext.bind(self),
16
+ xgettext: self.xgettext.bind(self),
17
+ ngettext: self.ngettext.bind(self),
18
+ nxgettext: self.nxgettext.bind(self)
19
+ };
20
+ }
21
+ gettext(message) {
22
+ const {
23
+ translations
24
+ } = this.props;
25
+ return (0, _gettext.gettext)(translations, message);
26
+ }
27
+ ngettext(singular, plural, n) {
28
+ const {
29
+ translations,
30
+ plural: plurality
31
+ } = this.props;
32
+ return (0, _gettext.ngettext)(translations, plurality, singular, plural, n);
33
+ }
34
+ xgettext(message, context) {
35
+ const {
36
+ translations
37
+ } = this.props;
38
+ return (0, _gettext.xgettext)(translations, message, context);
39
+ }
40
+ nxgettext(singular, plural, n, context) {
41
+ const {
42
+ translations,
43
+ plural: plurality
44
+ } = this.props;
45
+ return (0, _gettext.nxgettext)(translations, plurality, singular, plural, n, context);
46
+ }
47
+ render() {
48
+ const {
49
+ children
50
+ } = this.props;
51
+ return children;
52
+ }
53
+ }
54
+ TextDomain.propTypes = {
55
+ translations: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.objectOf(_propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.arrayOf(_propTypes.default.string)]))]),
56
+ plural: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.string]),
57
+ children: _propTypes.default.oneOfType([_propTypes.default.node, _propTypes.default.arrayOf(_propTypes.default.node)])
58
+ };
59
+ TextDomain.defaultProps = {
60
+ translations: {},
61
+ plural: 'n != 1',
62
+ children: []
63
+ };
64
+ TextDomain.childContextTypes = {
65
+ gettext: _propTypes.default.func,
66
+ ngettext: _propTypes.default.func,
67
+ xgettext: _propTypes.default.func,
68
+ nxgettext: _propTypes.default.func
69
+ };
70
70
  var _default = exports.default = TextDomain;
@@ -1,11 +1,11 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _react = _interopRequireDefault(require("react"));
8
- var _buildTextDomain = _interopRequireDefault(require("./buildTextDomain"));
9
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
- const TextDomainContext = /*#__PURE__*/_react.default.createContext((0, _buildTextDomain.default)({}, 'n != 1'));
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _react = _interopRequireDefault(require("react"));
8
+ var _buildTextDomain = _interopRequireDefault(require("./buildTextDomain"));
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+ const TextDomainContext = /*#__PURE__*/_react.default.createContext((0, _buildTextDomain.default)({}, 'n != 1'));
11
11
  var _default = exports.default = TextDomainContext;
@@ -0,0 +1,17 @@
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;
@@ -1,24 +1,24 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _gettext = require("./gettext");
8
- /**
9
- * TODO: We need to add description here
10
- * @param {object} translations JSON
11
- * @param {any} plural TODO: No idea obout type
12
- * @returns {object} JSON
13
- */
14
- function buildTextDomain() {
15
- let translations = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
16
- let plural = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "n != 1";
17
- return {
18
- gettext: _gettext.gettext.bind(null, translations),
19
- ngettext: _gettext.ngettext.bind(null, translations, plural),
20
- xgettext: _gettext.xgettext.bind(null, translations),
21
- nxgettext: _gettext.nxgettext.bind(null, translations, plural)
22
- };
23
- }
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _gettext = require("./gettext");
8
+ /**
9
+ * TODO: We need to add description here
10
+ * @param {object} translations JSON
11
+ * @param {any} plural TODO: No idea obout type
12
+ * @returns {object} JSON
13
+ */
14
+ function buildTextDomain() {
15
+ let translations = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
16
+ let plural = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "n != 1";
17
+ return {
18
+ gettext: _gettext.gettext.bind(null, translations),
19
+ ngettext: _gettext.ngettext.bind(null, translations, plural),
20
+ xgettext: _gettext.xgettext.bind(null, translations),
21
+ nxgettext: _gettext.nxgettext.bind(null, translations, plural)
22
+ };
23
+ }
24
24
  var _default = exports.default = buildTextDomain;
package/lib/gettext.js CHANGED
@@ -1,103 +1,103 @@
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
- return 0;
38
- }
39
-
40
- /**
41
- * TODO: We need to add description here
42
- * @param {string} catalog Catalog
43
- * @param {string} message Message
44
- * @returns {string} Message
45
- */
46
- export function gettext(catalog, message) {
47
- const messages = getTranslations(catalog);
48
- return Object.prototype.hasOwnProperty.call(messages, message) ? messages[message] : message;
49
- }
50
-
51
- /**
52
- * TODO: WE need to add description here
53
- * @param {string} catalog Catalog
54
- * @param {string} plurality Plurality
55
- * @param {string} singular Singular
56
- * @param {string} plural Plural
57
- * @param {number} n n
58
- * @returns {string} Message
59
- */
60
- export function ngettext(catalog, plurality, singular, plural, n) {
61
- const messages = getTranslations(catalog);
62
- const pluralIndex = getPluralForm(plurality, n);
63
- const defaultValue = n > 1 ? plural : singular;
64
- return Object.prototype.hasOwnProperty.call(messages, singular) && Array.isArray(messages[singular]) && messages[singular].length > pluralIndex && pluralIndex >= 0 ? messages[singular][pluralIndex] : defaultValue;
65
- }
66
-
67
- /**
68
- * TODO: We need to add description here
69
- * @param {string} catalog Catalog
70
- * @param {string} message Message
71
- * @param {string} context Context
72
- * @returns {string} Message
73
- */
74
- export function xgettext(catalog, message, context) {
75
- const messages = getTranslations(catalog);
76
- const key = context + DELIMITER + message;
77
- return Object.prototype.hasOwnProperty.call(messages, key) ? messages[key] : message;
78
- }
79
-
80
- /**
81
- * TODO: We need to add description here
82
- * @param {string} catalog Catalog
83
- * @param {string} plurality Plurality
84
- * @param {string} singular Singular
85
- * @param {string} plural Plural
86
- * @param {number} n n
87
- * @param {string} context Context
88
- * @returns {string} Message
89
- */
90
- export function nxgettext(catalog, plurality, singular, plural, n, context) {
91
- const messages = getTranslations(catalog);
92
- const pluralIndex = getPluralForm(plurality, n);
93
- const defaultValue = n > 1 ? plural : singular;
94
- const key = context + DELIMITER + singular;
95
- return Object.prototype.hasOwnProperty.call(messages, key) && Array.isArray(messages[key]) && messages[key].length > pluralIndex && pluralIndex >= 0 ? messages[key][pluralIndex] : defaultValue;
96
- }
97
- const allDomain = {
98
- gettext,
99
- ngettext,
100
- xgettext,
101
- nxgettext
102
- };
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
+ return 0;
38
+ }
39
+
40
+ /**
41
+ * TODO: We need to add description here
42
+ * @param {string} catalog Catalog
43
+ * @param {string} message Message
44
+ * @returns {string} Message
45
+ */
46
+ export function gettext(catalog, message) {
47
+ const messages = getTranslations(catalog);
48
+ return Object.prototype.hasOwnProperty.call(messages, message) ? messages[message] : message;
49
+ }
50
+
51
+ /**
52
+ * TODO: WE need to add description here
53
+ * @param {string} catalog Catalog
54
+ * @param {string} plurality Plurality
55
+ * @param {string} singular Singular
56
+ * @param {string} plural Plural
57
+ * @param {number} n n
58
+ * @returns {string} Message
59
+ */
60
+ export function ngettext(catalog, plurality, singular, plural, n) {
61
+ const messages = getTranslations(catalog);
62
+ const pluralIndex = getPluralForm(plurality, n);
63
+ const defaultValue = n > 1 ? plural : singular;
64
+ return Object.prototype.hasOwnProperty.call(messages, singular) && Array.isArray(messages[singular]) && messages[singular].length > pluralIndex && pluralIndex >= 0 ? messages[singular][pluralIndex] : defaultValue;
65
+ }
66
+
67
+ /**
68
+ * TODO: We need to add description here
69
+ * @param {string} catalog Catalog
70
+ * @param {string} message Message
71
+ * @param {string} context Context
72
+ * @returns {string} Message
73
+ */
74
+ export function xgettext(catalog, message, context) {
75
+ const messages = getTranslations(catalog);
76
+ const key = context + DELIMITER + message;
77
+ return Object.prototype.hasOwnProperty.call(messages, key) ? messages[key] : message;
78
+ }
79
+
80
+ /**
81
+ * TODO: We need to add description here
82
+ * @param {string} catalog Catalog
83
+ * @param {string} plurality Plurality
84
+ * @param {string} singular Singular
85
+ * @param {string} plural Plural
86
+ * @param {number} n n
87
+ * @param {string} context Context
88
+ * @returns {string} Message
89
+ */
90
+ export function nxgettext(catalog, plurality, singular, plural, n, context) {
91
+ const messages = getTranslations(catalog);
92
+ const pluralIndex = getPluralForm(plurality, n);
93
+ const defaultValue = n > 1 ? plural : singular;
94
+ const key = context + DELIMITER + singular;
95
+ return Object.prototype.hasOwnProperty.call(messages, key) && Array.isArray(messages[key]) && messages[key].length > pluralIndex && pluralIndex >= 0 ? messages[key][pluralIndex] : defaultValue;
96
+ }
97
+ const allDomain = {
98
+ gettext,
99
+ ngettext,
100
+ xgettext,
101
+ nxgettext
102
+ };
103
103
  export default allDomain;
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
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
- export { TextDomain, TextDomainContext, buildTextDomain };
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
+ export { TextDomain, TextDomainContext, buildTextDomain };
6
6
  export default withGettext;
@@ -1,27 +1,27 @@
1
- import React from 'react';
2
- import hoistNonReactStatic from 'hoist-non-react-statics';
3
- import TextDomain from './TextDomain.js';
4
- const withGettext = (translations = {}, pluralForm = 'n != 1', options = {}) => WrappedComponent => {
5
- const args = Object.assign({
6
- withRef: false
7
- }, options);
8
- class WithGettext extends TextDomain {
9
- getWrappedComponent() {
10
- return this.refs.wrappedComponent;
11
- }
12
- render() {
13
- const newprops = Object.assign({}, this.props);
14
- if (args.withRef) {
15
- newprops.ref = 'wrappedComponent';
16
- }
17
- return React.createElement(WrappedComponent, newprops);
18
- }
19
- }
20
- WithGettext.defaultProps = {
21
- translations,
22
- plural: pluralForm
23
- };
24
- WithGettext.displayName = `withGettext(${WrappedComponent.displayName || WrappedComponent.name || 'Component'})`;
25
- return hoistNonReactStatic(WithGettext, WrappedComponent);
26
- };
1
+ import React from 'react';
2
+ import hoistNonReactStatic from 'hoist-non-react-statics';
3
+ import TextDomain from './TextDomain.js';
4
+ const withGettext = (translations = {}, pluralForm = 'n != 1', options = {}) => WrappedComponent => {
5
+ const args = Object.assign({
6
+ withRef: false
7
+ }, options);
8
+ class WithGettext extends TextDomain {
9
+ getWrappedComponent() {
10
+ return this.refs.wrappedComponent;
11
+ }
12
+ render() {
13
+ const newprops = Object.assign({}, this.props);
14
+ if (args.withRef) {
15
+ newprops.ref = 'wrappedComponent';
16
+ }
17
+ return React.createElement(WrappedComponent, newprops);
18
+ }
19
+ }
20
+ WithGettext.defaultProps = {
21
+ translations,
22
+ plural: pluralForm
23
+ };
24
+ WithGettext.displayName = `withGettext(${WrappedComponent.displayName || WrappedComponent.name || 'Component'})`;
25
+ return hoistNonReactStatic(WithGettext, WrappedComponent);
26
+ };
27
27
  export default withGettext;