Skip to content

Commit 9b16a6d

Browse files
authored
Merge pull request #475 from macabeus/feat/add-use-translate
(@fluent/react) Add useTranslate
2 parents c27994d + 3d986b8 commit 9b16a6d

File tree

5 files changed

+78
-8
lines changed

5 files changed

+78
-8
lines changed

fluent-react/example/src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useState } from "react";
22
import { Localized } from "@fluent/react";
33
import { FluentDateTime } from "@fluent/bundle";
44
import { Hello } from "./Hello";
5-
import { LocalizedSignIn } from "./SignIn";
5+
import { SignIn } from "./SignIn";
66

77
export function App() {
88
let [date] = useState(() => new Date());
@@ -36,6 +36,6 @@ export function App() {
3636
</p>
3737
</Localized>
3838

39-
<LocalizedSignIn />
39+
<SignIn />
4040
</>;
4141
}

fluent-react/example/src/SignIn.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import React from "react";
2-
import { Localized, withLocalization, WithLocalizationProps } from "@fluent/react";
2+
import { Localized, useLocalization } from "@fluent/react";
3+
4+
export function SignIn() {
5+
const { l10n } = useLocalization()
36

4-
function SignIn(props: WithLocalizationProps) {
57
function showAlert(id: string) {
6-
const { getString } = props;
7-
alert(getString(id));
8+
alert(l10n.getString(id));
89
}
910

1011
return (
@@ -21,5 +22,3 @@ function SignIn(props: WithLocalizationProps) {
2122
</div>
2223
);
2324
}
24-
25-
export const LocalizedSignIn = withLocalization(SignIn);

fluent-react/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ export { LocalizationProvider } from "./provider";
2222
export { withLocalization, WithLocalizationProps } from "./with_localization";
2323
export { Localized, LocalizedProps } from "./localized";
2424
export { MarkupParser } from "./markup";
25+
export { useLocalization } from "./use_localization";
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useContext } from "react";
2+
import { FluentContext } from "./context";
3+
import { ReactLocalization } from "./localization";
4+
5+
/*
6+
* The `useLocalization` hook returns the FluentContext
7+
*/
8+
type useLocalization = () => { l10n: ReactLocalization }
9+
export const useLocalization: useLocalization = () => {
10+
const l10n = useContext(FluentContext);
11+
12+
return { l10n };
13+
};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import React from "react";
2+
import TestRenderer from "react-test-renderer";
3+
import { FluentBundle, FluentResource } from "@fluent/bundle";
4+
import { ReactLocalization, LocalizationProvider, useLocalization } from "../esm/index";
5+
6+
function DummyComponent() {
7+
const { l10n } = useLocalization();
8+
9+
return (
10+
<p>{l10n.getString('foo')}</p>
11+
);
12+
}
13+
14+
describe("useLocalization", () => {
15+
test("render inside of a LocalizationProvider", () => {
16+
const renderer = TestRenderer.create(
17+
<LocalizationProvider l10n={new ReactLocalization([])}>
18+
<DummyComponent />
19+
</LocalizationProvider>
20+
);
21+
expect(renderer.toJSON()).toMatchInlineSnapshot(`
22+
<p>
23+
foo
24+
</p>
25+
`);
26+
});
27+
28+
test("render outside of a LocalizationProvider", () => {
29+
const renderer = TestRenderer.create(<DummyComponent />);
30+
expect(renderer.toJSON()).toMatchInlineSnapshot(`
31+
<p>
32+
foo
33+
</p>
34+
`);
35+
});
36+
37+
test("useLocalization exposes getString from ReactLocalization", () => {
38+
const bundle = new FluentBundle("en", { useIsolating: false });
39+
bundle.addResource(
40+
new FluentResource(`
41+
foo = FOO
42+
`)
43+
);
44+
45+
const renderer = TestRenderer.create(
46+
<LocalizationProvider l10n={new ReactLocalization([bundle])}>
47+
<DummyComponent />
48+
</LocalizationProvider>
49+
);
50+
51+
expect(renderer.toJSON()).toMatchInlineSnapshot(`
52+
<p>
53+
FOO
54+
</p>
55+
`);
56+
});
57+
});

0 commit comments

Comments
 (0)