Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ReactLocalization the value of FluentContext #446

Merged
merged 3 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions fluent-react/src/context.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { createContext } from "react";
import ReactLocalization from "./localization";

export default createContext({
l10n: new ReactLocalization([]),
parseMarkup: null
});
export default createContext(new ReactLocalization([]));
1 change: 1 addition & 0 deletions fluent-react/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* components for more information.
*/

export { default as FluentContext } from "./context";
export { default as LocalizationProvider } from "./provider";
export { default as withLocalization } from "./with_localization";
export { default as Localized } from "./localized";
4 changes: 3 additions & 1 deletion fluent-react/src/localization.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { mapBundleSync } from "@fluent/sequence";
import { CachedSyncIterable } from "cached-iterable";
import createParseMarkup from "./markup";

/*
* `ReactLocalization` handles translation formatting and fallback.
Expand All @@ -13,8 +14,9 @@ import { CachedSyncIterable } from "cached-iterable";
* via the `LocalizationProvider` component.
*/
export default class ReactLocalization {
constructor(bundles) {
constructor(bundles, parseMarkup = createParseMarkup()) {
this.bundles = CachedSyncIterable.from(bundles);
this.parseMarkup = parseMarkup;
}

getBundle(id) {
Expand Down
4 changes: 2 additions & 2 deletions fluent-react/src/localized.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function toArguments(props) {
*/
function Localized(props) {
const { id, attrs, children: child = null } = props;
const { l10n, parseMarkup } = useContext(FluentContext);
const l10n = useContext(FluentContext);

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

// If the message contains markup, parse it and try to match the children
// found in the translation with the props passed to this Localized.
const translationNodes = parseMarkup(messageValue);
const translationNodes = l10n.parseMarkup(messageValue);
const translatedChildren = translationNodes.map(childNode => {
if (childNode.nodeType === childNode.TEXT_NODE) {
return childNode.textContent;
Expand Down
8 changes: 3 additions & 5 deletions fluent-react/src/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { createElement, memo } from "react";
import PropTypes from "prop-types";
import FluentContext from "./context";
import ReactLocalization from "./localization";
import createParseMarkup from "./markup";

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

return createElement(
FluentContext.Provider,
{ value: {
l10n: new ReactLocalization(props.bundles, props.parseMarkup),
parseMarkup: props.parseMarkup || createParseMarkup()
} },
{
value: new ReactLocalization(props.bundles, props.parseMarkup),
},
props.children
);
}
Expand Down
10 changes: 3 additions & 7 deletions fluent-react/src/with_localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ import FluentContext from "./context";

export default function withLocalization(Inner) {
function WithLocalization(props) {
const { l10n } = useContext(FluentContext);
const l10n = useContext(FluentContext);
return createElement(
Inner,
// getString needs to be re-bound on updates to trigger a re-render
{
getString: (id, args, fallback) => (
l10n
? l10n.getString(id, args, fallback)
: fallback || id
),
// getString needs to be re-bound to trigger a re-render of Inner
getString: (id, args, fallback) => l10n.getString(id, args, fallback),
...props
},
);
Expand Down