Skip to content

Commit 0bc8a06

Browse files
authored
Merge pull request #406 from Gregoor/new-context-api
Use React's new Context API
2 parents ac5f743 + d02a9c5 commit 0bc8a06

39 files changed

+2056
-2164
lines changed

common.mk

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,6 @@ lint:
1919
@eslint --config $(ROOT)/eslint_test.json --max-warnings 0 test/
2020
@echo -e " $(OK) $@"
2121

22-
test:
23-
ifneq (,$(wildcard ./test/index.js))
24-
@nyc --reporter=text --reporter=html mocha \
25-
--recursive --ui tdd \
26-
--require $(ROOT)/mocha_setup \
27-
--require ./test/index \
28-
test/**/*_test.js
29-
else
30-
@nyc --reporter=text --reporter=html mocha \
31-
--recursive --ui tdd \
32-
--require $(ROOT)/mocha_setup \
33-
test/**/*_test.js
34-
endif
35-
3622
html:
3723
ifneq (,$(wildcard ./.esdoc.json))
3824
@esdoc

fluent-bundle/makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ include ../common.mk
55

66
build: index.js compat.js
77

8+
test:
9+
@nyc --reporter=text --reporter=html mocha \
10+
--recursive --ui tdd \
11+
--require $(ROOT)/mocha_setup \
12+
--require ./test/index \
13+
test/**/*_test.js
14+
815
index.js: $(SOURCES)
916
@rollup $(CURDIR)/src/index.js \
1017
--config $(ROOT)/bundle_config.js \

fluent-dedent/makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ include ../common.mk
55

66
build: index.js compat.js
77

8+
test:
9+
@nyc --reporter=text --reporter=html mocha \
10+
--recursive --ui tdd \
11+
--require $(ROOT)/mocha_setup \
12+
test/**/*_test.js
13+
814
index.js: $(SOURCES)
915
@rollup $(CURDIR)/src/index.js \
1016
--config $(ROOT)/bundle_config.js \

fluent-dom/makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ DEPS := cached-iterable:CachedIterable
44

55
include ../common.mk
66

7+
test:
8+
@nyc --reporter=text --reporter=html mocha \
9+
--recursive --ui tdd \
10+
--require $(ROOT)/mocha_setup \
11+
--require ./test/index \
12+
test/**/*_test.js
13+
714
build: index.js compat.js
815

916
index.js: $(SOURCES)

fluent-gecko/makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ include ../common.mk
66
version = $(1)@$(shell node -e "\
77
console.log(require('../$(1)/package.json').version)")
88

9+
test:
10+
@nyc --reporter=text --reporter=html mocha \
11+
--recursive --ui tdd \
12+
--require $(ROOT)/mocha_setup \
13+
test/**/*_test.js
14+
915
build: Fluent.jsm FluentSyntax.jsm Localization.jsm DOMLocalization.jsm l10n.js fluent-react.js
1016

1117
Fluent.jsm: $(SOURCES)

fluent-langneg/makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ GLOBAL := FluentLangNeg
33

44
include ../common.mk
55

6+
test:
7+
@nyc --reporter=text --reporter=html mocha \
8+
--recursive --ui tdd \
9+
--require $(ROOT)/mocha_setup \
10+
test/**/*_test.js
11+
612
build: index.js compat.js
713

814
index.js: $(SOURCES)

fluent-react/babel.config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Jest requires us to specify the transforms it needs to run the tests
2+
module.exports = {
3+
"presets": [
4+
"@babel/preset-react",
5+
["@babel/preset-env", {
6+
"targets": "node >= 8.9.0"
7+
}]
8+
],
9+
"plugins": [
10+
["babel-plugin-transform-rename-import", {
11+
"original": "fluent",
12+
"replacement": "fluent/compat"
13+
}],
14+
"@babel/plugin-proposal-async-generator-functions"
15+
],
16+
};

fluent-react/makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ include ../common.mk
66

77
build: index.js compat.js
88

9+
test:
10+
jest --collect-coverage
11+
912
index.js: $(SOURCES)
1013
@rollup $(CURDIR)/src/index.js \
1114
--config $(ROOT)/bundle_config.js \

fluent-react/package.json

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,20 @@
5555
"react": "^0.14.9 || ^15.0.0 || ^16.0.0"
5656
},
5757
"devDependencies": {
58-
"@babel/preset-react": "^7.0.0-beta.47",
58+
"@babel/plugin-proposal-async-generator-functions": "^7.2.0",
59+
"@babel/preset-env": "^7.5.5",
60+
"@babel/preset-react": "7.0.0",
61+
"babel-jest": "^24.8.0",
5962
"babel-plugin-transform-rename-import": "^2.2.0",
60-
"enzyme": "^3.3.0",
61-
"enzyme-adapter-react-16": "^1.1.1",
62-
"jsdom": "^11.12.0",
63+
"jest": "^24.8.0",
64+
"prettier": "^1.18.2",
6365
"react": "^16.2.0",
6466
"react-dom": "^16.2.0",
65-
"sinon": "^4.2.2"
67+
"react-test-renderer": "^16.8.6"
68+
},
69+
"jest": {
70+
"transformIgnorePatterns": [
71+
"node_modules/(?!(@fluent/sequence)/)"
72+
]
6673
}
6774
}

