Skip to content

Commit 27a251d

Browse files
authored
Merge pull request #38 from reasonml-community/rescript
ReScript
2 parents 94203d5 + 46f9a5b commit 27a251d

17 files changed

+2306
-2390
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ dist/
55
.merlin
66
.bsb.lock
77
*.bs.js
8+
yarn-error.log

bsconfig.json

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44
"src",
55
{
66
"dir": "examples",
7-
"type" : "dev",
8-
"subdirs": true,
9-
},
7+
"type": "dev",
8+
"subdirs": true
9+
}
1010
],
11-
"bs-dependencies": ["reason-react"],
11+
"bs-dependencies": ["@rescript/react"],
1212
"reason": {
13-
"react-jsx": 3,
13+
"react-jsx": 3
1414
},
15-
"refmt": 3,
1615
"package-specs": {
1716
"module": "es6",
18-
"in-source": true,
17+
"in-source": true
1918
},
20-
"suffix": ".bs.js",
19+
"suffix": ".bs.js"
2120
}
Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,46 @@
1-
type action =
2-
| SetLocale(Locale.locale);
1+
type action = SetLocale(Locale.locale)
32

4-
let initialState = Locale.En;
3+
let initialState = Locale.En
54

65
let reducer = (_, action) =>
7-
switch (action) {
6+
switch action {
87
| SetLocale(locale) => locale
9-
};
8+
}
109

1110
// As an alternative to using IntlProvider like in the main make function below,
1211
// you can construct the intl instance yourself and pass it to RawIntlProvider instead.
1312
// This is especially useful if you need to create/use the intl instance outside of a
1413
// React component.
1514
module WithRawIntlProvider = {
1615
let createIntlForLocale = locale => {
17-
let intlConfig =
18-
ReactIntl.intlConfig(
19-
~locale=locale->Locale.toString,
20-
~messages=locale->Locale.translations->Translation.toDict,
21-
(),
22-
);
23-
let intlCache = ReactIntl.createIntlCache();
24-
25-
ReactIntl.createIntl(intlConfig, intlCache);
26-
};
27-
28-
[@react.component]
16+
let intlConfig = ReactIntl.intlConfig(
17+
~locale=locale->Locale.toString,
18+
~messages=locale->Locale.translations->Translation.toDict,
19+
(),
20+
)
21+
let intlCache = ReactIntl.createIntlCache()
22+
23+
ReactIntl.createIntl(intlConfig, intlCache)
24+
}
25+
26+
@react.component
2927
let make = () => {
30-
let (locale, dispatch) = reducer->React.useReducer(initialState);
28+
let (locale, dispatch) = reducer->React.useReducer(initialState)
3129

32-
let intl =
33-
React.useMemo1(() => createIntlForLocale(locale), [|locale|]);
30+
let intl = React.useMemo1(() => createIntlForLocale(locale), [locale])
3431

3532
<ReactIntl.RawIntlProvider value=intl>
3633
<Page locale setLocale={locale => locale->SetLocale->dispatch} />
37-
</ReactIntl.RawIntlProvider>;
38-
};
39-
};
34+
</ReactIntl.RawIntlProvider>
35+
}
36+
}
4037

41-
[@react.component]
38+
@react.component
4239
let make = () => {
43-
let (locale, dispatch) = reducer->React.useReducer(initialState);
40+
let (locale, dispatch) = reducer->React.useReducer(initialState)
4441

4542
<ReactIntl.IntlProvider
46-
locale={locale->Locale.toString}
47-
messages={locale->Locale.translations->Translation.toDict}>
43+
locale={locale->Locale.toString} messages={locale->Locale.translations->Translation.toDict}>
4844
<Page locale setLocale={locale => locale->SetLocale->dispatch} />
49-
</ReactIntl.IntlProvider>;
50-
};
45+
</ReactIntl.IntlProvider>
46+
}

examples/Locale.re

Lines changed: 0 additions & 20 deletions
This file was deleted.

examples/Locale.res

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
@module("./translations/en.json")
2+
external en: array<Translation.t> = "default"
3+
@module("./translations/ru.json")
4+
external ru: array<Translation.t> = "default"
5+
6+
type locale =
7+
| En
8+
| Ru
9+
10+
let all = [En, Ru]
11+
12+
let toString = x =>
13+
switch x {
14+
| En => "en"
15+
| Ru => "ru"
16+
}
17+
18+
let translations = x =>
19+
switch x {
20+
| En => en
21+
| Ru => ru
22+
}
Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
1-
open ReactIntl;
1+
open ReactIntl
22

