react-polyglot-hooks
Advanced tools
Comparing version 0.1.3 to 0.2.0
@@ -5,4 +5,19 @@ # Changelog | ||
## [0.2.0](https://github.com/pmmmwh/react-polyglot-hooks/compare/v0.1.3...v0.2.0) (2019-08-28) | ||
### Bug Fixes | ||
- **i18n:** allow consumption of t options ([0e9371b](https://github.com/pmmmwh/react-polyglot-hooks/commit/0e9371b)) | ||
- **t:** BREAKING - rename options to interpolations ([88d58df](https://github.com/pmmmwh/react-polyglot-hooks/commit/88d58df)) | ||
### Features | ||
- **t:** add a fallback prop for easier consumption ([f919872](https://github.com/pmmmwh/react-polyglot-hooks/commit/f919872)) | ||
### [0.1.3](https://github.com/pmmmwh/react-polyglot-hooks/compare/v0.1.2...v0.1.3) (2019-08-27) | ||
### Bug Fixes | ||
- **publish:** fix missing dist folder in v0.1.2 ([7783d6b](https://github.com/pmmmwh/react-polyglot-hooks/commit/7783d6b)) | ||
### [0.1.2](https://github.com/pmmmwh/react-polyglot-hooks/compare/v0.1.1...v0.1.2) (2019-08-27) | ||
@@ -9,0 +24,0 @@ |
@@ -18,3 +18,6 @@ "use strict"; | ||
var children = _a.children, locale = _a.locale, phrases = _a.phrases, allowMissing = _a.allowMissing, onMissingKey = _a.onMissingKey, interpolation = _a.interpolation; | ||
var polyglot = React.useMemo(function () { | ||
var polyglot = React.useMemo( | ||
// Create a new instance on every change | ||
// This make sure we always consume the latest phrases and Polyglot context | ||
function () { | ||
return new node_polyglot_1.default({ | ||
@@ -28,4 +31,6 @@ locale: locale, | ||
}, [locale, phrases, allowMissing, onMissingKey, interpolation]); | ||
var t = React.useCallback(function t(key) { | ||
return polyglot.t(key); | ||
var t = React.useCallback( | ||
// We use a named function here to aid debugging | ||
function t(key, interpolations) { | ||
return polyglot.t(key, interpolations); | ||
}, [polyglot]); | ||
@@ -32,0 +37,0 @@ return (React.createElement(i18nContext_1.default.Provider, { value: { locale: polyglot.locale(), t: t } }, React.Children.only(children))); |
import * as React from 'react'; | ||
import { InterpolationOptions } from 'node-polyglot'; | ||
export interface TProps { | ||
fallback?: string; | ||
interpolations?: number | InterpolationOptions; | ||
phrase: string; | ||
options?: number | InterpolationOptions; | ||
} | ||
declare const T: React.FC<TProps>; | ||
export default T; |
"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) { | ||
@@ -8,8 +19,14 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var T = function (_a) { | ||
var phrase = _a.phrase, options = _a.options; | ||
var fallback = _a.fallback, interpolations = _a.interpolations, phrase = _a.phrase; | ||
var t = useTranslate_1.default(); | ||
var tOptions = __assign({}, (fallback ? { _: fallback } : undefined), (interpolations && | ||
// Handles smart_count from a number | ||
// This allows the fallback to be safely assigned | ||
(typeof interpolations === 'number' | ||
? { smart_count: interpolations } | ||
: interpolations))); | ||
// HACK: A workaround for the current limitations of TSX with FC | ||
// Ref: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20544 | ||
return t(phrase, options); | ||
return t(phrase, tOptions); | ||
}; | ||
exports.default = T; |
{ | ||
"name": "react-polyglot-hooks", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "Hooks for using Polyglot.js with React.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -8,2 +8,5 @@ # React Polyglot Hooks | ||
[![Coverage Status](https://img.shields.io/codecov/c/github/pmmmwh/react-polyglot-hooks/master.svg)](https://codecov.io/gh/pmmmwh/react-polyglot-hooks/branch/master) | ||
[![Minified Size](https://img.shields.io/bundlephobia/min/react-polyglot-hooks)](https://bundlephobia.com/result?p=react-polyglot-hooks@latest) | ||
[![Min-zipped Size](https://img.shields.io/bundlephobia/minzip/react-polyglot-hooks)](https://bundlephobia.com/result?p=react-polyglot-hooks@latest) | ||
![Code Style](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?logo=prettier) | ||
@@ -102,3 +105,3 @@ [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org) | ||
| `phrases` | `{ [key: string]: string }` | ✅ | Key-value pair of translated phrases, can be nested. | | ||
| `allowMissing` | `boolean` | ❌ | Controls whether missing keys in a `t` call is allowed. | | ||
| `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. | | ||
@@ -113,6 +116,7 @@ | `interpolation` | `{ prefix: string, suffix: string }` | ❌ | Controls the prefix and suffix for an interpolation. | | ||
| Props | Type | Required | Description | | ||
| --------- | --------------------------------------- | -------- | --------------------------------- | | ||
| `phrase` | `string` | ✅ | Key of the needed phrase. | | ||
| `options` | `number` or `{ [key: string]: string }` | ❌ | See `InterpolationOptions` below. | | ||
| Props | Type | Required | Description | | ||
| ---------------- | --------------------------------------- | -------- | --------------------------------------------------- | | ||
| `phrase` | `string` | ✅ | Key of the needed phrase. | | ||
| `fallback` | `string` | ❌ | A fallback to be rendered if the phrase is missing. | | ||
| `interpolations` | `number` or `{ [key: string]: string }` | ❌ | See `InterpolationOptions` below. | | ||
@@ -119,0 +123,0 @@ ### `useLocale` |
19827
150
149