forked from lingui/js-lingui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrans.js
87 lines (70 loc) · 2.39 KB
/
Trans.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// @flow
import * as React from "react"
import withI18n from "./withI18n"
import type { withI18nProps } from "./withI18n"
import { formatElements } from "./format"
import type { RenderProps } from "./Render"
import Render from "./Render"
type TransProps = {
id?: string,
defaults?: string,
values?: Object,
formats?: Object,
components?: Array<React$Element<*>>,
children?: any
} & withI18nProps &
RenderProps
class Trans extends React.Component<TransProps> {
props: TransProps
componentDidMount() {
if (process.env.NODE_ENV !== "production") {
if (!this.getTranslation() && this.props.children) {
console.warn(
"@lingui/babel-preset-react is probably missing in babel config, " +
"but you are using <Trans> component in a way which requires it. " +
"Either don't use children in <Trans> component or configure babel " +
"to load @lingui/babel-preset-react preset. See tutorial for more info: " +
"https://l.lingui.io/tutorial-i18n-react"
)
}
}
}
getTranslation(): string | ?Array<any> {
const { id = "", defaults, i18n, formats } = this.props
const values = { ...this.props.values }
const components = this.props.components ? [...this.props.components] : []
if (values) {
/*
Related discussion: https://github.com/lingui/js-lingui/issues/183
Values *might* contain React elements with static content.
They're replaced with <INDEX /> placeholders and added to `components`.
Example:
Translation: Hello {name}
Values: { name: <strong>Jane</strong> }
It'll become "Hello <0 />" with components=[<strong>Jane</strong>]
*/
Object.keys(values).forEach(key => {
const value = values[key]
if (!React.isValidElement(value)) return
const index = components.push(value) - 1 // push returns new length of array
values[key] = `<${index}/>`
})
}
const translation =
i18n && typeof i18n._ === "function"
? i18n._(id, values, { defaults, formats })
: id // i18n provider isn't loaded at all
if (!translation) return null
return formatElements(translation, components)
}
render() {
return (
<Render
render={this.props.render}
className={this.props.className}
value={this.getTranslation()}
/>
)
}
}
export default withI18n()(Trans)