3-
[@react.component]
3+
@react.component
44
let make = (~locale, ~setLocale) => {
5-
let intl = ReactIntl.useIntl();
6-
let className = locale' => locale' === locale ? "active" : "";
5+
let intl = ReactIntl.useIntl()
6+
let className = locale' => locale' === locale ? "active" : ""
77

88
<div className="container">
99
<div className="buttons">
10-
<button
11-
className={Locale.En->className} onClick={_ => Locale.En->setLocale}>
10+
<button className={Locale.En->className} onClick={_ => Locale.En->setLocale}>
1211
{Locale.En->Locale.toString->React.string}
1312
</button>
14-
<button
15-
className={Locale.Ru->className} onClick={_ => Locale.Ru->setLocale}>
13+
<button className={Locale.Ru->className} onClick={_ => Locale.Ru->setLocale}>
1614
{Locale.Ru->Locale.toString->React.string}
1715
</button>
1816
</div>
1917
<div className="message">
2018
<FormattedMessage id="page.hello" defaultMessage="Hello" />
21-
" "->React.string
19+
{" "->React.string}
2220
<FormattedMessage id="page.world" defaultMessage="World" />
2321
</div>
2422
<div>
2523
{intl->Intl.formatMessage(PageLocale.today)->React.string}
26-
" "->React.string
24+
{" "->React.string}
2725
{intl->Intl.formatDate(Js.Date.make())->React.string}
28-
" (intl.formatDate)"->React.string
26+
{" (intl.formatDate)"->React.string}
2927
<br />
3028
{intl->Intl.formatMessage(PageLocale.today)->React.string}
31-
" "->React.string
29+
{" "->React.string}
3230
<FormattedDate value={Js.Date.make()} />
33-
" (FormattedDate)"->React.string
31+
{" (FormattedDate)"->React.string}
3432
</div>
3533
<div>
3634
<FormattedMessage
@@ -39,10 +37,9 @@ let make = (~locale, ~setLocale) => {
3937
values={
4038
"bold": text => <strong> text </strong>,
4139
"italic": text => <em> text </em>,
42-
"combined": (italicBold, text) =>
43-
<strong> italicBold text </strong>,
40+
"combined": (italicBold, text) => <strong> italicBold text </strong>,
4441
}
4542
/>
4643
</div>
47-
</div>;
48-
};
44+
</div>
45+
}

examples/PageLocale.re

Lines changed: 0 additions & 6 deletions
This file was deleted.

examples/PageLocale.res

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
open ReactIntl
2+
@@intl.messages
3+
4+
let hello = {id: "page.hello", defaultMessage: "Hello"}
5+
let world = {id: "page.world", defaultMessage: "World"}
6+
let today = {id: "page.today", defaultMessage: "Today is"}

examples/Translation.re

Lines changed: 0 additions & 22 deletions
This file was deleted.

examples/Translation.res

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
type t = {
2+
id: string,
3+
defaultMessage: string,
4+
message: Js.nullable<string>,
5+
}
6+
7+
let toDict = (translations: array<t>) =>
8+
translations->Belt.Array.reduce(Js.Dict.empty(), (dict, entry) => {
9+
dict->Js.Dict.set(
10+
entry.id,
11+
switch entry.message->Js.Nullable.toOption {
12+
| None
13+
| Some("") =>
14+
entry.defaultMessage
15+
| Some(message) => message
16+
},
17+
)
18+
dict
19+
})

examples/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
</head>
88
<body>
99
<div id="app"></div>
10-
<script src="./index.re"></script>
10+
<script src="./index.bs.js"></script>
1111
</body>
1212
</html>

examples/index.re

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/index.res

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
switch ReactDOM.querySelector("#app") {
2+
| Some(element) => ReactDOM.render(<App />, element)
3+
| None => ()
4+
}

package.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,33 @@
77
"main": "src/ReactIntl.bs.js",
88
"scripts": {
99
"start": "parcel examples/index.html",
10-
"build": "bsb -clean-world -make-world",
11-
"watch": "bsb -clean-world -make-world -w",
12-
"clean": "bsb -clean-world",
10+
"build": "rescript build -with-deps",
11+
"watch": "rescript build -with-deps -w",
12+
"clean": "rescript clean -with-deps",
1313
"extract": "node examples/extractor",
1414
"preextract": "yarn run build",
15-
"format": "bsrefmt --in-place **/*.re",
15+
"format": "yarn rescript format",
1616
"test": "exit 0",
1717
"preversion": "yarn run clean"
1818
},
1919
"peerDependencies": {
20-
"bs-platform": ">=7.2.2",
20+
"@rescript/react": ">=0.10.0",
2121
"react-intl": ">=4.0.0",
22-
"reason-react": ">=0.7.0"
22+
"rescript": ">=9.1.0"
2323
},
2424
"devDependencies": {
25-
"bs-platform": "7.3.2",
26-
"bs-react-intl-extractor-bin": "0.9.0",
25+
"@rescript/react": "^0.10.3",
26+
"bs-react-intl-extractor-bin": "0.10.0",
2727
"bsb-js": "^1.1.7",
28-
"parcel-bundler": "1.12.4",
29-
"react": "16.13.1",
30-
"react-dom": "16.13.1",
28+
"parcel-bundler": "1.12.5",
29+
"react": "17.0.2",
30+
"react-dom": "17.0.2",
3131
"react-intl": "4.5.3",
32-
"reason-react": "0.9.1"
32+
"rescript": "^9.1.2"
3333
},
3434
"repository": {
3535
"type": "git",
36-
"url": "https://github.com/alexfedoseev/bs-react-intl.git"
36+
"url": "https://github.com/reasonml-community/bs-react-intl"
3737
},
3838
"files": [
3939
"src",

0 commit comments

Comments
 (0)