@m6web/react-i18n
Advanced tools
Comparing version 2.0.0-alpha.0 to 2.0.0-alpha.1
@@ -10,6 +10,2 @@ # Change Log | ||
## [1.9.1](https://github.com/M6Web/i18n-tools/compare/@m6web/react-i18n@1.9.0...@m6web/react-i18n@1.9.1) (2021-03-09) | ||
@@ -19,9 +15,4 @@ | ||
# [1.9.0](https://github.com/M6Web/i18n-tools/compare/@m6web/react-i18n@1.9.0...@m6web/react-i18n@1.9.0) (2021-03-08) | ||
### Bug Fixes | ||
@@ -35,3 +26,2 @@ | ||
### Features | ||
@@ -38,0 +28,0 @@ |
@@ -1,2 +0,2 @@ | ||
import React from 'react'; | ||
import React, { useMemo } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
@@ -12,5 +12,10 @@ import { translate } from '../utils/i18n.utils'; | ||
parseHTML = _ref.parseHTML; | ||
var t = useMemo(function () { | ||
return translate(lang, { i18nNames: i18nNames, errorCallback: errorCallback, parseHTML: parseHTML }); | ||
}, [lang, i18nNames, errorCallback, parseHTML]); | ||
return React.createElement( | ||
Context.Provider, | ||
{ value: translate(lang, i18nNames, errorCallback, parseHTML) }, | ||
{ value: t }, | ||
children | ||
@@ -17,0 +22,0 @@ ); |
@@ -22,3 +22,3 @@ var _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; }; | ||
function (t) { | ||
return React.createElement(Element, _extends({}, props, { dangerouslySetInnerHTML: { __html: t(i18nKey, data, number, general, renderers) } })); | ||
return React.createElement(Element, _extends({}, props, { dangerouslySetInnerHTML: { __html: t(i18nKey, { data: data, number: number, general: general, renderers: renderers }) } })); | ||
} | ||
@@ -25,0 +25,0 @@ ); |
@@ -15,3 +15,3 @@ import React from 'react'; | ||
function (t) { | ||
return t(i18nKey, data, number, general, renderers); | ||
return t(i18nKey, { data: data, number: number, general: general, renderers: renderers }); | ||
} | ||
@@ -18,0 +18,0 @@ ); |
import { useContext } from 'react'; | ||
import { Context } from '../context/i18n.context'; | ||
var emptyObject = {}; | ||
export var useTranslate = function useTranslate() { | ||
@@ -8,10 +9,5 @@ var t = useContext(Context); | ||
return function (key) { | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
data = _ref.data, | ||
number = _ref.number, | ||
general = _ref.general, | ||
renderers = _ref.renderers; | ||
return t(key, data, number, general, renderers); | ||
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : emptyObject; | ||
return t(key, data); | ||
}; | ||
}; |
@@ -48,14 +48,20 @@ import _has from 'lodash-es/has'; | ||
var defaultConfig = { i18nNames: {}, errorCallback: _noop, parseHTML: false }; | ||
export var translate = function translate(lang) { | ||
var i18nNames = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var errorCallback = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _noop; | ||
var parseHTML = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false; | ||
var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var _defaultConfig$config = _extends({}, defaultConfig, config), | ||
i18nNames = _defaultConfig$config.i18nNames, | ||
errorCallback = _defaultConfig$config.errorCallback, | ||
parseHTML = _defaultConfig$config.parseHTML; | ||
var pluralize = pluralizeFunctions[_get(lang, '_i18n.lang')] || pluralizeFunctions.fr; | ||
return function (key) { | ||
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var number = arguments[2]; | ||
var general = arguments[3]; | ||
var renderers = arguments[4]; | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
_ref$data = _ref.data, | ||
data = _ref$data === undefined ? {} : _ref$data, | ||
number = _ref.number, | ||
general = _ref.general, | ||
renderers = _ref.renderers; | ||
@@ -62,0 +68,0 @@ var combineKey = key; |
@@ -28,5 +28,10 @@ 'use strict'; | ||
parseHTML = _ref.parseHTML; | ||
var t = (0, _react.useMemo)(function () { | ||
return (0, _i18n.translate)(lang, { i18nNames: i18nNames, errorCallback: errorCallback, parseHTML: parseHTML }); | ||
}, [lang, i18nNames, errorCallback, parseHTML]); | ||
return _react2.default.createElement( | ||
_i18n2.Context.Provider, | ||
{ value: (0, _i18n.translate)(lang, i18nNames, errorCallback, parseHTML) }, | ||
{ value: t }, | ||
children | ||
@@ -33,0 +38,0 @@ ); |
@@ -37,3 +37,3 @@ 'use strict'; | ||
function (t) { | ||
return _react2.default.createElement(Element, _extends({}, props, { dangerouslySetInnerHTML: { __html: t(i18nKey, data, number, general, renderers) } })); | ||
return _react2.default.createElement(Element, _extends({}, props, { dangerouslySetInnerHTML: { __html: t(i18nKey, { data: data, number: number, general: general, renderers: renderers }) } })); | ||
} | ||
@@ -40,0 +40,0 @@ ); |
@@ -30,3 +30,3 @@ 'use strict'; | ||
function (t) { | ||
return t(i18nKey, data, number, general, renderers); | ||
return t(i18nKey, { data: data, number: number, general: general, renderers: renderers }); | ||
} | ||
@@ -33,0 +33,0 @@ ); |
@@ -12,2 +12,3 @@ 'use strict'; | ||
var emptyObject = {}; | ||
var useTranslate = exports.useTranslate = function useTranslate() { | ||
@@ -17,10 +18,5 @@ var t = (0, _react.useContext)(_i18n.Context); | ||
return function (key) { | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
data = _ref.data, | ||
number = _ref.number, | ||
general = _ref.general, | ||
renderers = _ref.renderers; | ||
return t(key, data, number, general, renderers); | ||
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : emptyObject; | ||
return t(key, data); | ||
}; | ||
}; |
@@ -58,14 +58,20 @@ 'use strict'; | ||
var defaultConfig = { i18nNames: {}, errorCallback: _lodash2.default.noop, parseHTML: false }; | ||
var translate = exports.translate = function translate(lang) { | ||
var i18nNames = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var errorCallback = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _lodash2.default.noop; | ||
var parseHTML = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false; | ||
var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var _defaultConfig$config = _extends({}, defaultConfig, config), | ||
i18nNames = _defaultConfig$config.i18nNames, | ||
errorCallback = _defaultConfig$config.errorCallback, | ||
parseHTML = _defaultConfig$config.parseHTML; | ||
var pluralize = pluralizeFunctions[_lodash2.default.get(lang, '_i18n.lang')] || pluralizeFunctions.fr; | ||
return function (key) { | ||
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var number = arguments[2]; | ||
var general = arguments[3]; | ||
var renderers = arguments[4]; | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
_ref$data = _ref.data, | ||
data = _ref$data === undefined ? {} : _ref$data, | ||
number = _ref.number, | ||
general = _ref.general, | ||
renderers = _ref.renderers; | ||
@@ -72,0 +78,0 @@ var combineKey = key; |
{ | ||
"name": "@m6web/react-i18n", | ||
"version": "2.0.0-alpha.0", | ||
"version": "2.0.0-alpha.1", | ||
"description": "Provider and utils for translation in a react app", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -10,3 +10,2 @@ # @m6web/react-i18n | ||
This library brings internationalisation through a set of react components. | ||
@@ -61,2 +60,3 @@ | ||
### i18n Provider | ||
This component will provide the translation function to following components via the React.Context api. | ||
@@ -231,2 +231,3 @@ | ||
### HTML Interpolation | ||
Basic html tags are automatically interpolated in translation if the syntax is correct (opening tag should be close within the translation). | ||
@@ -237,3 +238,5 @@ | ||
Basic textual interpolations are proceeded first, and the HTML comes in a second time. | ||
- translation | ||
* translation | ||
```json | ||
@@ -246,3 +249,5 @@ { | ||
``` | ||
- code | ||
* code | ||
```jsx | ||
@@ -260,5 +265,7 @@ import React from 'react'; | ||
); | ||
} | ||
}; | ||
``` | ||
- result | ||
* result | ||
```jsx harmony | ||
@@ -273,12 +280,15 @@ <div> | ||
#### excluded elements | ||
For now `script` and `iframe` elements are ignored with all their children in the HTML tree. | ||
For now `script` and `iframe` elements are ignored with all their children in the HTML tree. | ||
#### keys | ||
In case of arrays of component, keys will be automatically generated to please React. | ||
- translation | ||
* translation | ||
```js | ||
{ | ||
foo: { | ||
bar: | ||
'<h1>Test</h1>' + | ||
bar: '<h1>Test</h1>' + | ||
'<p>This is not what we wanna do with this lib but we need to ensure it works anyway</p>' + | ||
@@ -289,7 +299,9 @@ '<ul>' + | ||
'<li>and an auto closing br <br /></li>' + | ||
'</ul>' | ||
'</ul>'; | ||
} | ||
}; | ||
} | ||
``` | ||
- result | ||
* result | ||
```jsx harmony | ||
@@ -296,0 +308,0 @@ <div> |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47941
810
380