Skip to content

Commit c7c39da

Browse files
authored
Merge pull request #446 from stasm/react-context-value
Make ReactLocalization the value of FluentContext
2 parents 00a1404 + 397cfd0 commit c7c39da

File tree

6 files changed

+13
-19
lines changed

6 files changed

+13
-19
lines changed

fluent-react/src/context.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import { createContext } from "react";
22
import ReactLocalization from "./localization";
33

4-
export default createContext({
5-
l10n: new ReactLocalization([]),
6-
parseMarkup: null
7-
});
4+
export default createContext(new ReactLocalization([]));

fluent-react/src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* components for more information.
1818
*/
1919

20+
export { default as FluentContext } from "./context";
2021
export { default as LocalizationProvider } from "./provider";
2122
export { default as withLocalization } from "./with_localization";
2223
export { default as Localized } from "./localized";

fluent-react/src/localization.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { mapBundleSync } from "@fluent/sequence";
22
import { CachedSyncIterable } from "cached-iterable";
3+
import createParseMarkup from "./markup";
34

45
/*
56
* `ReactLocalization` handles translation formatting and fallback.
@@ -13,8 +14,9 @@ import { CachedSyncIterable } from "cached-iterable";
1314
* via the `LocalizationProvider` component.
1415
*/
1516
export default class ReactLocalization {
16-
constructor(bundles) {
17+
constructor(bundles, parseMarkup = createParseMarkup()) {
1718
this.bundles = CachedSyncIterable.from(bundles);
19+
this.parseMarkup = parseMarkup;
1820
}
1921

2022
getBundle(id) {

fluent-react/src/localized.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function toArguments(props) {
5353
*/
5454
function Localized(props) {
5555
const { id, attrs, children: child = null } = props;
56-
const { l10n, parseMarkup } = useContext(FluentContext);
56+
const l10n = useContext(FluentContext);
5757

5858
// Validate that the child element isn't an array
5959
if (Array.isArray(child)) {
@@ -141,7 +141,7 @@ function Localized(props) {
141141

142142
// If the message contains markup, parse it and try to match the children
143143
// found in the translation with the props passed to this Localized.
144-
const translationNodes = parseMarkup(messageValue);
144+
const translationNodes = l10n.parseMarkup(messageValue);
145145
const translatedChildren = translationNodes.map(childNode => {
146146
if (childNode.nodeType === childNode.TEXT_NODE) {
147147
return childNode.textContent;

fluent-react/src/provider.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { createElement, memo } from "react";
22
import PropTypes from "prop-types";
33
import FluentContext from "./context";
44
import ReactLocalization from "./localization";
5-
import createParseMarkup from "./markup";
65

76
/*
87
* The Provider component for the `ReactLocalization` class.
@@ -33,10 +32,9 @@ function LocalizationProvider(props) {
3332

3433
return createElement(
3534
FluentContext.Provider,
36-
{ value: {
37-
l10n: new ReactLocalization(props.bundles, props.parseMarkup),
38-
parseMarkup: props.parseMarkup || createParseMarkup()
39-
} },
35+
{
36+
value: new ReactLocalization(props.bundles, props.parseMarkup),
37+
},
4038
props.children
4139
);
4240
}

fluent-react/src/with_localization.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@ import FluentContext from "./context";
33

44
export default function withLocalization(Inner) {
55
function WithLocalization(props) {
6-
const { l10n } = useContext(FluentContext);
6+
const l10n = useContext(FluentContext);
77
return createElement(
88
Inner,
9-
// getString needs to be re-bound on updates to trigger a re-render
109
{
11-
getString: (id, args, fallback) => (
12-
l10n
13-
? l10n.getString(id, args, fallback)
14-
: fallback || id
15-
),
10+
// getString needs to be re-bound to trigger a re-render of Inner
11+
getString: (id, args, fallback) => l10n.getString(id, args, fallback),
1612
...props
1713
},
1814
);

0 commit comments

Comments
 (0)