Skip to content

Commit ea35184

Browse files
committed
Bring ReactLocalization back to encapsulate bundles and related methods
1 parent 61a6219 commit ea35184

File tree

6 files changed

+55
-41
lines changed

6 files changed

+55
-41
lines changed

fluent-react/babel.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
"original": "fluent",
1212
"replacement": "fluent/compat"
1313
}],
14-
"@babel/plugin-proposal-async-generator-functions"
14+
"@babel/plugin-proposal-async-generator-functions",
15+
"@babel/plugin-proposal-class-properties"
1516
],
1617
};

fluent-react/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
},
5757
"devDependencies": {
5858
"@babel/plugin-proposal-async-generator-functions": "^7.2.0",
59+
"@babel/plugin-proposal-class-properties": "^7.7.4",
5960
"@babel/preset-env": "^7.5.5",
6061
"@babel/preset-react": "7.0.0",
6162
"babel-jest": "^24.8.0",

fluent-react/src/context.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import { createContext } from "react";
2+
import ReactLocalization from "./localization";
23

3-
export default createContext({getBundle: null, parseMarkup: null});
4+
export default createContext(new ReactLocalization([]));

fluent-react/src/localization.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { mapBundleSync } from "@fluent/sequence";
2+
import { CachedSyncIterable } from "cached-iterable";
3+
4+
/*
5+
* `ReactLocalization` handles translation formatting and fallback.
6+
*
7+
* The current negotiated fallback chain of languages is stored in the
8+
* `ReactLocalization` instance in form of an iterable of `FluentBundle`
9+
* instances. This iterable is used to find the best existing translation for
10+
* a given identifier.
11+
*
12+
* The `ReactLocalization` class instances are exposed to `Localized` elements
13+
* via the `LocalizationProvider` component.
14+
*/
15+
export default class ReactLocalization {
16+
constructor(bundles) {
17+
this.bundles = CachedSyncIterable.from(bundles);
18+
}
19+
20+
getBundle = id => mapBundleSync(this.bundles, id);
21+
22+
getString(id, args, fallback) {
23+
const bundle = this.getBundle(id);
24+
25+
if (bundle) {
26+
const msg = bundle.getMessage(id);
27+
if (msg && msg.value) {
28+
let errors = [];
29+
let value = bundle.formatPattern(msg.value, args, errors);
30+
for (let error of errors) {
31+
this.reportError(error);
32+
}
33+
return value;
34+
}
35+
}
36+
37+
return fallback || id;
38+
}
39+
40+
// XXX Control this via a prop passed to the LocalizationProvider.
41+
// See https://github.com/projectfluent/fluent.js/issues/411.
42+
reportError(error) {
43+
/* global console */
44+
// eslint-disable-next-line no-console
45+
console.warn(`[@fluent/react] ${error.name}: ${error.message}`);
46+
}
47+
}

fluent-react/src/provider.js

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { CachedSyncIterable } from "cached-iterable";
21
import { createElement, memo } from "react";
32
import PropTypes from "prop-types";
4-
import { mapBundleSync } from "@fluent/sequence";
53
import FluentContext from "./context";
4+
import ReactLocalization from "./localization";
65
import createParseMarkup from "./markup";
76

87
/*
@@ -32,41 +31,11 @@ function LocalizationProvider(props) {
3231
throw new Error("The bundles prop must be an iterable.");
3332
}
3433

35-
const bundles = CachedSyncIterable.from(props.bundles);
36-
const parseMarkup = props.parseMarkup || createParseMarkup();
37-
const l10n = {
38-
getBundle: id => mapBundleSync(bundles, id),
39-
getString(id, args, fallback) {
40-
const bundle = l10n.getBundle(id);
41-
42-
if (bundle) {
43-
const msg = bundle.getMessage(id);
44-
if (msg && msg.value) {
45-
let errors = [];
46-
let value = bundle.formatPattern(msg.value, args, errors);
47-
for (let error of errors) {
48-
l10n.reportError(error);
49-
}
50-
return value;
51-
}
52-
}
53-
54-
return fallback || id;
55-
},
56-
// XXX Control this via a prop passed to the LocalizationProvider.
57-
// See https://github.com/projectfluent/fluent.js/issues/411.
58-
reportError(error) {
59-
/* global console */
60-
// eslint-disable-next-line no-console
61-
console.warn(`[@fluent/react] ${error.name}: ${error.message}`);
62-
}
63-
};
64-
6534
return createElement(
6635
FluentContext.Provider,
6736
{ value: {
68-
l10n,
69-
parseMarkup
37+
l10n: new ReactLocalization(props.bundles, props.parseMarkup),
38+
parseMarkup: props.parseMarkup || createParseMarkup()
7039
} },
7140
props.children
7241
);

fluent-react/test/.babelrc

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

0 commit comments

Comments
 (0)