@lingui/react
Advanced tools
Comparing version 3.0.0-5 to 3.0.0-6
/// <reference types="react" /> | ||
import { I18n } from "@lingui/core"; | ||
import { TransRenderType } from "./Trans"; | ||
interface I18nContext { | ||
i18n: I18n; | ||
defaultRender?: any; | ||
defaultRender?: TransRenderType; | ||
} | ||
@@ -10,8 +11,5 @@ export interface I18nProviderProps extends I18nContext { | ||
} | ||
export declare function useLingui(): { | ||
i18n: any; | ||
defaultRender: any; | ||
}; | ||
export declare function useLingui(): I18nContext; | ||
export declare const I18nProvider: (props: I18nProviderProps) => JSX.Element; | ||
export {}; | ||
//# sourceMappingURL=I18nProvider.d.ts.map |
@@ -1,12 +0,3 @@ | ||
/// <reference types="react" /> | ||
export { I18nProvider, useLingui } from "./I18nProvider"; | ||
export { Trans } from "./Trans"; | ||
export declare const DateFormat: import("react").ElementType<{ | ||
value: {}; | ||
format?: {}; | ||
}>; | ||
export declare const NumberFormat: import("react").ElementType<{ | ||
value: {}; | ||
format?: {}; | ||
}>; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -10,3 +10,2 @@ 'use strict'; | ||
var _objectSpread = _interopDefault(require('@babel/runtime/helpers/objectSpread')); | ||
var core = require('@lingui/core'); | ||
@@ -17,19 +16,12 @@ var LinguiContext = React.createContext(null); | ||
if (context == null) { | ||
{ | ||
{ | ||
if (context == null) { | ||
throw new Error("useLingui hook was used without I18nProvider."); | ||
} | ||
return null; // return {} | ||
} | ||
var i18n = context.i18n, | ||
defaultRender = context.defaultRender; | ||
return { | ||
i18n: i18n, | ||
defaultRender: defaultRender | ||
}; | ||
return context; | ||
} | ||
var I18nProvider = function I18nProvider(props) { | ||
var _React$useState = React.useState(refreshContext(true)), | ||
var _React$useState = React.useState(makeContext()), | ||
_React$useState2 = _slicedToArray(_React$useState, 2), | ||
@@ -48,4 +40,4 @@ context = _React$useState2[0], | ||
React.useEffect(function () { | ||
var unsubscribe = props.i18n.didActivate(function () { | ||
return refreshContext(); | ||
var unsubscribe = props.i18n.on("change", function () { | ||
return setContext(makeContext()); | ||
}); | ||
@@ -64,12 +56,11 @@ return function () { | ||
* of creating a separate Provider/Consumer pair. | ||
* | ||
* We can't use useMemo hook either, because we want to recalculate value manually. | ||
*/ | ||
function refreshContext() { | ||
var initial = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; | ||
var newContext = { | ||
function makeContext() { | ||
return { | ||
i18n: props.i18n, | ||
defaultRender: props.defaultRender | ||
}; | ||
if (initial) return newContext; | ||
setContext(newContext); | ||
} | ||
@@ -115,2 +106,3 @@ | ||
}, // format children for pair tags | ||
// unpaired tags might have children if it's a component passed as a variable | ||
children ? formatElements(children, elements) : element.props.children)); | ||
@@ -212,13 +204,13 @@ if (after) tree.push(after); | ||
return React.createElement(render, {}, translation); | ||
} else if (typeof render === "function") { | ||
// Function: (props) => <a title={props.translation}>x</a> | ||
return render({ | ||
id: id, | ||
translation: translation, | ||
message: message | ||
}); | ||
} // Element: <p className="lear' /> | ||
} else if (React.isValidElement(render)) { | ||
// Element: <p className="lear' /> | ||
return React.cloneElement(render, {}, translation); | ||
} // Component: (props) => <a title={props.translation}>x</a> | ||
return React.cloneElement(render, {}, translation); | ||
return React.createElement(render, { | ||
id: id, | ||
translation: translation, | ||
message: message | ||
}); | ||
} | ||
@@ -230,23 +222,4 @@ Trans.defaultProps = { | ||
function createFormat(formatFunction) { | ||
// @ts-ignore: React types doens't support string as a component return type | ||
return function (_ref) { | ||
var value = _ref.value, | ||
format = _ref.format; | ||
var _useLingui = useLingui(), | ||
i18n = _useLingui.i18n; | ||
var formatter = formatFunction(i18n.locales || i18n.locale, format); | ||
return formatter(value); | ||
}; | ||
} | ||
var DateFormat = createFormat(core.formats.date); | ||
var NumberFormat = createFormat(core.formats.number); | ||
exports.DateFormat = DateFormat; | ||
exports.I18nProvider = I18nProvider; | ||
exports.NumberFormat = NumberFormat; | ||
exports.Trans = Trans; | ||
exports.useLingui = useLingui; |
@@ -1,1 +0,1 @@ | ||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var r=e(require("@babel/runtime/helpers/slicedToArray")),t=require("react"),n=e(require("@babel/runtime/helpers/objectSpread")),a=require("@lingui/core"),u=t.createContext(null);function i(){var e=t.useContext(u);if(null==e)return null;var r=e.i18n,n=e.defaultRender;return{i18n:r,defaultRender:n}}var o=/<(\d+)>(.*)<\/\1>|<(\d+)\/>/,l=/(?:\r\n|\r|\n)/g;function c(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},a=e.replace(l,"").split(o);if(1===a.length)return e;var u=[],i=a.shift();i&&u.push(i);var s=!0,f=!1,d=void 0;try{for(var v,p=function e(t){if(!t.length)return[];var n=t.slice(0,4),a=r(n,4),u=a[0],i=a[1],o=a[2],l=a[3];return[[parseInt(u||o),i||"",l]].concat(e(t.slice(4,t.length)))}(a)[Symbol.iterator]();!(s=(v=p.next()).done);s=!0){var m=r(v.value,3),h=m[0],y=m[1],g=m[2],b=n[h];u.push(t.cloneElement(b,{key:h},y?c(y,n):b.props.children)),g&&u.push(g)}}catch(e){f=!0,d=e}finally{try{s||null==p.return||p.return()}finally{if(f)throw d}}return u}function s(e){var r=i(),a=r.i18n,u=r.defaultRender,o=e.render,l=void 0===o?u:o,s=e.id,f=e.message,d=e.formats,v=n({},e.values),p=n({},e.components);v&&Object.keys(v).forEach(function(e){var r=v[e];if(t.isValidElement(r)){var n=Object.keys(p).length;p[n]=r,v[e]="<".concat(n,"/>")}});var m=a&&"function"==typeof a._?a._(s,v,{message:f,formats:d}):s,h=m?c(m,p):null;return null==l?h:"string"==typeof l?t.createElement(l,{},h):"function"==typeof l?l({id:s,translation:h,message:f}):t.cloneElement(l,{},h)}function f(e){return function(r){var t=r.value,n=r.format,a=i(),u=a.i18n,o=e(u.locales||u.locale,n);return o(t)}}s.defaultProps={values:{},components:{}};var d=f(a.formats.date),v=f(a.formats.number);exports.DateFormat=d,exports.I18nProvider=function(e){var n=t.useState(l(!0)),a=r(n,2),i=a[0],o=a[1];function l(){var r=arguments.length>0&&void 0!==arguments[0]&&arguments[0],t={i18n:e.i18n,defaultRender:e.defaultRender};if(r)return t;o(t)}return t.useEffect(function(){var r=e.i18n.didActivate(function(){return l()});return function(){return r()}},[]),t.createElement(u.Provider,{value:i},i.i18n.locale&&e.children)},exports.NumberFormat=v,exports.Trans=s,exports.useLingui=i; | ||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var r=e(require("@babel/runtime/helpers/slicedToArray")),n=require("react"),t=e(require("@babel/runtime/helpers/objectSpread")),a=n.createContext(null);function u(){var e=n.useContext(a);return e}var l=/<(\d+)>(.*)<\/\1>|<(\d+)\/>/,i=/(?:\r\n|\r|\n)/g;function o(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},a=e.replace(i,"").split(l);if(1===a.length)return e;var u=[],c=a.shift();c&&u.push(c);var s=!0,f=!1,d=void 0;try{for(var v,p=function e(n){if(!n.length)return[];var t=n.slice(0,4),a=r(t,4),u=a[0],l=a[1],i=a[2],o=a[3];return[[parseInt(u||i),l||"",o]].concat(e(n.slice(4,n.length)))}(a)[Symbol.iterator]();!(s=(v=p.next()).done);s=!0){var h=r(v.value,3),m=h[0],y=h[1],g=h[2],b=t[m];u.push(n.cloneElement(b,{key:m},y?o(y,t):b.props.children)),g&&u.push(g)}}catch(e){f=!0,d=e}finally{try{s||null==p.return||p.return()}finally{if(f)throw d}}return u}function c(e){var r=u(),a=r.i18n,l=r.defaultRender,i=e.render,c=void 0===i?l:i,s=e.id,f=e.message,d=e.formats,v=t({},e.values),p=t({},e.components);v&&Object.keys(v).forEach(function(e){var r=v[e];if(n.isValidElement(r)){var t=Object.keys(p).length;p[t]=r,v[e]="<".concat(t,"/>")}});var h=a&&"function"==typeof a._?a._(s,v,{message:f,formats:d}):s,m=h?o(h,p):null;return null==c?m:"string"==typeof c?n.createElement(c,{},m):n.isValidElement(c)?n.cloneElement(c,{},m):n.createElement(c,{id:s,translation:m,message:f})}c.defaultProps={values:{},components:{}},exports.I18nProvider=function(e){var t=n.useState(o()),u=r(t,2),l=u[0],i=u[1];function o(){return{i18n:e.i18n,defaultRender:e.defaultRender}}return n.useEffect(function(){var r=e.i18n.on("change",function(){return i(o())});return function(){return r()}},[]),n.createElement(a.Provider,{value:l},l.i18n.locale&&e.children)},exports.Trans=c,exports.useLingui=u; |
import * as React from "react"; | ||
export interface TransRenderProps { | ||
id: string; | ||
translation: React.ReactNode; | ||
message: string | null; | ||
} | ||
export declare type TransRenderType = string | React.ElementType<TransRenderProps> | React.ReactElement; | ||
export interface TransProps { | ||
@@ -10,5 +16,5 @@ id: string; | ||
formats?: Object; | ||
render?: string | React.ElementType | React.ReactElement; | ||
render?: TransRenderType; | ||
} | ||
export declare function Trans(props: TransProps): any; | ||
export declare function Trans(props: TransProps): string | any[] | React.ReactElement<{}, string | ((props: any) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)>) | (new (props: any) => React.Component<any, any, any>)>; | ||
export declare namespace Trans { | ||
@@ -15,0 +21,0 @@ var defaultProps: { |
{ | ||
"name": "@lingui/react", | ||
"version": "3.0.0-5", | ||
"version": "3.0.0-6", | ||
"description": "React components for translations", | ||
"main": "index.js", | ||
"types": "cjs/index.d.ts", | ||
"author": { | ||
@@ -41,5 +42,8 @@ "name": "Tomáš Ehrlich", | ||
"dependencies": { | ||
"@babel/runtime": "^7.0.0", | ||
"@lingui/core": "3.0.0-5" | ||
"@babel/runtime": "^7.4.5", | ||
"@lingui/core": "3.0.0-6" | ||
}, | ||
"devDependencies": { | ||
"react-testing-library": "^7.0.1" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
15892
1
14
250
+ Added@lingui/core@3.0.0-6(transitive)
- Removed@lingui/core@3.0.0-5(transitive)
Updated@babel/runtime@^7.4.5
Updated@lingui/core@3.0.0-6