@jetshop/intl 5.11.1 → 5.11.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.mdx +33 -7
- package/package.json +1 -1
package/index.mdx
CHANGED
|
@@ -13,30 +13,54 @@ Localization and translations are essential for building a good cross-border e-c
|
|
|
13
13
|
Much of the content in Flight stores come from the API, which already handles translations. However, there's many cases where you want to keep the texts inside the shop repository, or where you want to do some kind of formatting before rendering the text.
|
|
14
14
|
For this we have a wrapper around the library [format-message](https://github.com/format-message/format-message). The wrapper around format-message is needed to use the correct translations when doing server-side rendering.
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
When you want to render a translation string you should preferrably use the `useIntl` hook to wrap your translation strings:
|
|
17
|
+
|
|
18
|
+
```javascript
|
|
19
|
+
import { useIntl } from '@jetshop/intl';
|
|
20
|
+
|
|
21
|
+
const App = () => {
|
|
22
|
+
const t = useIntl();
|
|
23
|
+
|
|
24
|
+
return <button>{t('This is my translatable button text')}</button>;
|
|
25
|
+
};
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
This will result in a pure text string rendered where you call the function. There are lots of places in Flight where we use an older way of rendering translation strings, importing `t` directly from `@jetshop/intl`.
|
|
29
|
+
This works, but is not recommended, since it has some special behaviour that might cause issues in some places.
|
|
17
30
|
|
|
18
31
|
```javascript
|
|
19
32
|
import t from '@jetshop/intl';
|
|
20
33
|
|
|
21
|
-
const App = () =>
|
|
34
|
+
const App = () => {
|
|
35
|
+
return <button>{t('This is my translatable button text')}</button>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// what is actually being rendered
|
|
39
|
+
const App = () => {
|
|
40
|
+
return <button><IntlContext.Consumer>{t => t('This is my translatable button text')}</IntlContext></button>
|
|
41
|
+
}
|
|
22
42
|
```
|
|
23
43
|
|
|
44
|
+
In most cases this works well, but if you try to use a translation string inside e.g. a input placeholder, you will end up with broken markup.
|
|
45
|
+
|
|
24
46
|
You can also work with inline placeholders:
|
|
25
47
|
|
|
26
48
|
```javascript
|
|
27
|
-
import
|
|
49
|
+
import { useIntl } from '@jetshop/intl';
|
|
28
50
|
|
|
29
|
-
const App = () =>
|
|
30
|
-
|
|
31
|
-
)
|
|
51
|
+
const App = () => {
|
|
52
|
+
const t = useIntl();
|
|
53
|
+
return <span>{t('Hi {name}! How are you doing?', { name: 'User' })}</span>;
|
|
54
|
+
};
|
|
32
55
|
```
|
|
33
56
|
|
|
34
57
|
The [ICU Message Format](http://userguide.icu-project.org/formatparse/messages) that format-message uses, also support pluralization:
|
|
35
58
|
|
|
36
59
|
```javascript
|
|
37
|
-
import
|
|
60
|
+
import { useIntl } from '@jetshop/intl';
|
|
38
61
|
|
|
39
62
|
const App = () => {
|
|
63
|
+
const t = useIntl();
|
|
40
64
|
const [count, setCount] = useState(0);
|
|
41
65
|
return (
|
|
42
66
|
<button onClick={() => setCount(count + 1)}>
|
|
@@ -69,6 +93,8 @@ const App = () => (
|
|
|
69
93
|
);
|
|
70
94
|
```
|
|
71
95
|
|
|
96
|
+
Note that this does not work with the `useIntl` import.
|
|
97
|
+
|
|
72
98
|
## Localization
|
|
73
99
|
|
|
74
100
|
format-message can also help with formatting text according to local standards, such as date, time, numbers etc. It's basically a wrapper around the built in Intl functions in the browser and can be used either together with translation strings as a part of the ICU message format, or by using the exposed helper functions on its own.
|