fluent-react/src/context.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createContext } from "react";
2+
import ReactLocalization from "./localization";
3+
4+
export default createContext({
5+
l10n: new ReactLocalization([]),
6+
parseMarkup: null
7+
});

fluent-react/src/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,3 @@
2020
export { default as LocalizationProvider } from "./provider";
2121
export { default as withLocalization } from "./with_localization";
2222
export { default as Localized } from "./localized";
23-
export { default as ReactLocalization, isReactLocalization }
24-
from "./localization";

fluent-react/src/localization.js

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,24 @@ import { CachedSyncIterable } from "cached-iterable";
66
*
77
* The current negotiated fallback chain of languages is stored in the
88
* `ReactLocalization` instance in form of an iterable of `FluentBundle`
9-
* instances. This iterable is used to find the best existing translation for
9+
* instances. This iterable is used to find the best existing translation for
1010
* a given identifier.
1111
*
12-
* `Localized` components must subscribe to the changes of the
13-
* `ReactLocalization`'s fallback chain. When the fallback chain changes (the
14-
* `bundles` iterable is set anew), all subscribed compontent must relocalize.
15-
*
1612
* The `ReactLocalization` class instances are exposed to `Localized` elements
1713
* via the `LocalizationProvider` component.
1814
*/
1915
export default class ReactLocalization {
2016
constructor(bundles) {
2117
this.bundles = CachedSyncIterable.from(bundles);
22-
this.subs = new Set();
23-
}
24-
25-
/*
26-
* Subscribe a `Localized` component to changes of `bundles`.
27-
*/
28-
subscribe(comp) {
29-
this.subs.add(comp);
30-
}
31-
32-
/*
33-
* Unsubscribe a `Localized` component from `bundles` changes.
34-
*/
35-
unsubscribe(comp) {
36-
this.subs.delete(comp);
37-
}
38-
39-
/*
40-
* Set a new `bundles` iterable and trigger the retranslation.
41-
*/
42-
setBundles(bundles) {
43-
this.bundles = CachedSyncIterable.from(bundles);
44-
45-
// Update all subscribed Localized components.
46-
this.subs.forEach(comp => comp.relocalize());
4718
}
4819

4920
getBundle(id) {
5021
return mapBundleSync(this.bundles, id);
5122
}
5223

53-
/*
54-
* Find a translation by `id` and format it to a string using `args`.
55-
*/
5624
getString(id, args, fallback) {
5725
const bundle = this.getBundle(id);
26+
5827
if (bundle) {
5928
const msg = bundle.getMessage(id);
6029
if (msg && msg.value) {
@@ -78,15 +47,3 @@ export default class ReactLocalization {
7847
console.warn(`[@fluent/react] ${error.name}: ${error.message}`);
7948
}
8049
}
81-
82-
export function isReactLocalization(props, propName) {
83-
const prop = props[propName];
84-
85-
if (prop instanceof ReactLocalization) {
86-
return null;
87-
}
88-
89-
return new Error(
90-
`The ${propName} context field must be an instance of ReactLocalization.`
91-
);
92-
}

0 commit comments

Comments
 (0)