react-polyglot-hooks
Advanced tools
Comparing version 0.3.1 to 0.4.0
@@ -5,2 +5,14 @@ # Changelog | ||
## [0.4.0](https://github.com/pmmmwh/react-polyglot-hooks/compare/v0.3.1...v0.4.0) (2020-04-18) | ||
### ⚠ BREAKING CHANGES | ||
- remove number interpolations for T component | ||
- bump minimum version of node to 10 | ||
### Features | ||
- bump minimum version of node to 10 ([09d19a5](https://github.com/pmmmwh/react-polyglot-hooks/commit/09d19a500f81821c75541ca6312cb01c7776e078)) | ||
- remove number interpolations for T component ([be1a8c2](https://github.com/pmmmwh/react-polyglot-hooks/commit/be1a8c2418e3742ba433dfd2ba230bd35ba01acd)) | ||
### [0.3.1](https://github.com/pmmmwh/react-polyglot-hooks/compare/v0.3.0...v0.3.1) (2019-10-10) | ||
@@ -7,0 +19,0 @@ |
@@ -1,12 +0,1 @@ | ||
import Polyglot from 'node-polyglot'; | ||
import enhanceT from './enhanceT'; | ||
/** | ||
* The original t function from Polyglot.js. | ||
*/ | ||
export declare type PolyglotT = typeof Polyglot.prototype.t; | ||
/** | ||
* The t function for translation. | ||
*/ | ||
export declare type tFunction = ReturnType<typeof enhanceT>; | ||
export declare const NO_NUMBER_INTERPOLATIONS: string; | ||
export declare const NO_POLYGLOT_CONTEXT: string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// TODO: Remove after deprecation of number from interpolations in v0.4.0 | ||
exports.NO_NUMBER_INTERPOLATIONS = [ | ||
'Warning:', | ||
'Use of the interpolations prop as a shorthand for smart_count have been deprecated in favor of the count prop and will be removed in the next major version.', | ||
'Please update your app to use that instead.', | ||
].join(' '); | ||
exports.NO_POLYGLOT_CONTEXT = [ | ||
@@ -10,0 +4,0 @@ 'Warning:', |
/// <reference path="../src/vendors.d.ts" /> | ||
/// <reference types="node-polyglot" /> | ||
import { ReactElement } from 'react'; | ||
import * as React from 'react'; | ||
/** | ||
* An function to enhance Polyglot.js to allow React component interpolations. | ||
* A function to enhance Polyglot.js to allow React component interpolations. | ||
* | ||
@@ -10,3 +10,3 @@ * @param originalT The original t function from Polyglot.js. | ||
*/ | ||
declare const enhanceT: (originalT: (phrase: string, options?: number | import("node-polyglot").InterpolationOptions | undefined) => string) => (key: string, interpolations?: number | import("node-polyglot").InterpolationOptions | undefined) => ReactElement<any, string | ((props: any) => ReactElement<any, string | any | (new (props: any) => import("react").Component<any, any, any>)> | null) | (new (props: any) => import("react").Component<any, any, any>)>; | ||
declare const enhanceT: (originalT: (phrase: string, options?: number | import("node-polyglot").InterpolationOptions | undefined) => string) => (key: string, interpolations?: number | import("node-polyglot").InterpolationOptions | undefined) => React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<any, any, any>)>; | ||
export default enhanceT; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var react_1 = require("react"); | ||
var tslib_1 = require("tslib"); | ||
var React = tslib_1.__importStar(require("react")); | ||
/** | ||
@@ -9,3 +10,3 @@ * A pseudo-JSX string interpolation identifier. | ||
/** | ||
* An function to enhance Polyglot.js to allow React component interpolations. | ||
* A function to enhance Polyglot.js to allow React component interpolations. | ||
* | ||
@@ -31,4 +32,4 @@ * @param originalT The original t function from Polyglot.js. | ||
Object.keys(interpolations).forEach(function (key) { | ||
// Store all JSX elements into a array cache for processing later | ||
if (react_1.isValidElement(interpolations[key])) { | ||
// Store all JSX elements into an array cache for processing later | ||
if (React.isValidElement(interpolations[key])) { | ||
elementCache_1.push(interpolations[key]); | ||
@@ -39,3 +40,3 @@ interpolations[key] = "<" + (elementCache_1.length - 1) + "/>"; | ||
var tString = originalT(key, interpolations); | ||
// We can safely return if no element interpolation is needed | ||
// We can safely return if we don't need to do any element interpolation | ||
if (!elementCache_1.length) { | ||
@@ -53,4 +54,4 @@ return tString; | ||
// Interpolate the element | ||
renderItems.push(react_1.cloneElement(currentElement, | ||
// A unique key is needed when rendering an array | ||
renderItems.push(React.cloneElement(currentElement, | ||
// We need a unique key when rendering an array | ||
{ key: currentIndex }, currentElement.props.children)); | ||
@@ -57,0 +58,0 @@ // Interpolate any trailing string |
import * as React from 'react'; | ||
import { PolyglotOptions } from 'node-polyglot'; | ||
import type { PolyglotOptions } from 'node-polyglot'; | ||
/** | ||
@@ -4,0 +4,0 @@ * Props accepted by the I18n component. |
"use strict"; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var React = __importStar(require("react")); | ||
var node_polyglot_1 = __importDefault(require("node-polyglot")); | ||
var enhanceT_1 = __importDefault(require("./enhanceT")); | ||
var i18nContext_1 = __importDefault(require("./i18nContext")); | ||
var tslib_1 = require("tslib"); | ||
var React = tslib_1.__importStar(require("react")); | ||
var node_polyglot_1 = tslib_1.__importDefault(require("node-polyglot")); | ||
var enhanceT_1 = tslib_1.__importDefault(require("./enhanceT")); | ||
var i18nContext_1 = tslib_1.__importDefault(require("./i18nContext")); | ||
/** | ||
@@ -18,0 +9,0 @@ * A component to allow consumption of i18n props from any nested children. |
@@ -1,3 +0,3 @@ | ||
/// <reference types="react" /> | ||
import { tFunction } from './constants'; | ||
import * as React from 'react'; | ||
import type { tFunction } from './types'; | ||
/** | ||
@@ -13,3 +13,3 @@ * The props provided by I18nContext. | ||
*/ | ||
declare const I18nContext: import("react").Context<I18nContextProps>; | ||
declare const I18nContext: React.Context<I18nContextProps>; | ||
export default I18nContext; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var react_1 = require("react"); | ||
var tslib_1 = require("tslib"); | ||
var React = tslib_1.__importStar(require("react")); | ||
var constants_1 = require("./constants"); | ||
@@ -19,3 +20,3 @@ function warnWithoutContext() { | ||
*/ | ||
var I18nContext = react_1.createContext({ | ||
var I18nContext = React.createContext({ | ||
locale: undefined, | ||
@@ -22,0 +23,0 @@ t: warnWithoutContext, |
@@ -1,6 +0,8 @@ | ||
export { tFunction } from './constants'; | ||
export { default as I18n, I18nProps } from './I18n'; | ||
export { I18nContextProps } from './i18nContext'; | ||
export { default as T, TProps } from './T'; | ||
export { default as I18n } from './I18n'; | ||
export { default as T } from './T'; | ||
export { default as useLocale } from './useLocale'; | ||
export { default as useT } from './useT'; | ||
export type { I18nProps } from './I18n'; | ||
export type { I18nContextProps } from './i18nContext'; | ||
export type { TProps } from './T'; | ||
export type { tFunction } from './types'; |
import * as React from 'react'; | ||
import { InterpolationOptions } from 'node-polyglot'; | ||
import type { InterpolationOptions } from 'node-polyglot'; | ||
/** | ||
@@ -12,3 +12,3 @@ * Props accepted by the T component. | ||
/** A key-value map of components or strings to be interpolated. */ | ||
interpolations?: number | InterpolationOptions; | ||
interpolations?: InterpolationOptions; | ||
/** The key of the phrase to render. */ | ||
@@ -15,0 +15,0 @@ phrase: string; |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var constants_1 = require("./constants"); | ||
var useT_1 = __importDefault(require("./useT")); | ||
var tslib_1 = require("tslib"); | ||
var useT_1 = tslib_1.__importDefault(require("./useT")); | ||
/** | ||
@@ -25,15 +11,3 @@ * A component to render a translated string. | ||
var t = useT_1.default(); | ||
var cleanInterpolations; | ||
if (typeof interpolations !== 'number') { | ||
cleanInterpolations = interpolations; | ||
} | ||
else { | ||
// TODO: Deprecate number from interpolations in v0.4.0 | ||
// Handles number interpolation | ||
cleanInterpolations = { smart_count: interpolations }; | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.warn(constants_1.NO_NUMBER_INTERPOLATIONS); | ||
} | ||
} | ||
var tOptions = __assign(__assign(__assign({}, (fallback && { _: fallback })), (count && { smart_count: count })), cleanInterpolations); | ||
var tOptions = tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, (fallback && { _: fallback })), (count && { smart_count: count })), interpolations); | ||
// HACK: A workaround for the current limitations of TSX with FC | ||
@@ -40,0 +14,0 @@ // Ref: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20544 |
/** | ||
* A hook to consume the current locale. | ||
* | ||
* @returns The current locale. | ||
* @returns The current active locale. | ||
*/ | ||
declare const useLocale: () => string | undefined; | ||
export default useLocale; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var react_1 = require("react"); | ||
var i18nContext_1 = __importDefault(require("./i18nContext")); | ||
var i18nContext_1 = tslib_1.__importDefault(require("./i18nContext")); | ||
/** | ||
* A hook to consume the current locale. | ||
* | ||
* @returns The current locale. | ||
* @returns The current active locale. | ||
*/ | ||
@@ -13,0 +11,0 @@ var useLocale = function () { |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var react_1 = require("react"); | ||
var i18nContext_1 = __importDefault(require("./i18nContext")); | ||
var i18nContext_1 = tslib_1.__importDefault(require("./i18nContext")); | ||
/** | ||
@@ -9,0 +7,0 @@ * A hook to consume the t function. |
{ | ||
"name": "react-polyglot-hooks", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Hooks for using Polyglot.js with React.", | ||
@@ -34,8 +34,6 @@ "keywords": [ | ||
"build": "tsc", | ||
"lint": "yarn run lint:eslint && yarn run lint:prettier", | ||
"lint:fix": "yarn run lint:eslint:fix && yarn run lint:prettier:fix", | ||
"lint:eslint": "eslint --report-unused-disable-directives **/*.{js,ts,tsx}", | ||
"lint:prettier": "prettier --check **/*.{js,ts,tsx,json,md,yml}", | ||
"lint:eslint:fix": "eslint --fix --report-unused-disable-directives **/*.{js,ts,tsx}", | ||
"lint:prettier:fix": "prettier --write **/*.{js,ts,tsx,json,md,yml}", | ||
"lint": "eslint --report-unused-disable-directives \"**/*.{js,ts,tsx}\"", | ||
"lint:fix": "eslint --fix --report-unused-disable-directives \"**/*.{js,ts,tsx}\"", | ||
"format": "prettier --write \"**/*.{js,ts,tsx,json,md,yml}\"", | ||
"format:check": "prettier --check \"**/*.{js,ts,tsx,json,md,yml}\"", | ||
"prebuild": "rimraf dist", | ||
@@ -47,3 +45,4 @@ "release": "yarn run build && standard-version", | ||
"dependencies": { | ||
"node-polyglot": "^2.3.1" | ||
"node-polyglot": "^2.4.0", | ||
"tslib": "^1.11.1" | ||
}, | ||
@@ -57,29 +56,29 @@ "devDependencies": { | ||
"@commitlint/config-conventional": "^8.1.0", | ||
"@testing-library/jest-dom": "^4.1.0", | ||
"@testing-library/react": "^9.1.3", | ||
"@testing-library/react-hooks": "^2.0.1", | ||
"@types/jest": "^24.0.18", | ||
"@types/node": "^12.7.2", | ||
"@testing-library/jest-dom": "^5.3.0", | ||
"@testing-library/react": "^10.0.2", | ||
"@testing-library/react-hooks": "^3.1.1", | ||
"@types/jest": "^25.1.3", | ||
"@types/node": "^13.7.7", | ||
"@types/node-polyglot": "^0.4.34", | ||
"@types/react": "^16.9.0", | ||
"@types/react-dom": "^16.9.0", | ||
"@types/sinon": "^7.0.13", | ||
"@typescript-eslint/eslint-plugin": "^1.13.0", | ||
"@typescript-eslint/parser": "^1.13.0", | ||
"@types/sinon": "^9.0.0", | ||
"@typescript-eslint/eslint-plugin": "^2.26.0", | ||
"@typescript-eslint/parser": "^2.26.0", | ||
"babel-eslint": "^10.0.3", | ||
"babel-jest": "^24.8.0", | ||
"babel-jest": "^25.2.6", | ||
"codecov": "^3.5.0", | ||
"eslint": "^6.1.0", | ||
"eslint-config-react-app": "^5.0.1", | ||
"eslint-plugin-flowtype": "^3.13.0", | ||
"eslint-config-react-app": "^5.2.1", | ||
"eslint-plugin-flowtype": "^4.7.0", | ||
"eslint-plugin-import": "^2.18.2", | ||
"eslint-plugin-jsx-a11y": "^6.2.3", | ||
"eslint-plugin-react": "^7.14.3", | ||
"eslint-plugin-react-hooks": "^1.7.0", | ||
"husky": "^3.0.4", | ||
"jest": "^24.9.0", | ||
"jest-environment-jsdom-fourteen": "0.1.0", | ||
"jest-watch-typeahead": "^0.4.0", | ||
"lint-staged": "^9.2.4", | ||
"prettier": "^1.18.2", | ||
"eslint-plugin-react-hooks": "^3.0.0", | ||
"husky": "^4.2.3", | ||
"jest": "^25.2.7", | ||
"jest-environment-jsdom-sixteen": "1.0.3", | ||
"jest-watch-typeahead": "^0.5.0", | ||
"lint-staged": "^10.1.1", | ||
"prettier": "^2.0.2", | ||
"react": "^16.9.0", | ||
@@ -89,5 +88,5 @@ "react-dom": "^16.9.0", | ||
"rimraf": "^3.0.0", | ||
"sinon": "^7.4.1", | ||
"sinon": "^9.0.0", | ||
"standard-version": "^7.0.0", | ||
"typescript": "3.6.3" | ||
"typescript": "3.8.3" | ||
}, | ||
@@ -99,5 +98,5 @@ "peerDependencies": { | ||
"engines": { | ||
"node": ">=8.0.0" | ||
"node": ">=10" | ||
}, | ||
"sideEffects": false | ||
} |
@@ -99,10 +99,11 @@ # React Polyglot Hooks | ||
| Prop | Type | Required | Description | | ||
| --------------- | ------------------------------------------------------------------------ | -------- | --------------------------------------------------------------------------------- | | ||
| `children` | `Node` | ✅ | Any node(s) accepted by React. | | ||
| `locale` | `string` | ✅ | Current locale, used for pluralization. | | ||
| `phrases` | `{ [key: string]: string }` | ✅ | Key-value pair of translated phrases, can be nested. | | ||
| `allowMissing` | `boolean` | ❌ | Controls whether missing phrase keys in a `t` call is allowed. | | ||
| `onMissingKey` | `(key: string, options: InterpolationOptions, locale: string) => string` | ❌ | A function called when `allowMissing` is `true` and a missing key is encountered. | | ||
| `interpolation` | `{ prefix: string, suffix: string }` | ❌ | Controls the prefix and suffix for an interpolation. | | ||
| Prop | Type | Required | Description | | ||
| --------------- | ---------------------------------------------------------------------------- | -------- | --------------------------------------------------------------------------------- | | ||
| `children` | `Node` | ✅ | Any node(s) accepted by React. | | ||
| `locale` | `string` | ✅ | Current locale, used for pluralization. | | ||
| `phrases` | `{ [key: string]: string }` | ✅ | Key-value pair of translated phrases, can be nested. | | ||
| `allowMissing` | `boolean` | ❌ | Controls whether missing phrase keys in a `t` call is allowed. | | ||
| `interpolation` | `{ prefix: string, suffix: string }` | ❌ | Controls the prefix and suffix for an interpolation. | | ||
| `onMissingKey` | `(key: string, options: InterpolationOptions, locale: string) => string` | ❌ | A function called when `allowMissing` is `true` and a missing key is encountered. | | ||
| `pluralRules` | `{ pluralTypes: PluralTypes, pluralTypeToLanguages: PluralTypeToLanguages }` | ❌ | Custom pluralization rules to be applied to change language(s) behaviour(s). | | ||
@@ -109,0 +110,0 @@ ### `<T>` |
22
151
1
27260
4
284
+ Addedtslib@^1.11.1
+ Addedtslib@1.14.1(transitive)
Updatednode-polyglot@^2.4.0