Skip to content

Commit 9acf9df

Browse files
authored
Merge pull request #31 from reasonml-community/message-record
Change message and translation to record types
2 parents a04e495 + 2dc5006 commit 9acf9df

File tree

8 files changed

+38
-49
lines changed

8 files changed

+38
-49
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
- Removed `Intl.now`
1212
- `FormattedRelative` has been renamed to `FormattedRelativeTime` and its API has been changed as well
1313
- `Intl.formatRelative` has been renamed to `Intl.formatRelativeTime` and its API has been changed as well
14+
* **[ BREAKING ]** Changed `ReactIntl.message` to record type.
15+
* **[ BREAKING ]** Removed `ReactIntl.translation`.
1416
* **[ BREAKING ]** Removed `ReactIntlCompat`.
1517

1618
# 1.1.0

examples/App.re

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module WithRawIntlProvider = {
1717
let intlConfig =
1818
ReactIntl.intlConfig(
1919
~locale=locale->Locale.toString,
20-
~messages=locale->Locale.translations->Util.translationsToDict,
20+
~messages=locale->Locale.translations->Translation.toDict,
2121
(),
2222
);
2323
let intlCache = ReactIntl.createIntlCache();
@@ -44,7 +44,7 @@ let make = () => {
4444

4545
<ReactIntl.IntlProvider
4646
locale={locale->Locale.toString}
47-
messages={locale->Locale.translations->Util.translationsToDict}>
47+
messages={locale->Locale.translations->Translation.toDict}>
4848
<Page locale setLocale={locale => locale->SetLocale->dispatch} />
4949
</ReactIntl.IntlProvider>;
5050
};

examples/Locale.re

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[@bs.module "./translations/en.json"]
2-
external en: array(ReactIntl.translation) = "default";
2+
external en: array(Translation.t) = "default";
33
[@bs.module "./translations/ru.json"]
4-
external ru: array(ReactIntl.translation) = "default";
4+
external ru: array(Translation.t) = "default";
55

66
type locale =
77
| En

examples/Page.re

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
open ReactIntl;
2-
open PageLocale;
32

43
[@react.component]
54
let make = (~locale, ~setLocale) => {
@@ -23,12 +22,12 @@ let make = (~locale, ~setLocale) => {
2322
<FormattedMessage id="page.world" defaultMessage="World" />
2423
</div>
2524
<div>
26-
{intl->Intl.formatMessage(pageLocale##today)->React.string}
25+
{intl->Intl.formatMessage(PageLocale.today)->React.string}
2726
" "->React.string
2827
{intl->Intl.formatDate(Js.Date.make())->React.string}
2928
" (intl.formatDate)"->React.string
3029
<br />
31-
{intl->Intl.formatMessage(pageLocale##today)->React.string}
30+
{intl->Intl.formatMessage(PageLocale.today)->React.string}
3231
" "->React.string
3332
<FormattedDate value={Js.Date.make()} />
3433
" (FormattedDate)"->React.string

examples/PageLocale.re

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1-
let pageLocale =
2-
[@intl.messages]
3-
{
4-
"hello": {
5-
"id": "page.hello",
6-
"defaultMessage": "Hello",
7-
},
8-
"world": {
9-
"id": "page.world",
10-
"defaultMessage": "World",
11-
},
12-
"today": {
13-
"id": "page.today",
14-
"defaultMessage": "Today is",
15-
},
16-
};
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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(
9+
Js.Dict.empty(),
10+
(dict, entry) => {
11+
dict->Js.Dict.set(
12+
entry.id,
13+
switch (entry.message->Js.Nullable.toOption) {
14+
| None
15+
| Some("") => entry.defaultMessage
16+
| Some(message) => message
17+
},
18+
);
19+
dict;
20+
},
21+
);
22+
};

examples/Util.re

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

src/ReactIntl.re

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,8 @@ external displayNameFormatOptions:
9090
displayNameFormatOptions;
9191

9292
type message = {
93-
.
94-
"id": string,
95-
"defaultMessage": string,
96-
};
97-
98-
type translation = {
99-
.
100-
"id": string,
101-
"defaultMessage": string,
102-
"message": Js.nullable(string),
93+
id: string,
94+
defaultMessage: string,
10395
};
10496

10597
type part = {

0 commit comments

Comments
 (0)