This commit is contained in:
21
node_modules/react-polyglot/LICENSE
generated
vendored
Normal file
21
node_modules/react-polyglot/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Nayaabkhan Khan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
142
node_modules/react-polyglot/README.md
generated
vendored
Normal file
142
node_modules/react-polyglot/README.md
generated
vendored
Normal file
@@ -0,0 +1,142 @@
|
||||
<a href="https://codeclimate.com/github/nayaabkhan/react-polyglot/maintainability"><img src="https://api.codeclimate.com/v1/badges/fd8c57e662c5f08ba77e/maintainability" /></a>
|
||||
<a href="https://codeclimate.com/github/nayaabkhan/react-polyglot/test_coverage"><img src="https://api.codeclimate.com/v1/badges/fd8c57e662c5f08ba77e/test_coverage" /></a>
|
||||
<a href="https://travis-ci.org/nayaabkhan/react-polyglot"><img src="https://travis-ci.org/nayaabkhan/react-polyglot.svg?branch=master" /></a>
|
||||
<a href="https://bundlephobia.com/result?p=react-polyglot"><img src="https://badgen.net/bundlephobia/min/react-polyglot" /></a>
|
||||
<a href="https://bundlephobia.com/result?p=react-polyglot"><img src="https://badgen.net/bundlephobia/minzip/react-polyglot" /></a>
|
||||
|
||||
# React Polyglot
|
||||
Provides higher order component for using Polyglot with React
|
||||
|
||||
## Installation
|
||||
|
||||
```
|
||||
npm install --save react-polyglot
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
`react-polyglot` exports consists for one wrapper component called `I18n`, one decorator called
|
||||
`translate` and one hook called `useTranslate`. The decorator provides a prop `t` which is instance of `Polyglot`.
|
||||
|
||||
You are required to wrap your root component with `I18n` and pass on a `locale` like `en` or `fr`.
|
||||
And `messages` object containing the strings.
|
||||
|
||||
```js
|
||||
import React from 'react';
|
||||
import { render } from 'react-dom';
|
||||
import { I18n } from 'react-polyglot';
|
||||
import App from './components/app';
|
||||
|
||||
const locale = window.locale || 'en';
|
||||
const messages = {
|
||||
"hello_name": "Hello, %{name}.",
|
||||
"num_cars": "%{smart_count} car |||| %{smart_count} cars",
|
||||
}
|
||||
|
||||
render(
|
||||
<I18n locale={locale} messages={messages}>
|
||||
<App />
|
||||
</I18n>,
|
||||
document.getElementById('app')
|
||||
);
|
||||
```
|
||||
|
||||
Then inside `App` or a child component of `App` you can do:
|
||||
|
||||
```js
|
||||
import React from 'react';
|
||||
import { translate } from 'react-polyglot';
|
||||
|
||||
const Greeter = ({ name, t }) => (
|
||||
<h3>{t('hello_name', { name })}</h3>
|
||||
);
|
||||
|
||||
Greeter.propTypes = {
|
||||
name: React.PropTypes.string.isRequired,
|
||||
t: React.PropTypes.func.isRequired,
|
||||
};
|
||||
|
||||
export default translate()(Greeter);
|
||||
```
|
||||
|
||||
|
||||
or with React Hooks:
|
||||
|
||||
```js
|
||||
import React from 'react';
|
||||
import { useTranslate } from 'react-polyglot';
|
||||
|
||||
export default const Greeter = ({ name }) => {
|
||||
const t = useTranslate();
|
||||
|
||||
return (
|
||||
<h3>{t('hello_name', { name })}</h3>
|
||||
);
|
||||
};
|
||||
|
||||
Greeter.propTypes = {
|
||||
name: React.PropTypes.string.isRequired
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
|
||||
## Live Examples
|
||||
|
||||
### Minimal example using class components
|
||||
|
||||
https://codesandbox.io/s/mq76ojk228
|
||||
|
||||
### Advance example with user changeable locales
|
||||
|
||||
https://codesandbox.io/s/px8n63v0m
|
||||
|
||||
|
||||
## How to provide context in your tests
|
||||
|
||||
Use a simple helper to wrap your components in a context.
|
||||
|
||||
```js
|
||||
export const wrapWithContext = function (component, context, contextTypes) {
|
||||
const wrappedComponent = React.createClass({
|
||||
childContextTypes: contextTypes,
|
||||
getChildContext() {
|
||||
return context;
|
||||
},
|
||||
render() {
|
||||
return component;
|
||||
},
|
||||
});
|
||||
return React.createElement(wrappedComponent);
|
||||
}
|
||||
```
|
||||
|
||||
Then use it inside your tests.
|
||||
|
||||
```js
|
||||
import React from 'react';
|
||||
import { renderToString } from 'react-dom/server';
|
||||
import Polyglot from 'node-polyglot';
|
||||
import Greeter from './greeter';
|
||||
import { wrapWithContext } from './helpers';
|
||||
|
||||
const polyglot = new Polyglot({
|
||||
locale: 'en',
|
||||
phrases: {"hello_name": "Hello, %{name}."},
|
||||
});
|
||||
|
||||
const greeterWithContext = wrapWithContext(
|
||||
<Greeter name="Batsy" />,
|
||||
{ t: polyglot.t.bind(polyglot) },
|
||||
{ t: React.PropTypes.func }
|
||||
);
|
||||
|
||||
// use greeterWithContext in your tests
|
||||
// here it is shown how to use it with renderToString
|
||||
console.log(renderToString(greeterWithContext));
|
||||
```
|
||||
|
||||
|
||||
## Release History
|
||||
|
||||
Check the [Releases](https://github.com/nayaabkhan/react-polyglot/releases) tab.
|
||||
17
node_modules/react-polyglot/example/app.js
generated
vendored
Normal file
17
node_modules/react-polyglot/example/app.js
generated
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
import React from 'react';
|
||||
import { render } from 'react-dom';
|
||||
import { I18n } from '../lib';
|
||||
import Greeter from './greeter';
|
||||
|
||||
const locale = window.locale || 'en';
|
||||
const messages = {
|
||||
"hello_name": "Hello, %{name}.",
|
||||
"num_cars": "%{smart_count} car |||| %{smart_count} cars",
|
||||
}
|
||||
|
||||
render(
|
||||
<I18n locale={locale} messages={messages}>
|
||||
<Greeter name="Batsy" />
|
||||
</I18n>,
|
||||
document.getElementById('app')
|
||||
);
|
||||
21487
node_modules/react-polyglot/example/dist.js
generated
vendored
Normal file
21487
node_modules/react-polyglot/example/dist.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
8
node_modules/react-polyglot/example/greeter.js
generated
vendored
Normal file
8
node_modules/react-polyglot/example/greeter.js
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
import React from 'react';
|
||||
import { translate } from '../lib';
|
||||
|
||||
const Greeter = ({ name, t }) => (
|
||||
<h3>{t('hello_name', { name })}</h3>
|
||||
);
|
||||
|
||||
export default translate()(Greeter);
|
||||
10
node_modules/react-polyglot/example/index.html
generated
vendored
Normal file
10
node_modules/react-polyglot/example/index.html
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>react-polygot demo</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script src="dist.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
15
node_modules/react-polyglot/lib/i18n-context.js
generated
vendored
Normal file
15
node_modules/react-polyglot/lib/i18n-context.js
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports["default"] = void 0;
|
||||
|
||||
var _react = _interopRequireDefault(require("react"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
||||
|
||||
var I18nContext = _react["default"].createContext();
|
||||
|
||||
var _default = I18nContext;
|
||||
exports["default"] = _default;
|
||||
66
node_modules/react-polyglot/lib/i18n.js
generated
vendored
Normal file
66
node_modules/react-polyglot/lib/i18n.js
generated
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports["default"] = I18n;
|
||||
|
||||
var _react = _interopRequireDefault(require("react"));
|
||||
|
||||
var _propTypes = _interopRequireDefault(require("prop-types"));
|
||||
|
||||
var _nodePolyglot = _interopRequireDefault(require("node-polyglot"));
|
||||
|
||||
var _i18nContext = _interopRequireDefault(require("./i18n-context"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
||||
|
||||
function I18n(_ref) {
|
||||
var locale = _ref.locale,
|
||||
messages = _ref.messages,
|
||||
allowMissing = _ref.allowMissing,
|
||||
onMissingKey = _ref.onMissingKey,
|
||||
interpolation = _ref.interpolation,
|
||||
pluralRules = _ref.pluralRules,
|
||||
children = _ref.children;
|
||||
|
||||
var translate = _react["default"].useMemo(function () {
|
||||
var polyglot = new _nodePolyglot["default"]({
|
||||
locale: locale,
|
||||
phrases: messages,
|
||||
allowMissing: allowMissing,
|
||||
onMissingKey: onMissingKey,
|
||||
interpolation: interpolation,
|
||||
pluralRules: pluralRules
|
||||
});
|
||||
var boundTranslate = polyglot.t.bind(polyglot);
|
||||
boundTranslate._polyglot = polyglot;
|
||||
return boundTranslate;
|
||||
}, [locale, messages, allowMissing, onMissingKey, interpolation, pluralRules]);
|
||||
|
||||
return _react["default"].createElement(_i18nContext["default"].Provider, {
|
||||
value: translate
|
||||
}, _react["default"].Children.only(children));
|
||||
}
|
||||
|
||||
I18n.propTypes = {
|
||||
locale: _propTypes["default"].string.isRequired,
|
||||
messages: _propTypes["default"].object.isRequired,
|
||||
allowMissing: _propTypes["default"].bool,
|
||||
onMissingKey: _propTypes["default"].func,
|
||||
interpolation: _propTypes["default"].shape({
|
||||
suffix: _propTypes["default"].string,
|
||||
prefix: _propTypes["default"].string
|
||||
}),
|
||||
pluralRules: _propTypes["default"].shape({
|
||||
pluralTypes: _propTypes["default"].object,
|
||||
pluralTypeToLanguages: _propTypes["default"].object
|
||||
}),
|
||||
children: _propTypes["default"].element.isRequired
|
||||
};
|
||||
I18n.defaultProps = {
|
||||
allowMissing: false,
|
||||
onMissingKey: undefined,
|
||||
interpolation: undefined,
|
||||
pluralRules: undefined
|
||||
};
|
||||
100
node_modules/react-polyglot/lib/i18n.test.js
generated
vendored
Normal file
100
node_modules/react-polyglot/lib/i18n.test.js
generated
vendored
Normal file
@@ -0,0 +1,100 @@
|
||||
"use strict";
|
||||
|
||||
var _react = _interopRequireWildcard(require("react"));
|
||||
|
||||
var _reactTestRenderer = _interopRequireDefault(require("react-test-renderer"));
|
||||
|
||||
var _i18n = _interopRequireDefault(require("./i18n"));
|
||||
|
||||
var _i18nContext = _interopRequireDefault(require("./i18n-context"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
||||
|
||||
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
|
||||
|
||||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
||||
|
||||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
||||
|
||||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
||||
|
||||
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
|
||||
|
||||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
||||
|
||||
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
||||
|
||||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
||||
|
||||
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
||||
|
||||
describe('I18n Provider', function () {
|
||||
var createChild = function createChild() {
|
||||
var Child =
|
||||
/*#__PURE__*/
|
||||
function (_Component) {
|
||||
_inherits(Child, _Component);
|
||||
|
||||
function Child() {
|
||||
_classCallCheck(this, Child);
|
||||
|
||||
return _possibleConstructorReturn(this, _getPrototypeOf(Child).apply(this, arguments));
|
||||
}
|
||||
|
||||
_createClass(Child, [{
|
||||
key: "render",
|
||||
value: function render() {
|
||||
return _react["default"].createElement("div", null);
|
||||
}
|
||||
}]);
|
||||
|
||||
return Child;
|
||||
}(_react.Component);
|
||||
|
||||
return Child;
|
||||
};
|
||||
|
||||
var Child = createChild();
|
||||
|
||||
function getPolyglotFromRenderer(renderer) {
|
||||
var instance = renderer.root;
|
||||
var children = instance.children;
|
||||
var firstChild = children[0];
|
||||
var firstChildValueProps = firstChild.props.value;
|
||||
var polyglot = firstChildValueProps._polyglot;
|
||||
return polyglot;
|
||||
}
|
||||
|
||||
it('should update instance on receiving new props', function () {
|
||||
var props = {
|
||||
locale: 'en',
|
||||
messages: {
|
||||
test: 'test'
|
||||
}
|
||||
};
|
||||
|
||||
var renderer = _reactTestRenderer["default"].create(_react["default"].createElement(_i18n["default"], props, _react["default"].createElement(_i18nContext["default"].Consumer, null, function (value) {
|
||||
return _react["default"].createElement(Child, {
|
||||
value: value
|
||||
});
|
||||
})));
|
||||
|
||||
var newProps = {
|
||||
locale: 'jp',
|
||||
messages: {
|
||||
test: 'test'
|
||||
}
|
||||
};
|
||||
renderer.update(_react["default"].createElement(_i18n["default"], newProps, _react["default"].createElement(_i18nContext["default"].Consumer, null, function (value) {
|
||||
return _react["default"].createElement(Child, {
|
||||
value: value
|
||||
});
|
||||
})));
|
||||
var polyglot = getPolyglotFromRenderer(renderer);
|
||||
expect(polyglot.locale()).toBe('jp');
|
||||
});
|
||||
});
|
||||
31
node_modules/react-polyglot/lib/index.js
generated
vendored
Normal file
31
node_modules/react-polyglot/lib/index.js
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(exports, "I18n", {
|
||||
enumerable: true,
|
||||
get: function get() {
|
||||
return _i18n["default"];
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, "translate", {
|
||||
enumerable: true,
|
||||
get: function get() {
|
||||
return _translate["default"];
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, "useTranslate", {
|
||||
enumerable: true,
|
||||
get: function get() {
|
||||
return _useTranslate["default"];
|
||||
}
|
||||
});
|
||||
|
||||
var _i18n = _interopRequireDefault(require("./i18n"));
|
||||
|
||||
var _translate = _interopRequireDefault(require("./translate"));
|
||||
|
||||
var _useTranslate = _interopRequireDefault(require("./useTranslate"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
||||
31
node_modules/react-polyglot/lib/translate.js
generated
vendored
Normal file
31
node_modules/react-polyglot/lib/translate.js
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports["default"] = translate;
|
||||
|
||||
var _react = _interopRequireDefault(require("react"));
|
||||
|
||||
var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics"));
|
||||
|
||||
var _i18nContext = _interopRequireDefault(require("./i18n-context"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
||||
|
||||
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
||||
|
||||
// higher order decorator for components that need `t`
|
||||
function translate() {
|
||||
return function (WrappedComponent) {
|
||||
var _translate = function _translate(props) {
|
||||
return _react["default"].createElement(_i18nContext["default"].Consumer, null, function (t) {
|
||||
return _react["default"].createElement(WrappedComponent, _extends({}, props, {
|
||||
t: t
|
||||
}));
|
||||
});
|
||||
};
|
||||
|
||||
return (0, _hoistNonReactStatics["default"])(_translate, WrappedComponent);
|
||||
};
|
||||
}
|
||||
16
node_modules/react-polyglot/lib/useTranslate.js
generated
vendored
Normal file
16
node_modules/react-polyglot/lib/useTranslate.js
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports["default"] = useTranslate;
|
||||
|
||||
var _react = require("react");
|
||||
|
||||
var _i18nContext = _interopRequireDefault(require("./i18n-context"));
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
||||
|
||||
function useTranslate() {
|
||||
return (0, _react.useContext)(_i18nContext["default"]);
|
||||
}
|
||||
67
node_modules/react-polyglot/package.json
generated
vendored
Normal file
67
node_modules/react-polyglot/package.json
generated
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
{
|
||||
"name": "react-polyglot",
|
||||
"version": "0.7.2",
|
||||
"description": "Higher order React component for using Polyglot",
|
||||
"main": "lib/index.js",
|
||||
"files": [
|
||||
"lib",
|
||||
"src",
|
||||
"example"
|
||||
],
|
||||
"types": "src/index.d.ts",
|
||||
"scripts": {
|
||||
"clean": "rimraf lib",
|
||||
"build:example": "webpack example/app.js example/dist.js",
|
||||
"build:commonjs": "babel src --out-dir lib --ignore '*.test.js'",
|
||||
"build": "npm run build:commonjs",
|
||||
"prepublish": "npm run clean && npm run build",
|
||||
"prettify": "prettier 'src/**/*.js' --write",
|
||||
"lint": "eslint src",
|
||||
"test": "jest"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/nayaabkhan/react-polyglot.git"
|
||||
},
|
||||
"keywords": [
|
||||
"react",
|
||||
"airbnb",
|
||||
"polyglot",
|
||||
"i18n"
|
||||
],
|
||||
"author": "Nayaabkhan Khan <khannayaab@gmail.com> (http://nayaabkhan.me)",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/nayaabkhan/react-polyglot/issues"
|
||||
},
|
||||
"homepage": "https://github.com/nayaabkhan/react-polyglot#readme",
|
||||
"peerDependencies": {
|
||||
"node-polyglot": "^2.0.0",
|
||||
"react": ">=16.8.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/cli": "^7.7.0",
|
||||
"@babel/core": "^7.7.0",
|
||||
"@babel/preset-env": "^7.7.1",
|
||||
"@babel/preset-react": "^7.7.0",
|
||||
"babel-eslint": "^10.0.3",
|
||||
"babel-loader": "^8.0.6",
|
||||
"eslint": "^6.6.0",
|
||||
"eslint-config-prettier": "^6.5.0",
|
||||
"eslint-plugin-prettier": "^3.1.1",
|
||||
"eslint-plugin-react": "^7.7.0",
|
||||
"eslint-plugin-react-hooks": "^2.2.0",
|
||||
"jest": "^24.9.0",
|
||||
"node-polyglot": "^2.2.2",
|
||||
"prettier": "^1.18.2",
|
||||
"react": "^16.3.2",
|
||||
"react-dom": "^16.3.2",
|
||||
"react-test-renderer": "^16.3.2",
|
||||
"rimraf": "^2.5.4",
|
||||
"webpack": "^4.41.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"hoist-non-react-statics": "^3.3.0",
|
||||
"prop-types": "^15.5.8"
|
||||
}
|
||||
}
|
||||
5
node_modules/react-polyglot/src/i18n-context.js
generated
vendored
Normal file
5
node_modules/react-polyglot/src/i18n-context.js
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
import React from 'react'
|
||||
|
||||
const I18nContext = React.createContext()
|
||||
|
||||
export default I18nContext
|
||||
49
node_modules/react-polyglot/src/i18n.d.ts
generated
vendored
Normal file
49
node_modules/react-polyglot/src/i18n.d.ts
generated
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
// Type definitions for react-polyglot v0.4.0
|
||||
// TypeScript Version: 2.8
|
||||
|
||||
import { ComponentType, ReactNode } from 'react'
|
||||
|
||||
interface InterpolationOptions {
|
||||
smart_count?: number | { length: number };
|
||||
_?: string;
|
||||
[interpolationKey: string]: any;
|
||||
}
|
||||
|
||||
interface InterpolationTokenOptions {
|
||||
prefix?: string;
|
||||
suffix?: string;
|
||||
}
|
||||
|
||||
interface PluralRules {
|
||||
pluralTypes: {
|
||||
[key: string]: (no: number) => number;
|
||||
};
|
||||
pluralTypeToLanguages: {
|
||||
[key: string]: string[];
|
||||
};
|
||||
}
|
||||
|
||||
interface PolyglotOptions {
|
||||
/** Locale to use, e.g. `en` */
|
||||
locale: string;
|
||||
/** A dictionary of translations */
|
||||
messages: object;
|
||||
|
||||
/** A boolean to control whether missing keys are allowed **/
|
||||
allowMissing?: boolean;
|
||||
/** If allow missing is true this function will be called instead of default error handler **/
|
||||
onMissingKey?: (key: string, options?: InterpolationOptions, locale?: string) => string;
|
||||
/** An object to change the substituation syntax for interpolation by setting prefix and suffix **/
|
||||
interpolation?: InterpolationTokenOptions;
|
||||
/** https://github.com/airbnb/polyglot.js#custom-pluralization-rules */
|
||||
pluralRules?: PluralRules;
|
||||
}
|
||||
|
||||
interface I18nProps extends PolyglotOptions {
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
/** Provider component to wrap your root application component in. */
|
||||
declare const I18n: ComponentType<I18nProps>
|
||||
|
||||
export default I18n
|
||||
64
node_modules/react-polyglot/src/i18n.js
generated
vendored
Normal file
64
node_modules/react-polyglot/src/i18n.js
generated
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
import React from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
import Polyglot from 'node-polyglot'
|
||||
import I18nContext from './i18n-context'
|
||||
|
||||
export default function I18n({
|
||||
locale,
|
||||
messages,
|
||||
|
||||
allowMissing,
|
||||
onMissingKey,
|
||||
interpolation,
|
||||
pluralRules,
|
||||
|
||||
children,
|
||||
}) {
|
||||
const translate = React.useMemo(() => {
|
||||
const polyglot = new Polyglot({
|
||||
locale,
|
||||
phrases: messages,
|
||||
|
||||
allowMissing,
|
||||
onMissingKey,
|
||||
interpolation,
|
||||
pluralRules,
|
||||
})
|
||||
const boundTranslate = polyglot.t.bind(polyglot)
|
||||
|
||||
boundTranslate._polyglot = polyglot
|
||||
|
||||
return boundTranslate
|
||||
}, [locale, messages, allowMissing, onMissingKey, interpolation, pluralRules])
|
||||
|
||||
return (
|
||||
<I18nContext.Provider value={translate}>
|
||||
{React.Children.only(children)}
|
||||
</I18nContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
I18n.propTypes = {
|
||||
locale: PropTypes.string.isRequired,
|
||||
messages: PropTypes.object.isRequired,
|
||||
|
||||
allowMissing: PropTypes.bool,
|
||||
onMissingKey: PropTypes.func,
|
||||
interpolation: PropTypes.shape({
|
||||
suffix: PropTypes.string,
|
||||
prefix: PropTypes.string,
|
||||
}),
|
||||
pluralRules: PropTypes.shape({
|
||||
pluralTypes: PropTypes.object,
|
||||
pluralTypeToLanguages: PropTypes.object,
|
||||
}),
|
||||
|
||||
children: PropTypes.element.isRequired,
|
||||
}
|
||||
|
||||
I18n.defaultProps = {
|
||||
allowMissing: false,
|
||||
onMissingKey: undefined,
|
||||
interpolation: undefined,
|
||||
pluralRules: undefined,
|
||||
}
|
||||
67
node_modules/react-polyglot/src/i18n.test.js
generated
vendored
Normal file
67
node_modules/react-polyglot/src/i18n.test.js
generated
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
import React, { Component } from 'react'
|
||||
import TestRenderer from 'react-test-renderer'
|
||||
import I18n from './i18n'
|
||||
import I18nContext from './i18n-context'
|
||||
|
||||
describe('I18n Provider', () => {
|
||||
const createChild = () => {
|
||||
class Child extends Component {
|
||||
render() {
|
||||
return <div />
|
||||
}
|
||||
}
|
||||
|
||||
return Child
|
||||
}
|
||||
const Child = createChild()
|
||||
|
||||
function getPolyglotFromRenderer(renderer) {
|
||||
const instance = renderer.root
|
||||
const children = instance.children
|
||||
const firstChild = children[0]
|
||||
const firstChildValueProps = firstChild.props.value
|
||||
const polyglot = firstChildValueProps._polyglot
|
||||
|
||||
return polyglot
|
||||
}
|
||||
|
||||
it('should update instance on receiving new props', () => {
|
||||
const props = {
|
||||
locale: 'en',
|
||||
messages: {
|
||||
test: 'test',
|
||||
},
|
||||
}
|
||||
|
||||
const renderer = TestRenderer.create(
|
||||
<I18n {...props}>
|
||||
<I18nContext.Consumer>
|
||||
{value => {
|
||||
return <Child value={value} />
|
||||
}}
|
||||
</I18nContext.Consumer>
|
||||
</I18n>
|
||||
)
|
||||
|
||||
const newProps = {
|
||||
locale: 'jp',
|
||||
messages: {
|
||||
test: 'test',
|
||||
},
|
||||
}
|
||||
|
||||
renderer.update(
|
||||
<I18n {...newProps}>
|
||||
<I18nContext.Consumer>
|
||||
{value => {
|
||||
return <Child value={value} />
|
||||
}}
|
||||
</I18nContext.Consumer>
|
||||
</I18n>
|
||||
)
|
||||
|
||||
const polyglot = getPolyglotFromRenderer(renderer)
|
||||
|
||||
expect(polyglot.locale()).toBe('jp')
|
||||
})
|
||||
})
|
||||
4
node_modules/react-polyglot/src/index.d.ts
generated
vendored
Normal file
4
node_modules/react-polyglot/src/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
export { default as I18n } from './i18n';
|
||||
export { default as translate } from './translate';
|
||||
export { default as useTranslate} from './useTranslate';
|
||||
export * from './translate';
|
||||
5
node_modules/react-polyglot/src/index.js
generated
vendored
Normal file
5
node_modules/react-polyglot/src/index.js
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
import I18n from './i18n'
|
||||
import translate from './translate'
|
||||
import useTranslate from './useTranslate'
|
||||
|
||||
export { I18n, translate, useTranslate }
|
||||
24
node_modules/react-polyglot/src/translate.d.ts
generated
vendored
Normal file
24
node_modules/react-polyglot/src/translate.d.ts
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
// Type definitions for react-polyglot v0.4.0
|
||||
// Typescript version: 2.8
|
||||
|
||||
import { InterpolationOptions } from 'node-polyglot'
|
||||
import { ComponentType } from 'react'
|
||||
|
||||
export type t = (
|
||||
/** The key of the phrase to translate. */
|
||||
phrase: string,
|
||||
/** The options accepted by `polyglot.t`. */
|
||||
options?: number | InterpolationOptions
|
||||
) => string
|
||||
|
||||
export interface TranslateProps {
|
||||
/** Function to get translated phrase. */
|
||||
t: t
|
||||
}
|
||||
|
||||
/** Wrap your components with `translate` to get a prop `t` passed in. */
|
||||
declare const translate = () => <T extends object>(
|
||||
Component: ComponentType<T>
|
||||
) => ComponentType<T & TranslateProps>(Component)
|
||||
|
||||
export default translate
|
||||
16
node_modules/react-polyglot/src/translate.js
generated
vendored
Normal file
16
node_modules/react-polyglot/src/translate.js
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
import React from 'react'
|
||||
import hoistNonReactStatics from 'hoist-non-react-statics'
|
||||
import I18nContext from './i18n-context'
|
||||
|
||||
// higher order decorator for components that need `t`
|
||||
export default function translate() {
|
||||
return WrappedComponent => {
|
||||
const _translate = props => (
|
||||
<I18nContext.Consumer>
|
||||
{t => <WrappedComponent {...props} t={t} />}
|
||||
</I18nContext.Consumer>
|
||||
)
|
||||
|
||||
return hoistNonReactStatics(_translate, WrappedComponent)
|
||||
}
|
||||
}
|
||||
10
node_modules/react-polyglot/src/useTranslate.d.ts
generated
vendored
Normal file
10
node_modules/react-polyglot/src/useTranslate.d.ts
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
import { InterpolationOptions } from 'node-polyglot'
|
||||
|
||||
type TranslateFunction = (
|
||||
phrase: string,
|
||||
options?: number| InterpolationOptions
|
||||
) => string
|
||||
|
||||
declare const useTranslate = () => TranslateFunction
|
||||
|
||||
export default useTranslate
|
||||
6
node_modules/react-polyglot/src/useTranslate.js
generated
vendored
Normal file
6
node_modules/react-polyglot/src/useTranslate.js
generated
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
import { useContext } from 'react'
|
||||
import I18nContext from './i18n-context'
|
||||
|
||||
export default function useTranslate() {
|
||||
return useContext(I18nContext)
|
||||
}
|
||||
Reference in New Issue
Block a user