Skip to content

Commit cdf4dc5

Browse files
authored
Merge pull request #27 from reasonml-community/create-intl
Add createIntl and RawIntlProvider.
2 parents 0d980bd + c104045 commit cdf4dc5

File tree

4 files changed

+418
-343
lines changed

4 files changed

+418
-343
lines changed

examples/App.re

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,36 @@ let reducer = (_, action) =>
88
| SetLocale(locale) => locale
99
};
1010

11+
// As an alternative to using IntlProvider like in the main make function below,
12+
// you can construct the intl instance yourself and pass it to RawIntlProvider instead.
13+
// This is especially useful if you need to create/use the intl instance outside of a
14+
// React component.
15+
module WithRawIntlProvider = {
16+
let createIntlForLocale = locale => {
17+
let intlConfig =
18+
ReactIntl.intlConfig(
19+
~locale=locale->Locale.toString,
20+
~messages=locale->Locale.translations->Util.translationsToDict,
21+
(),
22+
);
23+
let intlCache = ReactIntl.createIntlCache();
24+
25+
ReactIntl.createIntl(intlConfig, intlCache);
26+
};
27+
28+
[@react.component]
29+
let make = () => {
30+
let (locale, dispatch) = reducer->React.useReducer(initialState);
31+
32+
let intl =
33+
React.useMemo1(() => createIntlForLocale(locale), [|locale|]);
34+
35+
<ReactIntl.RawIntlProvider value=intl>
36+
<Page locale setLocale={locale => locale->SetLocale->dispatch} />
37+
</ReactIntl.RawIntlProvider>;
38+
};
39+
};
40+
1141
[@react.component]
1242
let make = () => {
1343
let (locale, dispatch) = reducer->React.useReducer(initialState);

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
"reason-react": ">=0.7.0"
2323
},
2424
"devDependencies": {
25-
"bs-platform": "5.0.6",
25+
"bs-platform": "5.2.1",
2626
"bs-react-intl-extractor-bin": "0.7.0",
2727
"bsb-js": "^1.1.7",
28-
"parcel-bundler": "1.12.3",
28+
"parcel-bundler": "1.12.4",
2929
"react": "16.8.6",
3030
"react-dom": "16.8.6",
31-
"react-intl": "3.1.6",
31+
"react-intl": "3.4.0",
3232
"reason-react": "0.7.0"
3333
},
3434
"repository": {

src/ReactIntl.re

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,38 @@ external domTag: string => textComponent = "%identity";
140140
external textComponent: React.component('props) => textComponent =
141141
"%identity";
142142

143+
type intlCache;
144+
145+
[@bs.module "react-intl"]
146+
external createIntlCache: unit => intlCache = "createIntlCache";
147+
148+
type intlConfig;
149+
150+
[@bs.obj]
151+
external intlConfig:
152+
(
153+
~locale: string,
154+
~timeZone: string=?,
155+
~formats: Js.t({..})=?, /* TODO */
156+
~textComponent: textComponent=?,
157+
~messages: Js.Dict.t(string),
158+
~defaultLocale: string=?,
159+
~defaultFormats: Js.t({..})=?, /* TODO */
160+
~onError: string => unit=?,
161+
unit
162+
) =>
163+
intlConfig =
164+
"";
165+
166+
[@bs.module "react-intl"]
167+
external createIntl: (intlConfig, intlCache) => Intl.t = "createIntl";
168+
169+
module RawIntlProvider = {
170+
[@react.component] [@bs.module "react-intl"]
171+
external make: (~value: Intl.t, ~children: React.element) => React.element =
172+
"RawIntlProvider";
173+
};
174+
143175
module IntlProvider = {
144176
[@react.component] [@bs.module "react-intl"]
145177
external make:

0 commit comments

Comments
 (0)