@absolutesight/react-gettext 0.0.1

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.
@@ -0,0 +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
+ }
24
+ var _default = exports.default = buildTextDomain;
@@ -0,0 +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
+ }
24
+ var _default = exports.default = buildTextdomain;
package/lib/gettext.js ADDED
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ exports.gettext = gettext;
8
+ exports.ngettext = ngettext;
9
+ exports.nxgettext = nxgettext;
10
+ exports.xgettext = xgettext;
11
+ const DELIMITER = "\u0004"; // End of Transmission (EOT)
12
+
13
+ /**
14
+ * TODO: We need to add description here
15
+ * @param {any} catalog string or function
16
+ * @returns {string} string or string from a function
17
+ */
18
+ function getTranslations(catalog) {
19
+ return typeof catalog === "function" ? catalog() : catalog;
20
+ }
21
+
22
+ /**
23
+ * TODO: We need to add description here
24
+ * @param {any} plural string or function
25
+ * @param {any} n TODO: No idea what will pass in it
26
+ * @returns {any} 0 if nothing
27
+ */
28
+ function getPluralForm(plural, n) {
29
+ // return 0 if n is not integer
30
+ if (Number.isNaN(parseInt(n, 10))) {
31
+ return 0;
32
+ }
33
+
34
+ // if pluralForm is function, use it to get plural form index
35
+ if (typeof plural === "function") {
36
+ return plural(n);
37
+ }
38
+
39
+ // if pluralForm is string and contains only "n", "0-9", " ", "!=?:%+-/*><&|()"
40
+ // characters, then we can "eval" it to calculate plural form
41
+ if (typeof plural === "string" && !plural.match(/[^n0-9 !=?:%+-/*><&|()]/i)) {
42
+ /* eslint-disable no-new-func */
43
+ const calcPlural = Function("n", `return ${plural}`);
44
+ /* eslint-enable no-new-func */
45
+ return +calcPlural(n);
46
+ }
47
+ return 0;
48
+ }
49
+
50
+ /**
51
+ * TODO: We need to add description here
52
+ * @param {string} catalog Catalog
53
+ * @param {string} message Message
54
+ * @returns {string} Message
55
+ */
56
+ function gettext(catalog, message) {
57
+ const messages = getTranslations(catalog);
58
+ return Object.prototype.hasOwnProperty.call(messages, message) ? messages[message] : message;
59
+ }
60
+
61
+ /**
62
+ * TODO: WE need to add description here
63
+ * @param {string} catalog Catalog
64
+ * @param {string} plurality Plurality
65
+ * @param {string} singular Singular
66
+ * @param {string} plural Plural
67
+ * @param {number} n n
68
+ * @returns {string} Message
69
+ */
70
+ function ngettext(catalog, plurality, singular, plural, n) {
71
+ const messages = getTranslations(catalog);
72
+ const pluralIndex = getPluralForm(plurality, n);
73
+ const defaultValue = n > 1 ? plural : singular;
74
+ return Object.prototype.hasOwnProperty.call(messages, singular) && Array.isArray(messages[singular]) && messages[singular].length > pluralIndex && pluralIndex >= 0 ? messages[singular][pluralIndex] : defaultValue;
75
+ }
76
+
77
+ /**
78
+ * TODO: We need to add description here
79
+ * @param {string} catalog Catalog
80
+ * @param {string} message Message
81
+ * @param {string} context Context
82
+ * @returns {string} Message
83
+ */
84
+ function xgettext(catalog, message, context) {
85
+ const messages = getTranslations(catalog);
86
+ const key = context + DELIMITER + message;
87
+ return Object.prototype.hasOwnProperty.call(messages, key) ? messages[key] : message;
88
+ }
89
+
90
+ /**
91
+ * TODO: We need to add description here
92
+ * @param {string} catalog Catalog
93
+ * @param {string} plurality Plurality
94
+ * @param {string} singular Singular
95
+ * @param {string} plural Plural
96
+ * @param {number} n n
97
+ * @param {string} context Context
98
+ * @returns {string} Message
99
+ */
100
+ function nxgettext(catalog, plurality, singular, plural, n, context) {
101
+ const messages = getTranslations(catalog);
102
+ const pluralIndex = getPluralForm(plurality, n);
103
+ const defaultValue = n > 1 ? plural : singular;
104
+ const key = context + DELIMITER + singular;
105
+ return Object.prototype.hasOwnProperty.call(messages, key) && Array.isArray(messages[key]) && messages[key].length > pluralIndex && pluralIndex >= 0 ? messages[key][pluralIndex] : defaultValue;
106
+ }
107
+ const allDomain = {
108
+ gettext,
109
+ ngettext,
110
+ xgettext,
111
+ nxgettext
112
+ };
113
+ var _default = exports.default = allDomain;
package/lib/index.js ADDED
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "TextDomain", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _TextDomain.default;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "TextDomainContext", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _TextDomainContext.default;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "buildTextDomain", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _buildTextDomain.default;
22
+ }
23
+ });
24
+ exports.default = void 0;
25
+ var _TextDomain = _interopRequireDefault(require("./TextDomain"));
26
+ var _TextDomainContext = _interopRequireDefault(require("./TextDomainContext"));
27
+ var _buildTextDomain = _interopRequireDefault(require("./buildTextDomain"));
28
+ var _withGettext = _interopRequireDefault(require("./withGettext"));
29
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30
+ var _default = exports.default = _withGettext.default;
@@ -0,0 +1,39 @@
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 _TextDomain = _interopRequireDefault(require("./TextDomain"));
9
+ var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics"));
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+ const withGettext = function () {
12
+ let translations = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
13
+ let pluralForm = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'n != 1';
14
+ let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
15
+ return WrappedComponent => {
16
+ const args = Object.assign({
17
+ withRef: false
18
+ }, options);
19
+ class WithGettext extends _TextDomain.default {
20
+ getWrappedComponent() {
21
+ return this.refs.wrappedComponent;
22
+ }
23
+ render() {
24
+ const newprops = Object.assign({}, this.props);
25
+ if (args.withRef) {
26
+ newprops.ref = 'wrappedComponent';
27
+ }
28
+ return /*#__PURE__*/_react.default.createElement(WrappedComponent, newprops);
29
+ }
30
+ }
31
+ WithGettext.defaultProps = {
32
+ translations,
33
+ plural: pluralForm
34
+ };
35
+ WithGettext.displayName = `withGettext(${WrappedComponent.displayName || WrappedComponent.name || 'Component'})`;
36
+ return (0, _hoistNonReactStatics.default)(WithGettext, WrappedComponent);
37
+ };
38
+ };
39
+ var _default = exports.default = withGettext;
package/package.json ADDED
@@ -0,0 +1,81 @@
1
+ {
2
+ "name": "@absolutesight/react-gettext",
3
+ "description": "Gettext implementation for React based project.",
4
+ "license": "MIT",
5
+ "author": "Qadeer Ahmad <qadeer@gmail.com>",
6
+ "homepage": "https://absolutesight.com",
7
+ "bugs": {
8
+ "url": "https://absolutesight.com"
9
+ },
10
+ "version": "0.0.1",
11
+ "main": "lib/index.js",
12
+ "files": [
13
+ "*.md",
14
+ "dist",
15
+ "LICENSE",
16
+ "lib",
17
+ "src"
18
+ ],
19
+ "keywords": [
20
+ "react",
21
+ "gettext",
22
+ "gettextjs"
23
+ ],
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git@github.com:eugene-manuilov/react-gettext.git"
27
+ },
28
+ "scripts": {
29
+ "build": "npm run build:commonjs & npm run build:umd & npm run build:umd:min",
30
+ "build:commonjs": "mkdir -p lib && babel ./src -d lib",
31
+ "build:umd": "webpack --output-filename=react-gettext.js",
32
+ "build:umd:min": "NODE_ENV=production webpack --output-filename=react-gettext.min.js",
33
+ "test": "BABEL_ENV=test jest",
34
+ "prepublish": "npm run build",
35
+ "lint": "eslint src --fix --max-warnings=0"
36
+ },
37
+ "devDependencies": {
38
+ "@babel/cli": "^7.23.9",
39
+ "@babel/core": "^7.23.9",
40
+ "@babel/plugin-proposal-private-property-in-object": "^7.21.11",
41
+ "@babel/preset-env": "^7.23.9",
42
+ "@babel/preset-react": "^7.23.3",
43
+ "babel-jest": "^29.7.0",
44
+ "babel-loader": "^9.1.3",
45
+ "enzyme": "^3.11.0",
46
+ "eslint": "^8.56.0",
47
+ "eslint-config-react-app": "^7.0.1",
48
+ "eslint-plugin-import": "^2.29.1",
49
+ "eslint-plugin-jsdoc": "^48.0.4",
50
+ "eslint-plugin-jsx-a11y": "^6.8.0",
51
+ "eslint-plugin-react": "^7.33.2",
52
+ "eslint-webpack-plugin": "^4.0.1",
53
+ "faker": "^6.6.6",
54
+ "jest": "^29.7.0",
55
+ "jest-enzyme": "^7.1.2",
56
+ "lodash": "^4.17.21",
57
+ "prop-types": "^15.8.1",
58
+ "react": "^18.2.0",
59
+ "react-dom": "^18.2.0",
60
+ "react-test-renderer": "^18.2.0",
61
+ "webpack": "^5.90.1",
62
+ "webpack-cli": "^5.1.4"
63
+ },
64
+ "dependencies": {
65
+ "hoist-non-react-statics": "^1.2.0 || ^2.0.0-0 || ^3.0.0-0"
66
+ },
67
+ "peerDependencies": {
68
+ "hoist-non-react-statics": "^1.2.0 || ^2.0.0-0 || ^3.0.0-0",
69
+ "prop-types": "^15.0.0-0 || ^16.0.0-0",
70
+ "react": "^15.0.0-0 || ^16.0.0-0 || ^17.0.0-0 || ^18.0.0-0"
71
+ },
72
+ "jest": {
73
+ "setupFilesAfterEnv": [
74
+ "<rootDir>/__tests__/__setup.js"
75
+ ],
76
+ "testMatch": [
77
+ "<rootDir>/__tests__/**/[^_]*.js"
78
+ ],
79
+ "verbose": true
80
+ }
81
+ }
@@ -0,0 +1,82 @@
1
+ import {gettext,
2
+ ngettext,
3
+ nxgettext,
4
+ xgettext,} from './gettext';
5
+ import { Component } from 'react';
6
+ import PropTypes from 'prop-types';
7
+
8
+
9
+
10
+ class TextDomain extends Component {
11
+
12
+ getChildContext() {
13
+ const self = this;
14
+
15
+ return {
16
+ gettext: self.gettext.bind(self),
17
+ xgettext: self.xgettext.bind(self),
18
+ ngettext: self.ngettext.bind(self),
19
+ nxgettext: self.nxgettext.bind(self),
20
+ };
21
+ }
22
+
23
+ gettext(message) {
24
+ const { translations } = this.props;
25
+ return gettext(translations, message);
26
+ }
27
+
28
+ ngettext(singular, plural, n) {
29
+ const { translations, plural: plurality } = this.props;
30
+ return ngettext(translations, plurality, singular, plural, n);
31
+ }
32
+
33
+ xgettext(message, context) {
34
+ const { translations } = this.props;
35
+ return xgettext(translations, message, context);
36
+ }
37
+
38
+ nxgettext(singular, plural, n, context) {
39
+ const { translations, plural: plurality } = this.props;
40
+ return nxgettext(translations, plurality, singular, plural, n, context);
41
+ }
42
+
43
+ render() {
44
+ const { children } = this.props;
45
+ return children;
46
+ }
47
+
48
+ }
49
+
50
+ TextDomain.propTypes = {
51
+ translations: PropTypes.oneOfType([
52
+ PropTypes.func,
53
+ PropTypes.objectOf(PropTypes.oneOfType([
54
+ PropTypes.string,
55
+ PropTypes.arrayOf(PropTypes.string),
56
+ ])),
57
+ ]),
58
+ plural: PropTypes.oneOfType([
59
+ PropTypes.func,
60
+ PropTypes.string,
61
+ ]),
62
+ children: PropTypes.oneOfType([
63
+ PropTypes.node,
64
+ PropTypes.arrayOf(PropTypes.node),
65
+ ]),
66
+ };
67
+
68
+ TextDomain.defaultProps = {
69
+ translations: {
70
+ },
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;
@@ -0,0 +1,7 @@
1
+ import React from 'react';
2
+ import buildTextDomain from './buildTextDomain';
3
+
4
+ const TextDomainContext = React.createContext(buildTextDomain({
5
+ }, 'n != 1'));
6
+
7
+ export default TextDomainContext;
@@ -0,0 +1,19 @@
1
+ import { gettext, ngettext, nxgettext, xgettext } from "./gettext";
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 = {
10
+ }, plural = "n != 1") {
11
+ return {
12
+ gettext: gettext.bind(null, translations),
13
+ ngettext: ngettext.bind(null, translations, plural),
14
+ xgettext: xgettext.bind(null, translations),
15
+ nxgettext: nxgettext.bind(null, translations, plural)
16
+ };
17
+ }
18
+
19
+ export default buildTextDomain;
package/src/gettext.js ADDED
@@ -0,0 +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;
package/src/index.js ADDED
@@ -0,0 +1,12 @@
1
+ import TextDomain from './TextDomain';
2
+ import TextDomainContext from './TextDomainContext';
3
+ import buildTextDomain from './buildTextDomain';
4
+ import withGettext from './withGettext';
5
+
6
+ export {
7
+ TextDomain,
8
+ TextDomainContext,
9
+ buildTextDomain,
10
+ };
11
+
12
+ export default withGettext;
@@ -0,0 +1,40 @@
1
+ import React from 'react';
2
+ import TextDomain from './TextDomain';
3
+ import hoistNonReactStatic from 'hoist-non-react-statics';
4
+
5
+ const withGettext = (translations = {
6
+ }, pluralForm = 'n != 1', options = {
7
+ }) => (WrappedComponent) => {
8
+ const args = Object.assign({
9
+ withRef: false
10
+ }, options);
11
+
12
+ class WithGettext extends TextDomain {
13
+
14
+ getWrappedComponent() {
15
+ return this.refs.wrappedComponent;
16
+ }
17
+
18
+ render() {
19
+ const newprops = Object.assign({
20
+ }, this.props);
21
+ if (args.withRef) {
22
+ newprops.ref = 'wrappedComponent';
23
+ }
24
+
25
+ return React.createElement(WrappedComponent, newprops);
26
+ }
27
+
28
+ }
29
+
30
+ WithGettext.defaultProps = {
31
+ translations,
32
+ plural: pluralForm,
33
+ };
34
+
35
+ WithGettext.displayName = `withGettext(${WrappedComponent.displayName || WrappedComponent.name || 'Component'})`;
36
+
37
+ return hoistNonReactStatic(WithGettext, WrappedComponent);
38
+ };
39
+
40
+ export default withGettext;