|
1 | 1 | import { CachedSyncIterable } from "cached-iterable"; |
2 | 2 | import { createElement, useMemo } from "react"; |
3 | 3 | import PropTypes from "prop-types"; |
4 | | -import { mapBundleSync } from "fluent-sequence"; |
| 4 | +import { mapBundleSync } from "@fluent/sequence"; |
5 | 5 | import FluentContext from "./context"; |
6 | 6 | import createParseMarkup from "./markup"; |
7 | 7 |
|
@@ -32,52 +32,56 @@ export default function LocalizationProvider(props) { |
32 | 32 | throw new Error("The bundles prop must be an iterable."); |
33 | 33 | } |
34 | 34 |
|
35 | | - const bundles = useMemo( |
36 | | - () => CachedSyncIterable.from(props.bundles), |
37 | | - [props.bundles] |
38 | | - ); |
39 | | - const parseMarkup = useMemo( |
40 | | - () => props.parseMarkup || createParseMarkup(), |
41 | | - [props.parseMarkup] |
42 | | - ); |
| 35 | + const bundles = useMemo(() => CachedSyncIterable.from(props.bundles), [ |
| 36 | + props.bundles |
| 37 | + ]); |
| 38 | + const parseMarkup = useMemo(() => props.parseMarkup || createParseMarkup(), [ |
| 39 | + props.parseMarkup |
| 40 | + ]); |
43 | 41 | const value = useMemo( |
44 | | - () => ({ |
45 | | - l10n: { |
| 42 | + () => { |
| 43 | + const l10n = { |
46 | 44 | getBundle: id => mapBundleSync(bundles, id), |
47 | 45 | getString(id, args, fallback) { |
48 | | - const bundle = mapBundleSync(bundles, id); |
| 46 | + const bundle = l10n.getBundle(id); |
49 | 47 |
|
50 | 48 | if (bundle) { |
51 | 49 | const msg = bundle.getMessage(id); |
52 | 50 | if (msg && msg.value) { |
53 | 51 | let errors = []; |
54 | 52 | let value = bundle.formatPattern(msg.value, args, errors); |
55 | 53 | for (let error of errors) { |
56 | | - this.reportError(error); |
| 54 | + l10n.reportError(error); |
57 | 55 | } |
58 | 56 | return value; |
59 | 57 | } |
60 | 58 | } |
61 | 59 |
|
62 | 60 | return fallback || id; |
| 61 | + }, |
| 62 | + // XXX Control this via a prop passed to the LocalizationProvider. |
| 63 | + // See https://github.com/projectfluent/fluent.js/issues/411. |
| 64 | + reportError(error) { |
| 65 | + /* global console */ |
| 66 | + // eslint-disable-next-line no-console |
| 67 | + console.warn(`[@fluent/react] ${error.name}: ${error.message}`); |
63 | 68 | } |
64 | | - }, |
65 | | - parseMarkup |
66 | | - }), |
| 69 | + }; |
| 70 | + return { |
| 71 | + l10n, |
| 72 | + parseMarkup |
| 73 | + }; |
| 74 | + }, |
67 | 75 | [bundles, parseMarkup] |
68 | 76 | ); |
69 | 77 |
|
70 | | - return createElement( |
71 | | - FluentContext.Provider, |
72 | | - {value}, |
73 | | - props.children |
74 | | - ); |
| 78 | + return createElement(FluentContext.Provider, { value }, props.children); |
75 | 79 | } |
76 | 80 |
|
77 | 81 | LocalizationProvider.propTypes = { |
78 | 82 | children: PropTypes.element.isRequired, |
79 | 83 | bundles: isIterable, |
80 | | - parseMarkup: PropTypes.func, |
| 84 | + parseMarkup: PropTypes.func |
81 | 85 | }; |
82 | 86 |
|
83 | 87 | function isIterable(props, propName, componentName) { |
|
0 commit comments