This commit is contained in:
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